Skip to content

Commit e927666

Browse files
committed
Merge pull request #9 from plotly/cred_config_dev
Cred config dev
2 parents a1f9f54 + d4c8bcd commit e927666

File tree

6 files changed

+277
-68
lines changed

6 files changed

+277
-68
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@ julia> Plotly.signin("username","your api key")
3131
PlotlyAccount("username","your api key")
3232
```
3333

34+
Note: you may also specify your session endpoints using signin as follows:
35+
36+
```julia
37+
julia> Plotly.signin("username","your api key",{"plotly_domain"=> "your_plotly_base_endpoint", "plotly_domain"=> "your_plotly_api_endpoint"})
38+
```
39+
40+
## Saving your credentials
41+
```julia
42+
julia> Plotly.set_credentials_file({"username"=>"your_user_name","api_key"=>"your_api_key"})
43+
```
44+
45+
Note: your credentials will be saved within /YOUR_HOME_DIR/.plotly/.credentials
46+
47+
## Saving your endpoint configuration
48+
```julia
49+
julia> Plotly.set_config_file({"plotly_domain"=> "your_plotly_base_endpoint", "plotly_domain"=> "your_plotly_api_endpoint"})
50+
```
51+
52+
Note: your configuration will be saved within /YOUR_HOME_DIR/.plotly/.config
53+
3454
## Plot && Open in browser
3555
```julia
3656
julia> Plotly.openurl(Plotly.plot(["z"=>rand(6,6)],["style"=>["type"=>"heatmap"]]))

src/Plotly.jl

Lines changed: 35 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ module Plotly
22
using HTTPClient.HTTPC
33
using JSON
44

5-
type PlotlyAccount
6-
username::String
7-
api_key::String
8-
end
5+
include("plot.jl")
6+
include("utils.jl")
97

108
type 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)
5035
end
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)
5541
end
5642

5743
function 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
8367
end
8468

85-
include("plot.jl")
86-
8769
function 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)
10483
end
10584

10685
function 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)
12399
end
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
267242
end
268243

269-
function help(func_name::String)
270-
print("hihi")
271-
end
272244
function 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)")
278250
end
279-
function help(func_name::Symbol)
280-
print("hihi")
281-
end
282-
283251

284252
end

src/utils.jl

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
using JSON
2+
3+
default_endpoints = {
4+
"base" => "https://plot.ly",
5+
"api" => "https://api.plot.ly/v2"}
6+
7+
type PlotlyCredentials
8+
username::String
9+
api_key::String
10+
end
11+
12+
type PlotlyConfig
13+
plotly_domain::String
14+
plotly_api_domain::String
15+
end
16+
17+
function signin(username::String, api_key::String, endpoints=None)
18+
# Define session credentials/endpoint configuration, where endpoint is a Dict
19+
20+
global plotlycredentials = PlotlyCredentials(username, api_key)
21+
22+
# if endpoints are specified both the base and api domains must be specified
23+
if endpoints != None
24+
try
25+
base_domain = endpoints["plotly_domain"]
26+
api_domain = endpoints["plotly_api_domain"]
27+
global plotlyconfig = PlotlyConfig(base_domain, api_domain)
28+
catch
29+
error("You must specify both the base and api endpoints.")
30+
end
31+
end
32+
end
33+
34+
function get_credentials()
35+
# Return the session credentials if defined --> otherwise use .credentials specs
36+
37+
if !isdefined(Plotly,:plotlycredentials)
38+
39+
creds = get_credentials_file()
40+
41+
try
42+
username = creds["username"]
43+
api_key = creds["api_key"]
44+
45+
global plotlycredentials = PlotlyCredentials(username, api_key)
46+
47+
catch
48+
error("Please 'signin(username, api_key)' before proceeding. See
49+
http://plot.ly/API for help!")
50+
end
51+
end
52+
53+
# will persist for the remainder of the session
54+
return plotlycredentials
55+
end
56+
57+
function get_config()
58+
# Return the session configuration if defined --> otherwise use .config specs
59+
60+
if !isdefined(Plotly,:plotlyconfig)
61+
62+
config = get_config_file()
63+
64+
if isempty(config)
65+
base_domain = default_endpoints["base"]
66+
api_domain = default_endpoints["api"]
67+
else
68+
base_domain = get(config, "plotly_domain", default_endpoints["base"])
69+
api_domain = get(config, "plotly_api_domain", default_endpoints["api"])
70+
end
71+
72+
global plotlyconfig = PlotlyConfig(base_domain, api_domain)
73+
74+
end
75+
76+
# will persist for the remainder of the session
77+
return plotlyconfig
78+
end
79+
80+
function set_credentials_file(input_creds::Dict)
81+
# Save Plotly endpoint configuration as JSON key-value pairs in
82+
# userhome/.plotly/.credentials. This includes username and api_key.
83+
84+
# plotly credentials file
85+
userhome = homedir()
86+
plotly_credentials_folder = joinpath(userhome, ".plotly")
87+
plotly_credentials_file = joinpath(plotly_credentials_folder, ".credentials")
88+
89+
#check to see if dir/file exists --> if not, create it
90+
try
91+
mkdir(plotly_credentials_folder)
92+
catch err
93+
isa(err, SystemError) || rethrow(err)
94+
end
95+
96+
prev_creds = get_credentials_file()
97+
98+
#merge input creds with prev creds
99+
if !isempty(prev_creds)
100+
creds = merge(prev_creds, input_creds)
101+
else
102+
creds = input_creds
103+
end
104+
105+
#write the json strings to the cred file
106+
creds_file = open(plotly_credentials_file, "w")
107+
write(creds_file, JSON.json(creds))
108+
close(creds_file)
109+
end
110+
111+
function set_config_file(input_config::Dict)
112+
# Save Plotly endpoint configuration as JSON key-value pairs in
113+
# userhome/.plotly/.config. This includes the plotly_domain, and plotly_api_domain.
114+
115+
# plotly configuration file
116+
userhome = homedir()
117+
plotly_config_folder = joinpath(userhome, ".plotly")
118+
plotly_config_file = joinpath(plotly_config_folder, ".config")
119+
120+
#check to see if dir/file exists --> if not create it
121+
try
122+
mkdir(plotly_config_folder)
123+
catch err
124+
isa(err, SystemError) || rethrow(err)
125+
end
126+
127+
prev_config = get_config_file()
128+
129+
#merge input config with prev config
130+
if !isempty(prev_config)
131+
config = merge(prev_config, input_config)
132+
else
133+
config = input_config
134+
end
135+
136+
#write the json strings to the config file
137+
config_file = open(plotly_config_file, "w")
138+
write(config_file, JSON.json(config))
139+
close(config_file)
140+
end
141+
142+
function get_credentials_file()
143+
# Load user credentials as a Dict
144+
145+
# plotly credentials file
146+
userhome = homedir()
147+
plotly_credentials_folder = joinpath(userhome, ".plotly")
148+
plotly_credentials_file = joinpath(plotly_credentials_folder, ".credentials")
149+
150+
if !isfile(plotly_credentials_file)
151+
creds = {}
152+
else
153+
creds_file = open(plotly_credentials_file)
154+
creds = JSON.parse(creds_file)
155+
156+
if creds == nothing
157+
creds = {}
158+
end
159+
160+
end
161+
162+
return creds
163+
164+
end
165+
166+
function get_config_file()
167+
# Load endpoint configuration as a Dict
168+
169+
# plotly configuration file
170+
userhome = homedir()
171+
plotly_config_folder = joinpath(userhome, ".plotly")
172+
plotly_config_file = joinpath(plotly_config_folder, ".config")
173+
174+
if !isfile(plotly_config_file)
175+
config = {}
176+
else
177+
config_file = open(plotly_config_file)
178+
config = JSON.parse(config_file)
179+
180+
if config == nothing
181+
config = {}
182+
end
183+
184+
end
185+
186+
return config
187+
end

0 commit comments

Comments
 (0)