@@ -2,10 +2,8 @@ module Plotly
22using HTTPClient. HTTPC
33using JSON
44
5- type PlotlyAccount
6- username:: String
7- api_key:: String
8- end
5+ include (" plot.jl" )
6+ include (" utils.jl" )
97
108type CurrentPlot
119 filename:: String
@@ -30,47 +28,33 @@ default_opts = {
3028" platform" => " Julia" ,
3129" version" => " 0.2" }
3230
33- function signup (username:: String , email:: String )
34- r = post (" http://plot.ly/apimkacct" ,
35- merge (default_opts,
36- {" un" => username,
37- " email" => email}))
38- if r. http_code == 200
39- results = JSON. parse (bytestring (r. body))
40- for flag in [" error" ," warning" ," message" ]
41- if haskey (results, flag) && results[flag] != " "
42- println (results[flag])
43- end
44- end
45- if haskey (results," tmp_pw" )
46- println (" Success! Check your email to activate your account." )
47- results
48- end
49- end
31+ function get_plot_endpoint ()
32+ config = get_config ()
33+ plot_endpoint = " clientresp"
34+ return joinpath (config. plotly_domain, plot_endpoint)
5035end
5136
52- function signin (username:: String , api_key:: String )
53- global plotlyaccount
54- plotlyaccount = PlotlyAccount (username,api_key)
37+ function get_content_endpoint (file_id:: String , owner:: String )
38+ config = get_config ()
39+ content_endpoint = " files/$owner :$file_id /content"
40+ return joinpath (config. plotly_api_domain, content_endpoint)
5541end
5642
5743function plot (data:: Array ,options= Dict ())
58- global plotlyaccount
59- if ! isdefined (Plotly,:plotlyaccount )
60- println (" Please 'signin(username, api_key)' before proceeding. See http://plot.ly/API for help!" )
61- return
62- end
44+ creds = get_credentials ()
45+ endpoint = get_plot_endpoint ()
6346 opt = merge (default_options,options)
64- r = post (" http://plot.ly/clientresp " ,
47+ r = post (endpoint ,
6548 merge (default_opts,
6649 {
67- " un" => plotlyaccount . username,
68- " key" => plotlyaccount . api_key,
50+ " un" => creds . username,
51+ " key" => creds . api_key,
6952 " args" => json (data),
7053 " kwargs" => json (opt)
7154 })
7255 )
7356 body= JSON. parse (bytestring (r. body))
57+
7458 if r. http_code != 200
7559 error ([" r.http_code" ])
7660 elseif body[" error" ] != " "
@@ -82,68 +66,59 @@ function plot(data::Array,options=Dict())
8266 end
8367end
8468
85- include (" plot.jl" )
86-
8769function layout (layout_opts:: Dict ,meta_opts= Dict ())
88- global plotlyaccount
89- if ! isdefined (Plotly,:plotlyaccount )
90- println (" Please 'signin(username, api_key)' before proceeding. See http://plot.ly/API for help!" )
91- return
92- end
70+ creds = get_credentials ()
71+ endpoint = get_plot_endpoint ()
9372
9473 merge! (meta_opts,get_required_params ([" filename" ," fileopt" ],meta_opts))
9574
96- r = post (" http://plot.ly/clientresp " ,
75+ r = post (endpoint ,
9776 merge (default_opts,
98- {" un" => plotlyaccount . username,
99- " key" => plotlyaccount . api_key,
77+ {" un" => creds . username,
78+ " key" => creds . api_key,
10079 " args" => json (layout_opts),
10180 " origin" => " layout" ,
10281 " kwargs" => json (meta_opts)}))
10382 __parseresponse (r)
10483end
10584
10685function style (style_opts,meta_opts= Dict ())
107- global plotlyaccount
108- if ! isdefined (Plotly,:plotlyaccount )
109- println (" Please 'signin(username, api_key)' before proceeding. See http://plot.ly/API for help!" )
110- return
111- end
86+ creds = get_credentials ()
87+ endpoint = get_plot_endpoint ()
11288
11389 merge! (meta_opts,get_required_params ([" filename" ," fileopt" ],meta_opts))
11490
115- r = post (" http://plot.ly/clientresp " ,
91+ r = post (endpoint ,
11692 merge (default_opts,
117- {" un" => plotlyaccount . username,
118- " key" => plotlyaccount . api_key,
93+ {" un" => creds . username,
94+ " key" => creds . api_key,
11995 " args" => json ([style_opts]),
12096 " origin" => " style" ,
12197 " kwargs" => json (meta_opts)}))
12298 __parseresponse (r)
12399end
124100
125101
126- function getFile (file_id:: String , file_owner= None)
127- global plotlyaccount
102+ function getFile (file_id:: String , owner= None)
103+ creds = get_credentials ()
104+ username = creds. username
105+ api_key = creds. api_key
128106
129- user = plotlyaccount. username
130- apikey = plotlyaccount. api_key
131-
132- if (file_owner == None)
133- file_owner = user
107+ if (owner == None)
108+ owner = username
134109 end
135110
136- url = " https://api.plot.ly/v2/files/ $file_owner : $ file_id/content "
111+ endpoint = get_content_endpoint ( file_id, owner)
137112 lib_version = string (default_opts[" platform" ], " " , default_opts[" version" ])
138113
139- auth = string (" Basic " , base64 (" $user : $apikey " ))
114+ auth = string (" Basic " , base64 (" $username : $api_key " ))
140115
141116 options = RequestOptions (headers= [
142117 (" Authorization" , auth),
143118 (" Plotly-Client-Platform" , lib_version)
144119 ])
145120
146- r = get (url , options)
121+ r = get (endpoint , options)
147122
148123 __parseresponse (r)
149124
@@ -266,19 +241,12 @@ function get_template(format_type::String)
266241 end
267242end
268243
269- function help (func_name:: String )
270- print (" hihi" )
271- end
272244function help ()
273245 println (" Please enter the name of the funtion you'd like help with" )
274246 println (" Options include:" )
275247 println (" \t Plotly.help(\" plot\" ) OR Plotly.help(:plot)" )
276248 println (" \t Plotly.help(\" layout\" ) OR Plotly.help(:layout)" )
277249 println (" \t Plotly.help(\" style\" ) OR Plotly.help(:style)" )
278250end
279- function help (func_name:: Symbol )
280- print (" hihi" )
281- end
282-
283251
284252end
0 commit comments