Skip to content

Commit a1f9f54

Browse files
committed
Merge pull request #7 from plotly/getrequests
add getFile API
2 parents 3048312 + a39c907 commit a1f9f54

File tree

2 files changed

+87
-47
lines changed

2 files changed

+87
-47
lines changed

src/Plotly.jl

Lines changed: 74 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module Plotly
2-
using HTTPClient
2+
using HTTPClient.HTTPC
33
using JSON
44

5-
type PlotlyAccount
5+
type PlotlyAccount
66
username::String
77
api_key::String
88
end
@@ -31,12 +31,12 @@ default_opts = {
3131
"version" => "0.2"}
3232

3333
function signup(username::String, email::String)
34-
r = HTTPClient.HTTPC.post("http://plot.ly/apimkacct",
35-
merge(default_opts,
36-
{"un" => username,
34+
r = post("http://plot.ly/apimkacct",
35+
merge(default_opts,
36+
{"un" => username,
3737
"email" => email}))
3838
if r.http_code == 200
39-
results = JSON.parse(bytestring(r.body))
39+
results = JSON.parse(bytestring(r.body))
4040
for flag in ["error","warning","message"]
4141
if haskey(results, flag) && results[flag] != ""
4242
println(results[flag])
@@ -50,7 +50,7 @@ function signup(username::String, email::String)
5050
end
5151

5252
function signin(username::String, api_key::String)
53-
global plotlyaccount
53+
global plotlyaccount
5454
plotlyaccount = PlotlyAccount(username,api_key)
5555
end
5656

@@ -61,12 +61,15 @@ function plot(data::Array,options=Dict())
6161
return
6262
end
6363
opt = merge(default_options,options)
64-
r = HTTPClient.HTTPC.post("http://plot.ly/clientresp",
65-
merge(default_opts,
66-
{"un" => plotlyaccount.username,
67-
"key" => plotlyaccount.api_key,
68-
"args" => json(data),
69-
"kwargs" => json(opt)}))
64+
r = post("http://plot.ly/clientresp",
65+
merge(default_opts,
66+
{
67+
"un" => plotlyaccount.username,
68+
"key" => plotlyaccount.api_key,
69+
"args" => json(data),
70+
"kwargs" => json(opt)
71+
})
72+
)
7073
body=JSON.parse(bytestring(r.body))
7174
if r.http_code != 200
7275
error(["r.http_code"])
@@ -90,7 +93,7 @@ function layout(layout_opts::Dict,meta_opts=Dict())
9093

9194
merge!(meta_opts,get_required_params(["filename","fileopt"],meta_opts))
9295

93-
r = HTTPClient.HTTPC.post("http://plot.ly/clientresp",
96+
r = post("http://plot.ly/clientresp",
9497
merge(default_opts,
9598
{"un" => plotlyaccount.username,
9699
"key" => plotlyaccount.api_key,
@@ -109,7 +112,7 @@ function style(style_opts,meta_opts=Dict())
109112

110113
merge!(meta_opts,get_required_params(["filename","fileopt"],meta_opts))
111114

112-
r = HTTPClient.HTTPC.post("http://plot.ly/clientresp",
115+
r = post("http://plot.ly/clientresp",
113116
merge(default_opts,
114117
{"un" => plotlyaccount.username,
115118
"key" => plotlyaccount.api_key,
@@ -119,10 +122,38 @@ function style(style_opts,meta_opts=Dict())
119122
__parseresponse(r)
120123
end
121124

125+
126+
function getFile(file_id::String, file_owner=None)
127+
global plotlyaccount
128+
129+
user = plotlyaccount.username
130+
apikey = plotlyaccount.api_key
131+
132+
if (file_owner == None)
133+
file_owner = user
134+
end
135+
136+
url = "https://api.plot.ly/v2/files/$file_owner:$file_id/content"
137+
lib_version = string(default_opts["platform"], " ", default_opts["version"])
138+
139+
auth = string("Basic ", base64("$user:$apikey"))
140+
141+
options = RequestOptions(headers=[
142+
("Authorization", auth),
143+
("Plotly-Client-Platform", lib_version)
144+
])
145+
146+
r = get(url, options)
147+
148+
__parseresponse(r)
149+
150+
end
151+
152+
122153
function get_required_params(required,opts)
123154
# Priority given to user-inputted opts, then currentplot
124155
result=Dict()
125-
for p in required
156+
for p in required
126157
global currentplot
127158
if haskey(opts,p)
128159
result[p] = opts[p]
@@ -139,84 +170,86 @@ function __parseresponse(r)
139170
body=JSON.parse(bytestring(r.body))
140171
if r.http_code != 200
141172
error(["r.http_code"])
142-
elseif body["error"] != ""
173+
elseif haskey(body, "error") && body["error"] != ""
143174
error(body["error"])
175+
elseif haskey(body, "detail") && body["detail"] != ""
176+
error(body["detail"])
144177
else
145178
body
146179
end
147180
end
148181

149182
function get_template(format_type::String)
150-
if format_type == "layout"
183+
if format_type == "layout"
151184
return {
152185
"title"=>"Click to enter Plot title",
153186
"xaxis"=>{
154187
"range"=>[-1,6],
155188
"type"=>"-",
156189
"mirror"=>true,
157190
"linecolor"=>"#000",
158-
"linewidth"=>1,
191+
"linewidth"=>1,
159192
"tick0"=>0,
160193
"dtick"=>2,
161194
"ticks"=>"outside",
162195
"ticklen"=>5,
163196
"tickwidth"=>1,
164197
"tickcolor"=>"#000",
165-
"nticks"=>0,
198+
"nticks"=>0,
166199
"showticklabels"=>true,
167200
"tickangle"=>"auto",
168201
"exponentformat"=>"e",
169-
"showexponent"=>"all",
202+
"showexponent"=>"all",
170203
"showgrid"=>true,
171204
"gridcolor"=>"#ddd",
172-
"gridwidth"=>1,
205+
"gridwidth"=>1,
173206
"autorange"=>true,
174-
"autotick"=>true,
207+
"autotick"=>true,
175208
"zeroline"=>true,
176209
"zerolinecolor"=>"#000",
177-
"zerolinewidth"=>1,
210+
"zerolinewidth"=>1,
178211
"title"=>"Click to enter X axis title",
179-
"unit"=>"",
180-
"titlefont"=>{"family"=>"","size"=>0,"color"=>""},
181-
"tickfont"=>{"family"=>"","size"=>0,"color"=>""}},
212+
"unit"=>"",
213+
"titlefont"=>{"family"=>"","size"=>0,"color"=>""},
214+
"tickfont"=>{"family"=>"","size"=>0,"color"=>""}},
182215
"yaxis"=>{
183216
"range"=>[-1,4],
184217
"type"=>"-",
185218
"mirror"=>true,
186219
"linecolor"=>"#000",
187-
"linewidth"=>1,
220+
"linewidth"=>1,
188221
"tick0"=>0,
189222
"dtick"=>1,
190223
"ticks"=>"outside",
191224
"ticklen"=>5,
192225
"tickwidth"=>1,
193226
"tickcolor"=>"#000",
194-
"nticks"=>0,
227+
"nticks"=>0,
195228
"showticklabels"=>true,
196229
"tickangle"=>"auto",
197230
"exponentformat"=>"e",
198-
"showexponent"=>"all",
231+
"showexponent"=>"all",
199232
"showgrid"=>true,
200233
"gridcolor"=>"#ddd",
201-
"gridwidth"=>1,
234+
"gridwidth"=>1,
202235
"autorange"=>true,
203-
"autotick"=>true,
236+
"autotick"=>true,
204237
"zeroline"=>true,
205238
"zerolinecolor"=>"#000",
206-
"zerolinewidth"=>1,
239+
"zerolinewidth"=>1,
207240
"title"=>"Click to enter Y axis title",
208-
"unit"=>"",
209-
"titlefont"=>{"family"=>"","size"=>0,"color"=>""},
210-
"tickfont"=>{"family"=>"","size"=>0,"color"=>""}},
241+
"unit"=>"",
242+
"titlefont"=>{"family"=>"","size"=>0,"color"=>""},
243+
"tickfont"=>{"family"=>"","size"=>0,"color"=>""}},
211244
"legend"=>{
212-
"bgcolor"=>"#fff",
213-
"bordercolor"=>"#000",
214-
"borderwidth"=>1,
215-
"font"=>{"family"=>"","size"=>0,"color"=>""},
245+
"bgcolor"=>"#fff",
246+
"bordercolor"=>"#000",
247+
"borderwidth"=>1,
248+
"font"=>{"family"=>"","size"=>0,"color"=>""},
216249
"traceorder"=>"normal"},
217250
"width"=>700,
218251
"height"=>450,
219-
"autosize"=>"initial",
252+
"autosize"=>"initial",
220253
"margin"=>{"l"=>80,"r"=>80,"t"=>80,"b"=>80,"pad"=>2},
221254
"paper_bgcolor"=>"#fff",
222255
"plot_bgcolor"=>"#fff",

test/tests.jl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,31 @@ Plotly.signin("unittest","tfzz811f5n")
99
(x0,y0) = [1,2,3], [4,5,6]
1010
(x1,y1) = [1,2,3], [2,10,12]
1111

12-
response = Plotly.plot([[x0 y0] [x1 y1]])
12+
response = Plotly.plot([[x0 y0] [x1 y1]], ["filename" => "basic_test_plot"])
1313
@test response["error"] == ""
1414

1515
datastyle = ["line"=>["color"=> "rgb(84, 39, 143)", "width"=> 4]]
16-
style_res = Plotly.style(datastyle)
16+
style_res = Plotly.style(datastyle, ["filename" => "basic_test_plot"])
1717
@test style_res["error"] == ""
1818

19-
layout_res = Plotly.layout(["title"=>"Hello World"])
19+
layout_res = Plotly.layout(["title"=>"Hello World"], ["filename" => "basic_test_plot"])
2020
@test layout_res["error"] == ""
2121

2222
# just check that these won't raise an error
2323
trace1 = Plotly.line(sin, ["left"=>0, "right"=>10, "step"=>1])
2424
trace2 = Plotly.histogram(cos, ["left"=>0, "right"=>10, "step"=>1])
2525
trace3 = Plotly.box(abs, ["left"=>0, "right"=>10, "step"=>1])
2626
trace4 = Plotly.scatter(log, ["left"=>0, "right"=>10, "step"=>1])
27-
response = Plotly.plot([trace1, trace2, trace3, trace4])
27+
response = Plotly.plot([trace1, trace2, trace3, trace4], ["filename" => "test_plot"])
2828
@test response["error"] == ""
2929

3030
# and one last check
31-
response = Plotly.plot([sin, cos, abs, log], ["left"=>eps(), "right"=>10, "step"=>1])
32-
@test response["error"] == ""
31+
response = Plotly.plot([sin, cos, abs, log], ["left"=>eps(), "right"=>10, "step"=>1, "filename" => "test_plot"])
32+
@test response["error"] == ""
33+
34+
figure = Plotly.getFile("5", "chris")
35+
@test length(figure["data"][1]["x"]) == 501
36+
@test haskey(figure["layout"], "xaxis")
37+
38+
response = Plotly.plot(figure["data"], ["layout"=> figure["layout"], "filename" => "test_get_figure_plot"])
39+
@test response["error"] == ""

0 commit comments

Comments
 (0)