Skip to content

Commit 2d5ca6e

Browse files
Use existing client in List, Find, etc.
Instead of calling `default_client()` for each of those class methods, use a new method, `get_existing_client`, which gets if a user has setup a new api client with `configure`. Solves #13.
1 parent 4d87c43 commit 2d5ca6e

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
dist/
33
build/
44
cloudconvert.egg-info/
5-
*.egg
5+
*.egg
6+
**/__pycache__

cloudconvert/cloudconvertrestclient.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ def default_client():
245245

246246
return __client__
247247

248+
def get_existing_client():
249+
"""Gets an already created client if there is one, None otherwise."""
250+
global __client__
251+
return __client__
252+
248253

249254
def set_config(options=None, **config):
250255
"""Create new default api object with given configuration

cloudconvert/resource.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import uuid
22
import urllib
33
import cloudconvert.utils as util
4-
from cloudconvert.cloudconvertrestclient import default_client
4+
from cloudconvert.cloudconvertrestclient import default_client, get_existing_client
55

66

77
class Resource(object):
@@ -114,7 +114,7 @@ def find(cls, id):
114114
Usage::
115115
>>> job = Job.find("s9fsf9-s9f9sf9s-ggfgf9-fg9fg")
116116
"""
117-
api_client = default_client()
117+
api_client = get_existing_client() or default_client()
118118

119119
url = util.join_url(cls.path, str(id))
120120
res = api_client.get(url)
@@ -134,7 +134,7 @@ def all(cls, params=None):
134134
Usage::
135135
>>> tasks_list = tasks.all({'status': 'waiting'})
136136
"""
137-
api_client = default_client()
137+
api_client = get_existing_client() or default_client()
138138

139139
if params is None:
140140
url = cls.path
@@ -165,7 +165,7 @@ def create(cls, operation=None, payload={}):
165165
>>> task.create(name=TASK_NAME) # return newly created task
166166
"""
167167

168-
api_client = default_client()
168+
api_client = get_existing_client() or default_client()
169169
url = util.join_url('v2', operation or '')
170170
res = api_client.post(url, payload, headers={})
171171

@@ -182,7 +182,7 @@ def wait(cls, id):
182182
Usage::
183183
>>> job = job.wait("s9fsf9-s9f9sf9s-ggfgf9-fg9fg")
184184
"""
185-
api_client = default_client()
185+
api_client = get_existing_client() or default_client()
186186

187187
url = util.join_url(cls.path, str(id))
188188
res = api_client.get_sync(url)
@@ -199,7 +199,7 @@ def show(cls, id):
199199
Usage::
200200
>>> job = Job.show("s9fsf9-s9f9sf9s-ggfgf9-fg9fg")
201201
"""
202-
api_client = default_client()
202+
api_client = get_existing_client() or default_client()
203203
url = util.join_url(cls.path, str(id))
204204
res = api_client.get(url)
205205
try:
@@ -215,7 +215,7 @@ def delete(cls, id):
215215
Usage::
216216
>>> Task.delete(TASK_ID)
217217
"""
218-
api_client = default_client()
218+
api_client = get_existing_client() or default_client()
219219
url = util.join_url(cls.path, str(id))
220220
api_resource = Resource()
221221
new_attributes = api_client.delete(url)

0 commit comments

Comments
 (0)