Skip to content

Commit a19bea2

Browse files
author
Kevin Hannegan
committed
add get_client
1 parent eac3107 commit a19bea2

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

presalytics/client/api.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import json
1010
import environs
1111
import wsgi_microservice_middleware
12+
import functools
1213
import presalytics
1314
import presalytics.lib.exceptions
1415
import presalytics.lib.constants as cnst
@@ -43,11 +44,11 @@ class Client(object):
4344
4445
*A note for server-side development*:
4546
46-
By default, the client class automatically caches tokens in a file called
47+
The client class can automatically cache tokens in a file called
4748
"token.json", located in the python's current working directory. This is done so
48-
users running scripts do not have to acquire a new token every time an API call is
49-
made. If building a client to operate in a multi-user environment, this behavior
50-
should be turned off so that user do not pull one another's tokens.
49+
users running scripts accross multiple client instances do not have to acquire a new token
50+
every time an API call is made. If building a client to operate in a multi-user environment,
51+
this behavior should be turned off so that one user cannot not pull one another's tokens.
5152
To do this, ensure the following parameters are pass to the configuration either
5253
via initialization or in a `presalytics.CONFIG` file:
5354
@@ -454,6 +455,15 @@ def __init__(self, parent: Client, **kwargs):
454455
self.update_configuration()
455456

456457

458+
@functools.lru_cache(maxsize=None)
459+
def get_client():
460+
"""
461+
Caches a client instance for default parameters set in `presalytics.CONFIG`.
462+
463+
DO NOT use in server-side operation
464+
"""
465+
client = presalytics.Client()
466+
return client
457467

458468

459469

test/__init__.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +0,0 @@
1-
from functools import lru_cache
2-
import presalytics
3-
4-
@lru_cache(maxsize=None)
5-
def get_test_client():
6-
client = presalytics.Client()
7-
return client

test/test_components.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import presalytics
77
import io
88
import lxml
9-
from test import get_test_client
9+
from presalytics.client.api import get_client
1010
if typing.TYPE_CHECKING:
1111
from presalytics.client.presalytics_story import Story, OoxmlDocument
1212
from presalytics.client.presalytics_ooxml_automation.models import ChartChartDataDTO, TableTableDataDTO
@@ -29,11 +29,11 @@ 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-
client_info = get_test_client().get_client_info()
32+
client_info = get_client().get_client_info()
3333
story = presalytics.create_story_from_ooxml_file(tmp_filename, client_info=client_info)
3434
outline = presalytics.StoryOutline.load(story.outline)
3535
old_widget = outline.pages[0].widgets[0]
36-
childs = get_test_client().ooxml_automation.documents_childobjects_get_id(old_widget.data["document_ooxml_id"])
36+
childs = get_client().ooxml_automation.documents_childobjects_get_id(old_widget.data["document_ooxml_id"])
3737
endpoint_map = presalytics.OoxmlEndpointMap.shape()
3838
object_type = endpoint_map.get_object_type()
3939
info = next(x for x in childs if x.object_type == object_type)
@@ -66,7 +66,7 @@ def test_xml_widget(self):
6666

6767
presalytics.COMPONENTS.register(widget)
6868
story.outline = outline.dump()
69-
get_test_client().story.story_id_put(story.id, story)
69+
get_client().story.story_id_put(story.id, story)
7070
html = presalytics.Revealer(outline).package_as_standalone().decode('utf-8')
7171
self.assertIsInstance(html, str)
7272
os.remove(tmp_filename)
@@ -88,7 +88,7 @@ def test_bytesio_upload(self):
8888
with open(test_file, 'rb') as f:
8989
_bytes = io.BytesIO(f.read())
9090

91-
story = presalytics.story_post_file_bytes(get_test_client(), _bytes, "testfile.pptx")
91+
story = presalytics.story_post_file_bytes(get_client(), _bytes, "testfile.pptx")
9292
from presalytics.client.presalytics_story.models.story import Story
9393
self.assertIsInstance(story, Story)
9494

@@ -121,10 +121,10 @@ def test_chart_update(self):
121121
document: "OoxmlDocument"
122122

123123
test_file = os.path.join(os.path.dirname(__file__), "files", "bubblechart.pptx")
124-
story = get_test_client().story.story_post_file(file=[test_file])
125-
story_detailed = get_test_client().story.story_id_get(story.id, include_relationships=True)
124+
story = get_client().story.story_post_file(file=[test_file])
125+
story_detailed = get_client().story.story_id_get(story.id, include_relationships=True)
126126
document = story_detailed.ooxml_documents[0]
127-
object_tree = get_test_client().ooxml_automation.documents_childobjects_get_id(document.ooxml_automation_id)
127+
object_tree = get_client().ooxml_automation.documents_childobjects_get_id(document.ooxml_automation_id)
128128
endpoint_map = presalytics.OoxmlEndpointMap.chart()
129129
object_type = endpoint_map.get_object_type()
130130
chart_id = next(entity.entity_id for entity in object_tree if entity.object_type == object_type)
@@ -167,10 +167,10 @@ def test_table_update(self):
167167
dto: "TableTableDataDTO"
168168

169169
test_file = os.path.join(os.path.dirname(__file__), "files", "table.pptx")
170-
story = get_test_client().story.story_post_file(file=[test_file])
171-
story_detailed = get_test_client().story.story_id_get(story.id, include_relationships=True)
170+
story = get_client().story.story_post_file(file=[test_file])
171+
story_detailed = get_client().story.story_id_get(story.id, include_relationships=True)
172172
document = story_detailed.ooxml_documents[0]
173-
object_tree = get_test_client().ooxml_automation.documents_childobjects_get_id(document.ooxml_automation_id)
173+
object_tree = get_client().ooxml_automation.documents_childobjects_get_id(document.ooxml_automation_id)
174174
endpoint_map = presalytics.OoxmlEndpointMap.table()
175175
object_type = endpoint_map.get_object_type()
176176
table_id = next(entity.entity_id for entity in object_tree if entity.object_type == object_type)

0 commit comments

Comments
 (0)