Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit bf93fbd

Browse files
BartoszCkikossak
authored andcommitted
DRY some code and code style improvement - property moved up, below __init__
1 parent b6371f9 commit bf93fbd

File tree

2 files changed

+19
-33
lines changed

2 files changed

+19
-33
lines changed

paperspace/cli.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,7 @@ def common_experiments_create_single_node_options(f):
235235
@common_experiment_create_multi_node_options
236236
def create_multi_node(api_key, **kwargs):
237237
del_if_value_is_none(kwargs)
238-
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST)
239-
if api_key:
240-
experiments_api.api_key = api_key
238+
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST, api_key=api_key)
241239
experiments_commands.create_experiment(kwargs, api=experiments_api)
242240

243241

@@ -247,9 +245,7 @@ def create_multi_node(api_key, **kwargs):
247245
def create_single_node(api_key, **kwargs):
248246
kwargs["experimentTypeId"] = constants.ExperimentType.SINGLE_NODE
249247
del_if_value_is_none(kwargs)
250-
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST)
251-
if api_key:
252-
experiments_api.api_key = api_key
248+
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST, api_key=api_key)
253249
experiments_commands.create_experiment(kwargs, api=experiments_api)
254250

255251

@@ -258,9 +254,7 @@ def create_single_node(api_key, **kwargs):
258254
@common_experiment_create_multi_node_options
259255
def create_and_start_multi_node(api_key, **kwargs):
260256
del_if_value_is_none(kwargs)
261-
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST)
262-
if api_key:
263-
experiments_api.api_key = api_key
257+
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST, api_key=api_key)
264258
experiments_commands.create_and_start_experiment(kwargs, api=experiments_api)
265259

266260

@@ -270,9 +264,7 @@ def create_and_start_multi_node(api_key, **kwargs):
270264
def create_and_start_single_node(api_key, **kwargs):
271265
kwargs["experimentTypeId"] = constants.ExperimentType.SINGLE_NODE
272266
del_if_value_is_none(kwargs)
273-
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST)
274-
if api_key:
275-
experiments_api.api_key = api_key
267+
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST, api_key=api_key)
276268
experiments_commands.create_and_start_experiment(kwargs, api=experiments_api)
277269

278270

@@ -283,9 +275,7 @@ def create_and_start_single_node(api_key, **kwargs):
283275
"api_key",
284276
)
285277
def start(experiment_handle, api_key):
286-
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST)
287-
if api_key:
288-
experiments_api.api_key = api_key
278+
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST, api_key=api_key)
289279
experiments_commands.start_experiment(experiment_handle, api=experiments_api)
290280

291281

@@ -296,9 +286,7 @@ def start(experiment_handle, api_key):
296286
"api_key",
297287
)
298288
def stop(experiment_handle, api_key):
299-
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST)
300-
if api_key:
301-
experiments_api.api_key = api_key
289+
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST, api_key=api_key)
302290
experiments_commands.stop_experiment(experiment_handle, api=experiments_api)
303291

304292

@@ -309,9 +297,7 @@ def stop(experiment_handle, api_key):
309297
"api_key",
310298
)
311299
def list_experiments(project_handles, api_key):
312-
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST)
313-
if api_key:
314-
experiments_api.api_key = api_key
300+
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST, api_key=api_key)
315301
command = experiments_commands.ListExperimentsCommand(api=experiments_api)
316302
command.execute(project_handles)
317303

@@ -323,9 +309,7 @@ def list_experiments(project_handles, api_key):
323309
"api_key",
324310
)
325311
def get_experiment_details(experiment_handle, api_key):
326-
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST)
327-
if api_key:
328-
experiments_api.api_key = api_key
312+
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST, api_key=api_key)
329313
experiments_commands.get_experiment_details(experiment_handle, api=experiments_api)
330314

331315
# TODO: delete experiment - not implemented in the api

paperspace/client.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@
88

99

1010
class API(object):
11-
def __init__(self, api_url, headers=None):
11+
def __init__(self, api_url, headers=None, api_key=None):
1212
self.api_url = api_url
1313
headers = headers or default_headers
1414
self.headers = headers.copy()
15+
if api_key:
16+
self.api_key = api_key
17+
18+
@property
19+
def api_key(self):
20+
return self.headers.get("X-API-Key")
21+
22+
@api_key.setter
23+
def api_key(self, value):
24+
self.headers["X-API-Key"] = value
1525

1626
def get_path(self, url):
1727
api_url = self.api_url if not self.api_url.endswith("/") else self.api_url[:-1]
@@ -41,11 +51,3 @@ def get(self, url, params=None):
4151
logger.debug("Response status code: {}".format(response.status_code))
4252
logger.debug("Response content: {}".format(response.content))
4353
return response
44-
45-
@property
46-
def api_key(self):
47-
return self.headers.get("X-API-Key")
48-
49-
@api_key.setter
50-
def api_key(self, value):
51-
self.headers["X-API-Key"] = value

0 commit comments

Comments
 (0)