Skip to content

Commit 1aa9ddc

Browse files
committed
move credentials/config dev into utils.jl
1 parent f36b874 commit 1aa9ddc

File tree

2 files changed

+178
-177
lines changed

2 files changed

+178
-177
lines changed

src/Plotly.jl

Lines changed: 1 addition & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module Plotly
22
using HTTPClient.HTTPC
33
using JSON
4-
using Debug
54

65
include("plot.jl")
6+
include("utils.jl")
77

88
type CurrentPlot
99
filename::String
@@ -15,16 +15,6 @@ default_options = {"filename"=>"Plot from Julia API",
1515
"world_readable"=> true,
1616
"layout"=>{""=>""}}
1717

18-
type PlotlyCredentials
19-
username::String
20-
api_key::String
21-
end
22-
23-
type PlotlyConfig
24-
plotly_domain::String
25-
plotly_api_domain::String
26-
end
27-
2818
## Taken from https://github.com/johnmyleswhite/Vega.jl/blob/master/src/Vega.jl#L51
2919
# Open a URL in a browser
3020
function openurl(url::String)
@@ -38,172 +28,6 @@ default_opts = {
3828
"platform" => "Julia",
3929
"version" => "0.2"}
4030

41-
default_endpoints = {
42-
"base" => "https://plot.ly",
43-
"api" => "https://api.plot.ly/v2"}
44-
45-
function signin(username::String, api_key::String, endpoints=None)
46-
# Define session credentials/endpoint configuration, where endpoint is a Dict
47-
48-
if endpoints != None
49-
base_domain = get(endpoints, "plotly_domain", default_endpoints["base"])
50-
api_domain = get(endpoints, "plotly_api_domain", default_endpoints["api"])
51-
global plotlyconfig = PlotlyConfig(base_domain, api_domain)
52-
end
53-
global plotlycredentials = PlotlyCredentials(username, api_key)
54-
end
55-
56-
function get_credentials()
57-
# Return the session credentials if defined --> otherwise use .credentials specs
58-
59-
if !isdefined(Plotly,:plotlycredentials)
60-
creds = get_credentials_file()
61-
try
62-
username = creds["username"]
63-
api_key = creds["api_key"]
64-
global plotlycredentials = PlotlyCredentials(username, api_key)
65-
catch
66-
error("Please 'signin(username, api_key)' before proceeding. See
67-
http://plot.ly/API for help!")
68-
end
69-
end
70-
71-
# will persist for the remainder of the session
72-
return plotlycredentials
73-
end
74-
75-
function get_config()
76-
# Return the session configuration if defined --> otherwise use .config specs
77-
78-
if !isdefined(Plotly,:plotlyconfig)
79-
config = get_config_file()
80-
base_domain = get(config, "plotly_domain", default_endpoints["base"])
81-
api_domain = get(config, "plotly_api_domain", default_endpoints["api"])
82-
global plotlyconfig = PlotlyConfig(base_domain, api_domain)
83-
end
84-
85-
# will persist for the remainder of the session
86-
return plotlyconfig
87-
end
88-
89-
function set_credentials_file(input_creds::Dict)
90-
# Save Plotly endpoint configuration as JSON key-value pairs in
91-
# userhome/.plotly/.credentials. This includes username and api_key.
92-
93-
prev_creds = {}
94-
95-
try
96-
prev_creds = get_credentials_file()
97-
end
98-
99-
# plotly credentials file
100-
userhome = get(ENV, "HOME", "")
101-
plotly_credentials_folder = joinpath(userhome, ".plotly")
102-
plotly_credentials_file = joinpath(plotly_credentials_folder, ".credentials")
103-
104-
#check to see if dir/file exists --> if not create it
105-
try
106-
mkdir(plotly_credentials_folder)
107-
catch err
108-
isa(err, SystemError) || rethrow(err)
109-
end
110-
111-
#merge input creds with prev creds
112-
if prev_creds != {}
113-
creds = merge(prev_creds, input_creds)
114-
else
115-
creds = input_creds
116-
end
117-
118-
#write the json strings to the cred file
119-
creds_file = open(plotly_credentials_file, "w")
120-
write(creds_file, JSON.json(creds))
121-
close(creds_file)
122-
end
123-
124-
function set_config_file(input_config::Dict)
125-
# Save Plotly endpoint configuration as JSON key-value pairs in
126-
# userhome/.plotly/.config. This includes the plotly_domain, and plotly_api_domain.
127-
128-
prev_config = {}
129-
130-
try
131-
prev_config = get_config_file()
132-
end
133-
134-
# plotly configuration file
135-
userhome = get(ENV, "HOME", "")
136-
plotly_config_folder = joinpath(userhome, ".plotly")
137-
plotly_config_file = joinpath(plotly_config_folder, ".config")
138-
139-
#check to see if dir/file exists --> if not create it
140-
try
141-
mkdir(plotly_config_folder)
142-
catch err
143-
isa(err, SystemError) || rethrow(err)
144-
end
145-
146-
#merge input config with prev config
147-
if prev_config != {}
148-
config = merge(prev_config, input_config)
149-
else
150-
config = input_config
151-
end
152-
153-
#write the json strings to the config file
154-
config_file = open(plotly_config_file, "w")
155-
write(config_file, JSON.json(config))
156-
close(config_file)
157-
end
158-
159-
function get_credentials_file()
160-
# Load user credentials as a Dict
161-
162-
# plotly credentials file
163-
userhome = get(ENV, "HOME", "")
164-
plotly_credentials_folder = joinpath(userhome, ".plotly")
165-
plotly_credentials_file = joinpath(plotly_credentials_folder, ".credentials")
166-
167-
if !isfile(plotly_credentials_file)
168-
error(" No credentials file found. Please Set up your credentials
169-
file by running set_credentials_file({\"username\": \"your_plotly_username\", ...
170-
\"api_key\": \"your_plotly_api_key\"})")
171-
end
172-
173-
creds_file = open(plotly_credentials_file)
174-
creds = JSON.parse(creds_file)
175-
176-
if creds == nothing
177-
creds = {}
178-
end
179-
180-
return creds
181-
end
182-
183-
function get_config_file()
184-
# Load endpoint configuration as a Dict
185-
186-
# plotly configuration file
187-
userhome = get(ENV, "HOME", "")
188-
plotly_config_folder = joinpath(userhome, ".plotly")
189-
plotly_config_file = joinpath(plotly_config_folder, ".config")
190-
191-
if !isfile(plotly_config_file)
192-
error(" No configuration file found. Please Set up your configuration
193-
file by running set_config_file({\"plotly_domain\": \"your_plotly_domain\", ...
194-
\"plotly_api_domain\": \"your_plotly_api_domain\"})")
195-
end
196-
197-
config_file = open(plotly_config_file)
198-
config = JSON.parse(config_file)
199-
200-
if config == nothing
201-
config = {}
202-
end
203-
204-
return config
205-
end
206-
20731
function get_plot_endpoint()
20832
config = get_config()
20933
plot_endpoint = "clientresp"

src/utils.jl

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

0 commit comments

Comments
 (0)