11# __precompile__(true)
22
33module Plotly
4+ using Compat
45using Requests
56using JSON
67using Reexport: @reexport
8+ import Requests: URI, post
79
810@reexport using PlotlyJS
11+ export post
912
1013include (" utils.jl" )
1114
12- # export default_kwargs, default_opts, get_config, get_plot_endpoint, get_credentials,get_content_endpoint,get_template
13-
14- type CurrentPlot
15- filename:: ASCIIString
16- fileopt:: ASCIIString
17- url:: ASCIIString
18- end
19-
2015const api_version = " v2"
2116
2217const default_kwargs = Dict {Symbol,Any} (:filename => " Plot from Julia API" ,
23- :world_readable => true )
18+ :world_readable => true )
19+
20+ const default_opts = Dict {Symbol,Any} (:origin => " plot" ,
21+ :platform => " Julia" ,
22+ :version => " 0.2" )
2423
2524# # Taken from https://github.com/johnmyleswhite/Vega.jl/blob/master/src/Vega.jl#L51
2625# Open a URL in a browser
@@ -30,103 +29,92 @@ function openurl(url::ASCIIString)
3029 @linux_only run (` xdg-open $url ` )
3130end
3231
33- const default_opts = Dict {Symbol,Any} (:origin => " plot" ,
34- :platform => " Julia" ,
35- :version => " 0.2" )
32+ openurl (url:: URI ) = openurl (string (url))
3633
3734get_plot_endpoint () = " $(get_config (). plotly_domain) /clientresp"
3835
39- function get_content_endpoint (file_id:: ASCIIString , owner:: ASCIIString )
40- config = get_config ()
41- api_endpoint = " $(config. plotly_api_domain) /$api_version /files"
42- detail = " $owner :$file_id "
43- " $api_endpoint /$detail /content"
36+
37+ immutable RemotePlot
38+ url:: URI
4439end
40+ RemotePlot (url) = RemotePlot (URI (url))
41+
42+ Base. open (p:: RemotePlot ) = openurl (p. url)
4543
46- function Requests . post (p:: Plot ; kwargs... )
44+ function post (p:: Plot ; kwargs... )
4745 creds = get_credentials ()
4846 endpoint = get_plot_endpoint ()
4947 opt = merge (default_kwargs, Dict (:layout => p. layout. fields),
50- Dict (kwargs))
48+ Dict (kwargs))
5149
5250 data = merge (default_opts,
53- Dict (" un" => creds. username,
54- " key" => creds. api_key,
55- " args" => json (p. data),
56- " kwargs" => json (opt)))
51+ Dict (" un" => creds. username,
52+ " key" => creds. api_key,
53+ " args" => json (p. data),
54+ " kwargs" => json (opt)))
5755
5856 r = post (endpoint, data= data)
59- body= Requests. json (r)
60-
61- if statuscode (r) != 200
62- error ([" r.status" ])
63- elseif body[" error" ] != " "
64- error (body[" error" ])
65- else
66- global currentplot
67- currentplot= CurrentPlot (body[" filename" ], " new" , body[" url" ])
68- body
69- end
57+ # body=Requests.json(r)
58+ body = parse_response (r)
59+ return RemotePlot (URI (body[" url" ]))
7060end
7161
7262function Requests. post (l:: AbstractLayout , meta_opts= Dict (); meta_kwargs... )
7363 creds = get_credentials ()
7464 endpoint = get_plot_endpoint ()
7565
7666 meta = merge (meta_opts,
77- get_required_params ([" filename" , " fileopt" ], meta_opts),
78- Dict (meta_kwargs))
67+ get_required_params ([" filename" , " fileopt" ], meta_opts),
68+ Dict (meta_kwargs))
7969 data = merge (default_opts,
80- Dict (" un" => creds. username,
81- " key" => creds. api_key,
82- " args" => json (l),
83- " origin" => " layout" ,
84- " kwargs" => json (meta)))
70+ Dict (" un" => creds. username,
71+ " key" => creds. api_key,
72+ " args" => json (l),
73+ " origin" => " layout" ,
74+ " kwargs" => json (meta)))
8575
86- __parseresponse (post (endpoint, data= data))
76+ parse_response (post (endpoint, data= data))
8777end
8878
79+ post (p:: PlotlyJS.SyncPlot ) = post (p. plot)
80+
8981function style (style_opts, meta_opts= Dict (); meta_kwargs... )
9082 creds = get_credentials ()
9183 endpoint = get_plot_endpoint ()
9284
9385 meta = merge (meta_opts,
94- get_required_params ([" filename" , " fileopt" ], meta_opts),
95- Dict (meta_kwargs))
86+ get_required_params ([" filename" , " fileopt" ], meta_opts),
87+ Dict (meta_kwargs))
9688 data = merge (default_opts,
97- Dict (" un" => creds. username,
98- " key" => creds. api_key,
99- " args" => json ([style_opts]),
100- " origin" => " style" ,
101- " kwargs" => json (meta_opts)))
89+ Dict (" un" => creds. username,
90+ " key" => creds. api_key,
91+ " args" => json ([style_opts]),
92+ " origin" => " style" ,
93+ " kwargs" => json (meta_opts)))
10294
103- __parseresponse (post (endpoint, data= data))
95+ parse_response (post (endpoint, data= data))
10496end
10597
10698
107- function getFile (file_id :: ASCIIString , owner = None )
99+ function Base . download (plot :: RemotePlot )
108100 creds = get_credentials ()
109101 username = creds. username
110102 api_key = creds. api_key
111-
112- if (owner == None)
113- owner = username
114- end
115-
116- endpoint = get_content_endpoint (file_id, owner)
117- lib_version = string (default_opts[" platform" ], " " , default_opts[" version" ])
118-
119- auth = string (" Basic " , base64 (" $username :$api_key " ))
120-
103+ lib_version = string (default_opts[:platform ], " " , default_opts[:version ])
104+ auth = string (" Basic " , base64encode (" $username :$api_key " ))
121105 options = Dict (" Authorization" => auth, " Plotly-Client-Platform" => lib_version)
122-
123- r = get (endpoint, headers= options)
124- print (r)
125-
126- __parseresponse (r)
127-
106+ original_path = plot. url. path
107+ if original_path[end ] == ' /'
108+ path = original_path[1 : end - 1 ]
109+ else
110+ path = original_path
111+ end
112+ endpoint = URI (plot. url, path= " $path .json" )
113+ response = get (endpoint, headers= options)
114+ return JSON. parse (Plot, bytestring (response))
128115end
129116
117+ download_plot (url) = download (RemotePlot (url))
130118
131119function get_required_params (required,opts)
132120 # Priority given to user-inputted opts, then currentplot
@@ -139,22 +127,30 @@ function get_required_params(required,opts)
139127 result[p] = getfield (currentplot,symbol (p))
140128 else
141129 msg = string (" Missing required param $(p) . " ,
142- " Make sure to create a plot first. " ,
143- " Please refer to http://plot.ly/api" )
130+ " Make sure to create a plot first. " ,
131+ " Please refer to http://plot.ly/api" )
144132 error (msg)
145133 end
146134 end
147135 result
148136end
149137
150- function __parseresponse (r)
151- body= Requests. json (r)
152- if statuscode (r) != 200
153- error ([" r.status" ])
154- elseif haskey (body, " error" ) && body[" error" ] != " "
155- error (body[" error" ])
156- elseif haskey (body, " detail" ) && body[" detail" ] != " "
157- error (body[" detail" ])
138+ immutable PlotlyError <: Exception
139+ msg:: UTF8String
140+ end
141+
142+ function Base. show (io:: IO , err:: PlotlyError )
143+ print (io, " Plotly error: $(err. msg) " )
144+ end
145+
146+ function parse_response (r)
147+ body = Requests. json (r)
148+ if statuscode (r) ≠ 200
149+ throw (PlotlyError (" Non-sucessful status code: $(statuscode (r)) " ))
150+ elseif " error" ∈ keys (body) && body[" error" ] ≠ " "
151+ throw (PlotlyError (body[" error" ]))
152+ elseif " detail" ∈ keys (body) && body[" detail" ] ≠ " "
153+ throw (PlotlyError (body[" detail" ]))
158154 else
159155 body
160156 end
0 commit comments