Skip to content

Commit eac3107

Browse files
author
Kevin Hannegan
committed
add more robust token deserialization
1 parent f4ac9c7 commit eac3107

File tree

6 files changed

+38
-5
lines changed

6 files changed

+38
-5
lines changed

presalytics/client/api.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,15 @@ def __init__(
275275
cache_tokens = presalytics.CONFIG.get("CACHE_TOKENS")
276276
self.token_util = presalytics.client.auth.TokenUtil(token_cache=cache_tokens)
277277
if token:
278+
# Assume if token is passed as string, then it's an access token
279+
if isinstance(token, str):
280+
self.token_util.token = {"access_token": token}
281+
282+
# if token is a dictionary with an 'access_token_expire_time' key, it's previous been processed / deserialized
278283
if token.get('access_token_expire_time', None):
279284
self.token_util.token = token
285+
286+
# if token has an 'expires_in' key, if has not been deserialized
280287
elif token.get('expires_in', None):
281288
self.token_util.process_token(token)
282289
else:
@@ -395,6 +402,26 @@ def download_file(self, story_id, ooxml_automation_id, download_folder=None, fil
395402
with open(filepath, 'wb') as f:
396403
f.write(response.data)
397404

405+
def get_client_info(self):
406+
"""
407+
Convenience method returning information about this client to pass to downstream objects, e.g.,
408+
components and new client instances
409+
410+
Returns
411+
----------
412+
A dictionary containing instances values:
413+
- token: self.token_util.token
414+
- client_id: self.client_id
415+
- cache_tokens: self.token_util.token_cache
416+
- delegate_login: self.delegate login
417+
"""
418+
return {
419+
"token": self.token_util.token,
420+
"client_id": self.client_id,
421+
"cache_tokens": self.token_util.token_cache,
422+
"delegate_login": self._delegate_login
423+
}
424+
398425
class DocConverterApiClientWithAuth(presalytics.client.auth.AuthenticationMixIn, presalytics.client.presalytics_doc_converter.api_client.ApiClient):
399426
"""
400427
Wraps `presalytics.client.presalytics_doc_converter.api_client.ApiClient` with

presalytics/lib/templates/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class JinjaTemplateBuilder(presalytics.story.components.PageTemplateBase):
5555
multiple widgets on a page, the following pattern can be used inside of templates to
5656
increment through the widgets as the jinja2 rendering engine moves through the template:
5757
58-
{{ widgets[widget_index.next()].to_html().decode('utf-8') }} // renders widget and increments widget_index
58+
{{ widgets[widget_index.next()].to_html() }} // renders widget and increments widget_index
5959
6060
Please also note that if `<script>` tags are included in the template, they will be
6161
stripped out downstream by a `presalytics.story.components.Renderer` for security

presalytics/lib/templates/html/title_with_single_widget.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div class="flex-row">
55
<div class="flex-column">
66
<div class="item-container">
7-
{{ widgets[widget_index.next()].to_html().decode('utf-8') }}
7+
{{ widgets[widget_index.next()].to_html() }}
88
</div>
99
</div>
1010
</div>

presalytics/lib/widgets/matplotlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def create_container(self, **kwargs):
189189
'style': 'max-height: none; max-width: none;'
190190
})
191191
empty_parent_div.append(frame)
192-
return lxml.html.tostring(empty_parent_div)
192+
return lxml.html.tostring(empty_parent_div).decode('utf-8')
193193

194194

195195
@classmethod

presalytics/lib/widgets/ooxml.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,12 @@ def to_html(self, **kwargs):
328328
"""
329329
Returns an html string that will render the object at the endpoint
330330
"""
331-
self.svg_html = self.create_container()
331+
html = self.create_container()
332+
try:
333+
html = html.decode('utf-8')
334+
except Exception:
335+
pass
336+
self.svg_html = html
332337
return self.svg_html
333338

334339
def get_svg(self, id, timeout_iterator=0) -> str:

test/test_components.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def test_xml_widget(self):
2929
test_file = os.path.join(os.path.dirname(__file__), "files", "star.pptx")
3030
tmp_filename = os.path.join(os.path.dirname(__file__), os.path.basename(test_file))
3131
shutil.copyfile(test_file, tmp_filename)
32-
story = presalytics.create_story_from_ooxml_file(tmp_filename)
32+
client_info = get_test_client().get_client_info()
33+
story = presalytics.create_story_from_ooxml_file(tmp_filename, client_info=client_info)
3334
outline = presalytics.StoryOutline.load(story.outline)
3435
old_widget = outline.pages[0].widgets[0]
3536
childs = get_test_client().ooxml_automation.documents_childobjects_get_id(old_widget.data["document_ooxml_id"])

0 commit comments

Comments
 (0)