Skip to content

Commit b2d0af2

Browse files
committed
WIP: abstract away httpbits so I can swap out Requests for HTTP.jl some day
1 parent 2b0daf4 commit b2d0af2

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/v2.jl

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,37 @@ module PlotlyV2
44
# Imports/Setup #
55
# ------------- #
66

7-
using Plotly, JSON, Requests
7+
using Plotly
88

99
const API_ROOT = "https://api.plot.ly/v2/"
1010
const VERSION = string(Pkg.installed("Plotly"))
1111

12+
import Requests
1213
const METHOD_MAP = Dict(
1314
:get => Requests.get,
1415
:post => Requests.post,
1516
:put => Requests.put,
1617
:delete => Requests.delete,
1718
:patch => Requests.patch,
1819
)
20+
function validate_response(res::Requests.Response)
21+
code = Requests.statuscode(res)
22+
if code > 204
23+
uri = Requests.requestfor(res).uri
24+
throw(PlotlyAPIError("Request $uri failed with code $code", res))
25+
end
26+
end
27+
28+
get_json_data(res::Requests.Response) = Requests.json(res)
29+
get_headers(res::Requests.Response) = Requests.headers(res)
1930

20-
# import HTTP
31+
# import HTTP, JSON
2132
# const METHOD_MAP = Dict(
22-
# :get => HTTP.get,
23-
# :post => HTTP.post,
24-
# :put => HTTP.put,
25-
# :delete => HTTP.delete,
26-
# :patch => HTTP.patch,
33+
# :get => function (args...;json=Dict(), kwargs...) HTTP.get(args...; body=json, kwargs...) end,
34+
# :post => function (args...;json=Dict(), kwargs...) HTTP.post(args...; body=json, kwargs...) end,
35+
# :put => function (args...;json=Dict(), kwargs...) HTTP.put(args...; body=json, kwargs...) end,
36+
# :delete => function (args...;json=Dict(), kwargs...) HTTP.delete(args...; body=json, kwargs...) end,
37+
# :patch => function (args...;json=Dict(), kwargs...) HTTP.patch(args...; body=json, kwargs...) end,
2738
# )
2839
# function validate_response(res::HTTP.Response)
2940
# if res.status > 204
@@ -32,6 +43,7 @@ const METHOD_MAP = Dict(
3243
# end
3344
# end
3445
# get_json_data(res::HTTP.Response) = JSON.parse(deepcopy(res.body))
46+
# get_headers(res::HTTP.Response) = res.headers
3547

3648
# --------------- #
3749
# Tools/Utilities #
@@ -51,16 +63,6 @@ struct PlotlyAPIError <: Exception
5163
res
5264
end
5365

54-
function validate_response(res::Requests.Response)
55-
code = Requests.statuscode(res)
56-
if code > 204
57-
uri = Requests.requestfor(res).uri
58-
# TODO: provide meaningful error message based on request url + status
59-
throw(PlotlyAPIError("Request $uri failed with code $code", res))
60-
end
61-
end
62-
63-
get_json_data(res::Requests.Response) = Requests.json(res)
6466

6567
function basic_auth(username, password)
6668
# ref https://github.com/plotly/plotly.py/blob/master/plotly/api/utils.py
@@ -112,7 +114,7 @@ function request(method, endpoint; fid=nothing, route=nothing, json=nothing, kwa
112114
query_params = Dict()
113115
for (k, v) in kwargs
114116
if v !== nothing
115-
query_params[k] = v
117+
query_params[string(k)] = v
116118
end
117119
end
118120
if Symbol(method) in (:post, :patch, :put) && json !== nothing
@@ -127,7 +129,7 @@ end
127129

128130
function request_data(method, endpoint; kwargs...)
129131
res = request(method, endpoint; kwargs...)
130-
content_type = get(Requests.headers(res), "Content-Type", "application/json")
132+
content_type = get(get_headers(res), "Content-Type", "application/json")
131133
if startswith(content_type, "application/json")
132134
return get_json_data(res)
133135
else

0 commit comments

Comments
 (0)