Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Julia 0.7; switch to HTTP.jl #20

Merged
merged 9 commits into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
julia 0.6
julia 0.7

JSON 0.7.0
MbedTLS 0.3.1
Requests
Dates
Base64
Printf
JSON
MbedTLS
HTTP
Libz
MsgPack
URIParser
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Upload an object to the _a12345foo_ bucket:

```julia
# String containing the contents of test_image.jpg. The semi-colon avoids an error caused by printing the returned value.
file_contents = readstring(open("test_image.jpg", "r"));
file_contents = read(open("test_image.jpg", "r"), String);

# Upload
storage(:Object, :insert, "a12345foo"; # Returns metadata about the object
Expand Down
2 changes: 0 additions & 2 deletions src/GoogleCloud.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
__precompile__(true)

"""
Google Cloud APIs
"""
Expand Down
48 changes: 25 additions & 23 deletions src/api/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ module api

export APIRoot, APIResource, APIMethod, set_session!, get_session, iserror

using Base.Dates
using Dates, Base64

using Requests
using HTTP
import MbedTLS
import URIParser
import Libz
import JSON

Expand All @@ -33,7 +32,7 @@ path_tokens("/{foo}/{bar}/x/{baz}")
"{baz}"
```
"""
path_tokens(path::AbstractString) = matchall(r"{\w+}", path)
path_tokens(path::AbstractString) = collect(eachmatch(r"{\w+}", path))

"""
path_replace(path, values)
Expand All @@ -48,10 +47,13 @@ path_replace("/{foo}/{bar}/{baz}", ["this", "is", "it"])
"/this/is/it"
```
"""
path_replace(path::AbstractString, values) = reduce((x, y) -> replace(x, y[1], URIParser.escape(y[2]), 1), path, zip(path_tokens(path), values))
function path_replace(path::AbstractString, values)
reduce((x, y) -> replace(x, y[1], HTTP.URIs.escapeuri(y[2]), 1),
path, zip(path_tokens(path), values))
end

"""Check if response is/contains an error"""
iserror(x::Associative{Symbol}) = haskey(x, :error)
iserror(x::AbstractDict{Symbol}) = haskey(x, :error)
iserror(::Any) = false

"""
Expand All @@ -66,7 +68,7 @@ struct APIMethod
default_params::Dict{Symbol, Any}
transform::Function
function APIMethod(verb::Symbol, path::AbstractString, description::AbstractString,
default_params::Associative{Symbol}=Dict{Symbol, Any}();
default_params::AbstractDict{Symbol}=Dict{Symbol, Any}();
transform=(x, t) -> x)
new(verb, path, description, default_params, transform)
end
Expand Down Expand Up @@ -134,7 +136,7 @@ struct APIRoot
An API rooted at `path` with specified OAuth 2.0 access scopes and
resources.
"""
function APIRoot(path::AbstractString, scopes::Associative{<: AbstractString, <: AbstractString}; resources...)
function APIRoot(path::AbstractString, scopes::AbstractDict{<: AbstractString, <: AbstractString}; resources...)
if !isurl(path)
throw(APIError("API root must be a valid URL."))
end
Expand Down Expand Up @@ -174,7 +176,7 @@ Base.show(io::IO, x::APIRoot) = print(io, x)
Set the default session for a specific API. Set session to `nothing` to
forget session.
"""
function set_session!(api::APIRoot, session::Union{GoogleSession, Void})
function set_session!(api::APIRoot, session::Union{GoogleSession, Nothing})
_default_session[api] = session
nothing
end
Expand Down Expand Up @@ -214,13 +216,14 @@ Execute a method against the provided path arguments.

Optionally provide parameters and data (with optional MIME content-type).
"""
function execute(session::GoogleSession, resource::APIResource, method::APIMethod, path_args::AbstractString...;
data::Union{AbstractString, Associative, Vector{UInt8}, Void}=nothing,
gzip::Bool=false, content_type::AbstractString="application/json",
debug::Bool=false, raw::Bool=false,
max_backoff::TimePeriod=Second(64), max_attempts::Int64=10,
params...
)
function execute(session::GoogleSession, resource::APIResource, method::APIMethod,
path_args::AbstractString...;
data::Union{AbstractString, AbstractDict, Vector{UInt8}, Nothing}=nothing,
gzip::Bool=false, content_type::AbstractString="application/json",
debug::Bool=false, raw::Bool=false,
max_backoff::TimePeriod=Second(64), max_attempts::Int64=10,
params...)

if length(path_args) != length(path_tokens(method.path))
throw(APIError("Number of path arguments do not match"))
end
Expand All @@ -244,7 +247,7 @@ function execute(session::GoogleSession, resource::APIResource, method::APIMetho
if !isempty(content_type)
headers["Content-Type"] = content_type
end
if isa(data, Associative) || content_type == "application/json"
if isa(data, AbstractDict) || content_type == "application/json"
data = JSON.json(data)
elseif isempty(data)
headers["Content-Length"] = "0"
Expand Down Expand Up @@ -275,12 +278,11 @@ function execute(session::GoogleSession, resource::APIResource, method::APIMetho
info("Attempt: $attempt")
end
res = try
Requests.do_request(
URIParser.URI(path_replace(method.path, path_args)), string(method.verb);
query=params, data=data, headers=headers, compressed=true
)
HTTP.request(string(method.verb),
HTTP.URIs.URI(path_replace(method.path, path_args)), headers, data;
query=params )
catch e
if isa(e, Base.UVError) && e.code in (Base.UV_ECONNRESET, Base.UV_ECONNREFUSED, Base.UV_ECONNABORTED, Base.UV_EPIPE, Base.UV_ETIMEDOUT)
if isa(e, Base.IOError) && e.code in (Base.UV_ECONNRESET, Base.UV_ECONNREFUSED, Base.UV_ECONNABORTED, Base.UV_EPIPE, Base.UV_ETIMEDOUT)
elseif isa(e, MbedTLS.MbedException) && e.ret in (MbedTLS.MBEDTLS_ERR_SSL_TIMEOUT, MbedTLS.MBEDTLS_ERR_SSL_CONN_EOF)
else
rethrow(e)
Expand Down Expand Up @@ -315,7 +317,7 @@ function execute(session::GoogleSession, resource::APIResource, method::APIMetho
if get(res.headers, "Content-Length", "") == "0"
nothing
else
result = Requests.json(res; dicttype=Dict{Symbol, Any})
result = JSON.json(res; dicttype=Dict{Symbol, Any})
raw || (statuscode(res) >= 400) ? result : method.transform(result, resource.transform)
end
else
Expand Down
6 changes: 4 additions & 2 deletions src/api/datastore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Google Cloud Datastore API
"""
module _datastore

using Base64

export datastore

using ..api
Expand All @@ -12,7 +14,7 @@ using ...root
module types
export ValueType, OperatorType, wrap, unwrap

using Base.Dates
using Dates

import JSON

Expand Down Expand Up @@ -47,7 +49,7 @@ module types
AbstractString => stringValue,
Char => stringValue,
Enum => stringValue,
Void => nullValue,
Nothing => nullValue,
)
function wrap(x)
T = typeof(x)
Expand Down
54 changes: 30 additions & 24 deletions src/collection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module collection
export
KeyStore, connect!, destroy!, watch, unwatch

using Base.Dates
using Dates
using Printf

import JSON
import MsgPack
Expand Down Expand Up @@ -37,7 +38,7 @@ val_format_map = Dict{Symbol, Tuple{Function, Function}}(
"""
High-level container wrapping a Google Storage bucket
"""
immutable KeyStore{K, V} <: Associative{K, V}
struct KeyStore{K, V} <: AbstractDict{K, V}
bucket_name::String
session::GoogleSession
key_decoder::Function
Expand Down Expand Up @@ -88,21 +89,21 @@ function connect!(store::KeyStore; location::AbstractString="US", empty::Bool=fa
store
end

function destroy!{K, V}(store::KeyStore{K, V})
function destroy!(store::KeyStore{K, V}) where {K, V}
response = storage(:Bucket, :delete, store.bucket_name; session=store.session, fields="")
if iserror(response)
error("Unable to delete bucket: $(response[:error][:message])")
end
nothing
end

function Base.print{K, V}(io::IO, store::KeyStore{K, V})
function Base.print(io::IO, store::KeyStore{K, V}) where {K, V}
print(io, @sprintf("""KeyStore{%s, %s}("%s")""", K, V, store.bucket_name))
end
Base.show(io::IO, store::KeyStore) = print(io, store)
Base.display(store::KeyStore) = print(store)

function Base.setindex!{K, V}(store::KeyStore{K, V}, val::V, key::K)
function Base.setindex!(store::KeyStore{K, V}, val::V, key::K) where {K, V}
name = store.key_encoder(key)
data = store.writer(val)
response = storage(:Object, :insert, store.bucket_name; session=store.session,
Expand All @@ -114,7 +115,7 @@ function Base.setindex!{K, V}(store::KeyStore{K, V}, val::V, key::K)
store
end

function Base.getindex{K, V}(store::KeyStore{K, V}, key::K)
function Base.getindex(store::KeyStore{K, V}, key::K) where {K, V}
name = store.key_encoder(key)
data = storage(:Object, :get, store.bucket_name, name; session=store.session)
if iserror(data)
Expand All @@ -124,7 +125,7 @@ function Base.getindex{K, V}(store::KeyStore{K, V}, key::K)
convert(V, val)
end

function Base.get{K, V}(store::KeyStore{K, V}, key::K, default)
function Base.get(store::KeyStore{K, V}, key::K, default) where {K, V}
try
return getindex(store, key)
catch e
Expand All @@ -136,14 +137,14 @@ function Base.get{K, V}(store::KeyStore{K, V}, key::K, default)
end
end

function Base.haskey{K, V}(store::KeyStore{K, V}, key::K)
function Base.haskey(store::KeyStore{K, V}, key::K) where {K, V}
name = store.key_encoder(key)
response = storage(:Object, :get, store.bucket_name, name; session=store.session, alt="", fields="")
!iserror(response)
end

# WARNING: potential for race condition. don't zip keys with values... use collect instead
function Base.keys{K, V}(store::KeyStore{K, V})
function Base.keys(store::KeyStore{K, V}) where {K, V}
result = K[]
for response in storage(:Object, :list, store.bucket_name; session=store.session, fields="items(name)")
name = response[:name]
Expand All @@ -157,9 +158,11 @@ function Base.keys{K, V}(store::KeyStore{K, V})
end

# avoiding race condition where values might have been deleted after keys were generated
Base.values{K, V}(store::KeyStore{K, V}) = (x for x in (get(store, key, Void) for key in keys(store)) if x !== Void)
@inline function Base.values(store::KeyStore{K, V}) where {K, V}
(x for x in (get(store, key, Nothing) for key in keys(store)) if x !== Voide)
end

function Base.delete!{K, V}(store::KeyStore{K, V}, key::K)
function Base.delete!(store::KeyStore{K, V}, key::K) where {K, V}
name = store.key_encoder(key)
response = storage(:Object, :delete, store.bucket_name, name; session=store.session, fields="")
if iserror(response)
Expand All @@ -168,19 +171,19 @@ function Base.delete!{K, V}(store::KeyStore{K, V}, key::K)
store
end

function Base.pop!{K, V}(store::KeyStore{K, V}, key::K)
function Base.pop!(store::KeyStore{K, V}, key::K) where {K, V}
val = getindex(store, key)
delete!(store, key)
val
end

function Base.pop!{K, V}(store::KeyStore{K, V}, key::K, default)
function Base.pop!(store::KeyStore{K, V}, key::K, default) where {K, V}
val = get(store, key, default)
delete!(store, key)
val
end

function Base.merge!{K, V}(store::KeyStore{K, V}, d::Associative{K, V})
function Base.merge!(store::KeyStore{K, V}, d::AbstractDict{K, V}) where {K, V}
for (k, v) in d
store[k] = v
end
Expand All @@ -190,7 +193,7 @@ Base.length(store::KeyStore) = length(collect(keys(store)))

Base.isempty(store::KeyStore) = length(store) == 0

function Base.empty!{K, V}(store::KeyStore{K, V})
function Base.empty!(store::KeyStore{K, V}) where {K, V}
for object in storage(:Object, :list, store.bucket_name; session=store.session, fields="items(name)")
response = storage(:Object, :delete, store.bucket_name, object[:name]; session=store.session, fields="")
if iserror(response)
Expand All @@ -201,36 +204,39 @@ function Base.empty!{K, V}(store::KeyStore{K, V})
end

"""Skip over missing keys if any deleted since key list was geenrated"""
function fast_forward{K, V}(store::KeyStore{K, V}, key_list)
function fast_forward(store::KeyStore{K, V}, key_list) where {K, V}
while !isempty(key_list)
key = pop!(key_list)
val = get(store, key, Void)
if val !== Void
val = get(store, key, Nothing)
if val !== Nothing
return Pair{K, V}(key, val)
end
end
nothing
end

function Base.start{K, V}(store::KeyStore{K, V})
function Base.start(store::KeyStore{K, V}) where {K, V}
key_list = collect(keys(store))
return (fast_forward(store, key_list), key_list)
end

function Base.next{K, V}(store::KeyStore{K, V}, state)
function Base.next(store::KeyStore{K, V}, state) where {K, V}
pair, key_list = state
return pair, (fast_forward(store, key_list), key_list)
end

function Base.done{K, V}(store::KeyStore{K, V}, state)
function Base.done(store::KeyStore{K, V}, state) where {K, V}
pair, key_list = state
pair === nothing
end

Base.iteratorsize{K, V}(::Type{KeyStore{K, V}}) = Base.SizeUnknown()
@inline function Base.IteratorSize(::Type{KeyStore{K, V}}) where {K, V}
Base.SizeUnknown()
end

# notifications
function watch{K, V}(store::KeyStore{K, V}, channel_id::AbstractString, address::AbstractString)
function watch(store::KeyStore{K, V}, channel_id::AbstractString,
address::AbstractString) where {K, V}
if !isempty(store.channel)
error("Already watching: $store.channel")
end
Expand All @@ -244,7 +250,7 @@ function watch{K, V}(store::KeyStore{K, V}, channel_id::AbstractString, address:
store.channel = channel
end

function unwatch{K, V}(store::KeyStore{K, V})
function unwatch(store::KeyStore{K, V}) where {K, V}
response = storage(:Channel, :stop; data=store.channel, session=store.session)
if iserror(response)
error("Unable to unwatch bucket: $(response[:error][:message])")
Expand Down
Loading