-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathclient.jl
More file actions
445 lines (372 loc) · 15.6 KB
/
client.jl
File metadata and controls
445 lines (372 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
module Clients
using Downloads
using URIs
using JSON
using MbedTLS
using Dates
using TimeZones
using LibCURL
using HTTP
using MIMEs
import Base: convert, show, summary, getproperty, setproperty!, iterate
import ..OpenAPI: APIModel, UnionAPIModel, OneOfAPIModel, AnyOfAPIModel, APIClientImpl, OpenAPIException, InvocationException, to_json, from_json, validate_property, property_type
import ..OpenAPI: str2zoneddatetime, str2datetime, str2date, _json_parse
include("client/clienttypes.jl")
include("client/chunk_readers.jl")
include("client/httplibs/httplibs.jl")
"""
set_user_agent(client::Client, ua::String)
Set the User-Agent header to be sent with all API calls.
"""
set_user_agent(client::Client, ua::String) = set_header(client, "User-Agent", ua)
"""
set_cookie(client::Client, ck::String)
Set the Cookie header to be sent with all API calls.
"""
set_cookie(client::Client, ck::String) = set_header(client, "Cookie", ck)
"""
set_header(client::Client, name::String, value::String)
Set the specified header to be sent with all API calls.
"""
set_header(client::Client, name::String, value::String) = (client.headers[name] = value)
"""
set_timeout(client::Client, timeout::Int)
Set the timeout in seconds for all API calls.
"""
set_timeout(client::Client, timeout::Int) = (client.timeout[] = timeout)
function with_timeout(fn, client::Client, timeout::Integer)
oldtimeout = client.timeout[]
client.timeout[] = timeout
try
fn(client)
finally
client.timeout[] = oldtimeout
end
end
function with_timeout(fn, api::APIClientImpl, timeout::Integer)
client = api.client
oldtimeout = client.timeout[]
client.timeout[] = timeout
try
fn(api)
finally
client.timeout[] = oldtimeout
end
end
is_json_mime(mime::T) where {T <: AbstractString} = ("*/*" == mime) || occursin(r"(?i)application/json(;.*)?", mime) || occursin(r"(?i)application/(.*)\+json(;.*)?", mime)
function select_header_accept(accepts::Vector{String})
isempty(accepts) && (return "application/json")
for accept in accepts
is_json_mime(accept) && (return accept)
end
return join(accepts, ", ")
end
function select_header_content_type(ctypes::Vector{String})
isempty(ctypes) && (return "application/json")
for ctype in ctypes
is_json_mime(ctype) && (return (("*/*" == ctype) ? "application/json" : ctype))
end
return ctypes[1]
end
set_header_accept(ctx::Ctx, accepts::Vector{T}) where {T} = set_header_accept(ctx, convert(Vector{String}, accepts))
function set_header_accept(ctx::Ctx, accepts::Vector{String})
accept = select_header_accept(accepts)
!isempty(accept) && (ctx.header["Accept"] = accept)
return nothing
end
set_header_content_type(ctx::Ctx, ctypes::Vector{T}) where {T} = set_header_content_type(ctx, convert(Vector{String}, ctypes))
function set_header_content_type(ctx::Ctx, ctypes::Vector{String})
if !(ctx.method in ("GET", "HEAD"))
ctx.header["Content-Type"] = select_header_content_type(ctypes)
end
return nothing
end
set_param(params::Dict{String,String}, name::String, value::Nothing; collection_format=",", style="form", location=:query, is_explode=default_param_explode(style)) = nothing
# Choose the default collection_format based on spec.
# Overriding it may not match the spec and there's no check.
# But we do not prevent it to allow for wiggle room, since there are many interpretations in the wild over the loosely defined spec around this.
# TODO: `default_param_explode` needs to be improved to handle location too (query, header, cookie...)
function default_param_explode(style::String)
if style == "deepObject"
true
elseif style == "form"
true
else
false
end
end
function set_param(params::Dict{String,String}, name::String, value; collection_format=",", style="form", location::Symbol=:query, is_explode=default_param_explode(style))
deep_explode = style == "deepObject" && is_explode
if deep_explode
merge!(params, deep_object_serialize(Dict(name=>value)))
return nothing
end
if isa(value, Dict)
# implements the default serialization (style=form, explode=true, location=queryparams)
# as mentioned in https://swagger.io/docs/specification/serialization/
for (k, v) in value
params[k] = string(v)
end
elseif !isa(value, Vector) || isempty(collection_format)
params[name] = string(value)
else
dlm = get(COLL_DLM, collection_format, ",")
isempty(dlm) && throw(OpenAPIException("Unsupported collection format $collection_format"))
params[name] = join(string.(value), dlm)
end
end
prep_args(ctx::Ctx) = prep_args(Val(ctx.client.httplib), ctx)
response(::Type{Nothing}, resp::HTTPLibResponse, body) = nothing::Nothing
response(::Type{T}, resp::HTTPLibResponse, body) where {T <: Real} = response(T, body)::T
response(::Type{T}, resp::HTTPLibResponse, body) where {T <: String} = response(T, body)::T
function response(::Type{T}, resp::HTTPLibResponse, body) where {T}
ctype = get_response_header(resp, "Content-Type", "application/json")
response(T, is_json_mime(ctype), body)::T
end
response(::Type{T}, ::Nothing, body) where {T} = response(T, true, body)
function response(::Type{T}, is_json::Bool, body) where {T}
(length(body) == 0) && return T()
response(T, is_json ? JSON.parse(String(body)) : body)::T
end
response(::Type{String}, data::Vector{UInt8}) = String(data)
response(::Type{T}, data::Vector{UInt8}) where {T<:Real} = parse(T, String(data))
response(::Type{T}, data::T) where {T} = data
response(::Type{ZonedDateTime}, data) = str2zoneddatetime(data)
response(::Type{DateTime}, data) = str2datetime(data)
response(::Type{Date}, data) = str2date(data)
response(::Type{T}, data) where {T} = convert(T, data)
response(::Type{T}, data::AbstractDict{String,Any}) where {T} = from_json(T, data)::T
response(::Type{T}, data::AbstractDict{String,Any}) where {T<:Dict} = convert(T, Dict{String,Any}(data))
response(::Type{Vector{T}}, data::Vector{V}) where {T,V} = T[response(T, v) for v in data]
noop_pre_request_hook(ctx::Ctx) = ctx
noop_pre_request_hook(resource_path::AbstractString, body::Any, headers::Dict{String,String}) = (resource_path, body, headers)
function do_request(ctx::Ctx, stream::Bool=false; stream_to::Union{Channel,Nothing}=nothing)
# call the user hook to allow them to modify the request context
ctx = ctx.pre_request_hook(ctx)
# prepare the url
resource_path = replace(ctx.resource, "{format}"=>"json")
for (k,v) in ctx.path
esc_v = ctx.escape_path_params ? escapeuri(v) : v
resource_path = replace(resource_path, "{$k}"=>esc_v)
end
# append query params if needed
if !isempty(ctx.query)
resource_path = string(URIs.URI(URIs.URI(resource_path); query=escapeuri(ctx.query)))
end
body, kwargs = prep_args(ctx)
# call the user hook again, to allow them to modify the processed request
resource_path, body, headers = ctx.pre_request_hook(resource_path, body, kwargs[:headers])
kwargs[:headers] = headers
if stream
@assert stream_to !== nothing
end
output = Base.BufferStream()
resp, output = do_request(Val(ctx.client.httplib), ctx, resource_path, body, output, kwargs, stream; stream_to=stream_to)
return resp, output
end
function exec(ctx::Ctx, stream_to::Union{Channel,Nothing}=nothing)
stream = stream_to !== nothing
resp, output = do_request(ctx, stream; stream_to=stream_to)
if resp === nothing
# request was interrupted
throw(InvocationException("request was interrupted"))
end
if isa(resp, HTTPLibError)
throw(ApiException(resp))
end
if stream
return stream_to, ApiResponse(resp)
else
data = read(output)
return_type = ctx.client.get_return_type(ctx.return_types, resp.status, String(copy(data)))
if isnothing(return_type)
return nothing, ApiResponse(resp)
end
return response(return_type, resp, data), ApiResponse(resp)
end
end
function setproperty!(o::T, name::Symbol, val) where {T<:APIModel}
validate_property(T, name, val)
fieldtype = property_type(T, name)
if isa(val, fieldtype)
return setfield!(o, name, val)
elseif fieldtype === ZonedDateTime
return setfield!(o, name, str2zoneddatetime(val))
elseif fieldtype === DateTime
return setfield!(o, name, str2datetime(val))
elseif fieldtype === Date
return setfield!(o, name, str2date(val))
else
ftval = try
convert(fieldtype, val)
catch
fieldtype(val)
end
return setfield!(o, name, ftval)
end
end
"""
getpropertyat(o::T, path...) where {T<:APIModel}
Returns the property at the specified path.
The path can be a single property name or a chain of property names separated by dots, representing a nested property.
"""
function getpropertyat(o::T, path...) where {T<:APIModel}
val = getproperty(o, Symbol(path[1]))
rempath = path[2:end]
(length(rempath) == 0) && (return val)
if isa(val, Vector)
if isa(rempath[1], Integer)
val = val[rempath[1]]
rempath = rempath[2:end]
else
return [getpropertyat(item, rempath...) for item in val]
end
end
(length(rempath) == 0) && (return val)
getpropertyat(val, rempath...)
end
"""
haspropertyat(o::T, path...) where {T<:APIModel}
Returns true if the supplied object has the property at the specified path.
"""
function haspropertyat(o::T, path...) where {T<:APIModel}
p1 = Symbol(path[1])
ret = hasproperty(o, p1)
rempath = path[2:end]
(length(rempath) == 0) && (return ret)
ret || (return false)
val = getproperty(o, p1)
if isa(val, Vector)
if isa(rempath[1], Integer)
ret = length(val) >= rempath[1]
if ret
val = val[rempath[1]]
rempath = rempath[2:end]
end
else
return [haspropertyat(item, rempath...) for item in val]
end
end
(length(rempath) == 0) && (return ret)
haspropertyat(val, rempath...)
end
Base.hasproperty(o::T, name::Symbol) where {T<:APIModel} = ((name in propertynames(o)) && (getproperty(o, name) !== nothing))
convert(::Type{T}, json::AbstractDict{String,Any}) where {T<:APIModel} = from_json(T, json)
convert(::Type{T}, v::Nothing) where {T<:APIModel} = T()
convert(::Type{T}, v::T) where {T<:OneOfAPIModel} = v
convert(::Type{T}, json::AbstractDict{String,Any}) where {T<:OneOfAPIModel} = from_json(T, json)
convert(::Type{T}, v) where {T<:OneOfAPIModel} = T(v)
convert(::Type{T}, v::String) where {T<:OneOfAPIModel} = T(v)
convert(::Type{T}, v::T) where {T<:AnyOfAPIModel} = v
convert(::Type{T}, json::AbstractDict{String,Any}) where {T<:AnyOfAPIModel} = from_json(T, json)
convert(::Type{T}, v) where {T<:AnyOfAPIModel} = T(v)
convert(::Type{T}, v::String) where {T<:AnyOfAPIModel} = T(v)
show(io::IO, model::T) where {T<:UnionAPIModel} = print(io, JSON.json(model.value, 2))
show(io::IO, model::T) where {T<:APIModel} = print(io, JSON.json(model, 2))
summary(io::IO, model::T) where {T<:APIModel} = print(io, T)
"""
is_longpoll_timeout(ex::Exception)
Examine the supplied exception and return true if the reason is timeout
of a long polling request. If the exception is a nested exception of type
CompositeException or TaskFailedException, then navigates through the nested
exception values to examine the leaves.
"""
is_longpoll_timeout(ex) = false
is_longpoll_timeout(ex::TaskFailedException) = is_longpoll_timeout(ex.task.exception)
is_longpoll_timeout(ex::CompositeException) = any(is_longpoll_timeout, ex.exceptions)
function is_longpoll_timeout(ex::ApiException)
# All client library wrappers ensure that the reason string format is the same for longpoll timeouts
ex.status == 200 && match(r"Operation timed out after \d+ milliseconds with \d+ bytes received", ex.reason) !== nothing
end
"""
is_request_interrupted(ex::Exception)
Examine the supplied exception and return true if the reason is that the
request was interrupted. If the exception is a nested exception of type
CompositeException or TaskFailedException, then navigates through the nested
exception values to examine the leaves.
"""
is_request_interrupted(ex) = false
is_request_interrupted(ex::TaskFailedException) = is_request_interrupted(ex.task.exception)
is_request_interrupted(ex::CompositeException) = any(is_request_interrupted, ex.exceptions)
is_request_interrupted(ex::InvocationException) = ex.reason == "request was interrupted"
"""
storefile(api_call::Function;
folder::AbstractString = pwd(),
rename_file::String="",
)::Tuple{Any,ApiResponse,String}
Helper method that stores the result of an API call that returns file
contents (as binary or text string) into a file.
Convenient to use it in a do block. Returns the path where file is stored additionally.
E.g.:
```
_result, _http_response, file = OpenAPI.Clients.storefile() do
# Invoke the OpenaPI method that returns file contents.
# This is the method that returns a tuple of (result, http_response).
# The result is the file contents as binary or text string.
fetch_file(api, "reports", "category1")
end
```
Parameters:
- `api_call`: The OpenAPI function call that returns file contents (as binary or text string). See example in method description.
- `folder`: Location to store file, defaults to `pwd()`.
- `filename`: Use this filename, overrides any filename that may be there in the `Content-Disposition` header.
Returns: (result, http_response, file_path)
"""
function storefile(api_call::Function;
folder::AbstractString = pwd(),
filename::Union{String,Nothing} = nothing,
)::Tuple{Any,ApiResponse,String}
result, http_response = api_call()
if isnothing(filename)
filename = extract_filename(http_response)
end
mkpath(folder)
filepath = joinpath(folder, filename)
open(filepath, "w") do io
write(io, result)
end
return result, http_response, filepath
end
const content_disposition_re = r"filename\*?=['\"]?(?:UTF-\d['\"]*)?([^;\r\n\"']*)['\"]?;?"
"""
extract_filename(resp)::String
Extracts the filename from the `Content-Disposition` header of the HTTP response.
If not found, then creates a filename from the `Content-Type` header.
"""
extract_filename(resp::ApiResponse) = extract_filename(resp.raw)
function extract_filename(resp::HTTPLibResponse)::String
# attempt to extract filename from content-disposition header
content_disposition_str = get_response_header(resp, "content-disposition", "")
m = match(content_disposition_re, content_disposition_str)
if !isnothing(m) && !isempty(m.captures) && !isnothing(m.captures[1])
return m.captures[1]
end
# attempt to create a filename from content-type header
content_type_str = get_response_header(resp, "content-type", "")
return string("response", extension_from_mime(MIME(content_type_str)))
end
function deep_object_serialize(dict::Dict, parent_key::String = "")
parts = Pair[]
for (key, value) in dict
new_key = parent_key == "" ? key : "$parent_key[$key]"
if isa(value, Dict)
append!(parts, collect(deep_object_serialize(value, new_key)))
elseif isa(value, Vector)
for (i, v) in enumerate(value)
push!(parts, "$new_key[$(i-1)]"=>"$v")
end
else
push!(parts, "$new_key"=>"$value")
end
end
return Dict(parts)
end
function request_supports_interrupt()
for m in methods(request)
if :interrupt in Base.kwarg_decl(m)
return true
end
end
return false
end
end # module Clients