Skip to content

Commit 09f8a5e

Browse files
committed
Now with *real* cacheing!
* no need to reset the graph reference file * send a hash along with the request to check for an update * if the request fails *and* there’s no local reference —> error.
1 parent 52a51d8 commit 09f8a5e

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

plotly/graph_reference.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
"""
55
from __future__ import absolute_import
66

7+
import hashlib
78
import json
89
import requests
910

1011
from plotly import exceptions, files, utils
1112

12-
GRAPH_REFERENCE_PATH = '/plot-schema.json'
13+
GRAPH_REFERENCE_PATH = '/v2/plot-schema'
14+
1315

1416
def get_graph_reference():
1517
"""
@@ -19,36 +21,39 @@ def get_graph_reference():
1921
:raises: (PlotlyError) When graph reference DNE and GET request fails.
2022
2123
"""
24+
default_config = files.FILE_CONTENT[files.CONFIG_FILE]
2225
if files.check_file_permissions():
2326
graph_reference = utils.load_json_dict(files.GRAPH_REFERENCE_FILE)
2427
config = utils.load_json_dict(files.CONFIG_FILE)
2528

2629
# TODO: https://github.com/plotly/python-api/issues/293
27-
default_domain = files.FILE_CONTENT[files.CONFIG_FILE]['plotly_domain']
28-
plotly_domain = config.get('plotly_domain', default_domain)
30+
plotly_api_domain = config.get('plotly_api_domain',
31+
default_config['plotly_api_domain'])
2932
else:
3033
graph_reference = {}
31-
plotly_domain = files.FILE_CONTENT[files.CONFIG_FILE]['plotly_domain']
34+
plotly_api_domain = default_config['plotly_api_domain']
3235

33-
if not graph_reference:
36+
sha1 = hashlib.sha1(str(graph_reference)).hexdigest()
3437

35-
graph_reference_url = plotly_domain + GRAPH_REFERENCE_PATH
38+
graph_reference_url = '{}{}?sha1={}'.format(plotly_api_domain,
39+
GRAPH_REFERENCE_PATH, sha1)
3640

37-
try:
38-
response = requests.get(graph_reference_url)
39-
response.raise_for_status()
40-
except requests.exceptions.RequestException:
41+
try:
42+
response = requests.get(graph_reference_url)
43+
response.raise_for_status()
44+
except requests.exceptions.RequestException:
45+
if not graph_reference:
4146
raise exceptions.PlotlyError(
4247
"The schema used to validate Plotly figures has never been "
4348
"downloaded to your computer. You'll need to connect to a "
4449
"Plotly server at least once to do this.\n"
4550
"You're seeing this error because the attempt to download "
4651
"the schema from '{}' failed.".format(graph_reference_url)
4752
)
48-
49-
# TODO: Hash this so we don't have to load it every time. The server
50-
# will need to accept a hash query param as well.
51-
graph_reference = json.loads(response.content)
53+
else:
54+
data = json.loads(response.content)
55+
if data['modified']:
56+
graph_reference = data['schema']
5257

5358
return utils.decode_unicode(graph_reference)
5459

plotly/tools.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,9 @@ def ensure_local_plotly_files():
8282

8383
# make a request to get graph reference if DNE.
8484
utils.ensure_file_exists(GRAPH_REFERENCE_FILE)
85-
graph_reference_dict = utils.load_json_dict(GRAPH_REFERENCE_FILE)
85+
utils.save_json_dict(GRAPH_REFERENCE_FILE,
86+
graph_reference.GRAPH_REFERENCE)
8687

87-
if not graph_reference_dict:
88-
utils.save_json_dict(GRAPH_REFERENCE_FILE,
89-
graph_reference.GRAPH_REFERENCE)
9088
else:
9189
warnings.warn("Looks like you don't have 'read-write' permission to "
9290
"your 'home' ('~') directory or to our '~/.plotly' "
@@ -230,14 +228,6 @@ def reset_config_file():
230228
ensure_local_plotly_files() # put the defaults back
231229

232230

233-
### graph reference tools ###
234-
235-
def reset_graph_reference_file():
236-
"""Temporary until we can use local hash in request for graph reference."""
237-
utils.ensure_file_exists(GRAPH_REFERENCE_FILE)
238-
utils.save_json_dict(GRAPH_REFERENCE_FILE, {})
239-
240-
241231
### embed tools ###
242232

243233
def get_embed(file_owner_or_url, file_id=None, width="100%", height=525):

0 commit comments

Comments
 (0)