4
4
"""
5
5
from __future__ import absolute_import
6
6
7
+ import hashlib
7
8
import json
8
9
import requests
9
10
10
11
from plotly import exceptions , files , utils
11
12
12
- GRAPH_REFERENCE_PATH = '/plot-schema.json'
13
+ GRAPH_REFERENCE_PATH = '/v2/plot-schema'
14
+
13
15
14
16
def get_graph_reference ():
15
17
"""
@@ -19,36 +21,39 @@ def get_graph_reference():
19
21
:raises: (PlotlyError) When graph reference DNE and GET request fails.
20
22
21
23
"""
24
+ default_config = files .FILE_CONTENT [files .CONFIG_FILE ]
22
25
if files .check_file_permissions ():
23
26
graph_reference = utils .load_json_dict (files .GRAPH_REFERENCE_FILE )
24
27
config = utils .load_json_dict (files .CONFIG_FILE )
25
28
26
29
# 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' ] )
29
32
else :
30
33
graph_reference = {}
31
- plotly_domain = files . FILE_CONTENT [ files . CONFIG_FILE ][ 'plotly_domain ' ]
34
+ plotly_api_domain = default_config [ 'plotly_api_domain ' ]
32
35
33
- if not graph_reference :
36
+ sha1 = hashlib . sha1 ( str ( graph_reference )). hexdigest ()
34
37
35
- graph_reference_url = plotly_domain + GRAPH_REFERENCE_PATH
38
+ graph_reference_url = '{}{}?sha1={}' .format (plotly_api_domain ,
39
+ GRAPH_REFERENCE_PATH , sha1 )
36
40
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 :
41
46
raise exceptions .PlotlyError (
42
47
"The schema used to validate Plotly figures has never been "
43
48
"downloaded to your computer. You'll need to connect to a "
44
49
"Plotly server at least once to do this.\n "
45
50
"You're seeing this error because the attempt to download "
46
51
"the schema from '{}' failed." .format (graph_reference_url )
47
52
)
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' ]
52
57
53
58
return utils .decode_unicode (graph_reference )
54
59
0 commit comments