Skip to content

Commit 7caed6f

Browse files
committed
added locations service, added all availabilities endpoint,
use proper url query params
1 parent da13015 commit 7caed6f

6 files changed

Lines changed: 60 additions & 13 deletions

File tree

datacrunch/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def __init__(self):
5656

5757

5858
class Locations:
59-
FIN_01 = "FIN-01"
60-
ICE_01 = "ICE-01"
59+
FIN_01: str = "FIN-01"
60+
ICE_01: str = "ICE-01"
6161

6262
def __init__(self):
6363
return

datacrunch/datacrunch.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from datacrunch.volume_types.volume_types import VolumeTypesService
1010
from datacrunch.volumes.volumes import VolumesService
1111
from datacrunch.constants import Constants
12+
from datacrunch.locations.locations import LocationsService
1213
from datacrunch.__version__ import VERSION
1314

1415

@@ -62,3 +63,7 @@ def __init__(self, client_id: str, client_secret: str, base_url: str = "https://
6263

6364
self.volumes: VolumesService = VolumesService(self._http_client)
6465
"""Volume service. Create, attach, detach, get, rename, delete volumes"""
66+
67+
self.locations: LocationsService = LocationsService(
68+
self._http_client)
69+
"""Locations service. Get locations"""

datacrunch/http_client/http_client.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __init__(self, auth_service, base_url: str) -> None:
3232
self._auth_service = auth_service
3333
self._auth_service.authenticate()
3434

35-
def post(self, url: str, json: dict = None, **kwargs) -> requests.Response:
35+
def post(self, url: str, json: dict = None, params: dict = None, **kwargs) -> requests.Response:
3636
"""Sends a POST request.
3737
3838
A wrapper for the requests.post method.
@@ -43,6 +43,8 @@ def post(self, url: str, json: dict = None, **kwargs) -> requests.Response:
4343
:type url: str
4444
:param json: A JSON serializable Python object to send in the body of the Request, defaults to None
4545
:type json: dict, optional
46+
:param params: Dictionary of querystring data to attach to the Request, defaults to None
47+
:type params: dict, optional
4648
4749
:raises APIException: an api exception with message and error type code
4850
@@ -54,12 +56,13 @@ def post(self, url: str, json: dict = None, **kwargs) -> requests.Response:
5456
url = self._add_base_url(url)
5557
headers = self._generate_headers()
5658

57-
response = requests.post(url, json=json, headers=headers, **kwargs)
59+
response = requests.post(
60+
url, json=json, headers=headers, params=params, **kwargs)
5861
handle_error(response)
5962

6063
return response
6164

62-
def put(self, url: str, json: dict = None, **kwargs) -> requests.Response:
65+
def put(self, url: str, json: dict = None, params: dict = None, **kwargs) -> requests.Response:
6366
"""Sends a PUT request.
6467
6568
A wrapper for the requests.put method.
@@ -70,6 +73,8 @@ def put(self, url: str, json: dict = None, **kwargs) -> requests.Response:
7073
:type url: str
7174
:param json: A JSON serializable Python object to send in the body of the Request, defaults to None
7275
:type json: dict, optional
76+
:param params: Dictionary of querystring data to attach to the Request, defaults to None
77+
:type params: dict, optional
7378
7479
:raises APIException: an api exception with message and error type code
7580
@@ -81,7 +86,8 @@ def put(self, url: str, json: dict = None, **kwargs) -> requests.Response:
8186
url = self._add_base_url(url)
8287
headers = self._generate_headers()
8388

84-
response = requests.put(url, json=json, headers=headers, **kwargs)
89+
response = requests.put(
90+
url, json=json, headers=headers, params=params, **kwargs)
8591
handle_error(response)
8692

8793
return response
@@ -95,7 +101,7 @@ def get(self, url: str, params: dict = None, **kwargs) -> requests.Response:
95101
96102
:param url: relative url of the API endpoint
97103
:type url: str
98-
:param params: Dictionary, list of tuples or bytes to send in the query string for the Request. defaults to None
104+
:param params: Dictionary of querystring data to attach to the Request, defaults to None
99105
:type params: dict, optional
100106
101107
:raises APIException: an api exception with message and error type code
@@ -113,7 +119,7 @@ def get(self, url: str, params: dict = None, **kwargs) -> requests.Response:
113119

114120
return response
115121

116-
def delete(self, url: str, json: dict = None, **kwargs) -> requests.Response:
122+
def delete(self, url: str, json: dict = None, params: dict = None, **kwargs) -> requests.Response:
117123
"""Sends a DELETE request.
118124
119125
A wrapper for the requests.delete method.
@@ -124,6 +130,8 @@ def delete(self, url: str, json: dict = None, **kwargs) -> requests.Response:
124130
:type url: str
125131
:param json: A JSON serializable Python object to send in the body of the Request, defaults to None
126132
:type json: dict, optional
133+
:param params: Dictionary of querystring data to attach to the Request, defaults to None
134+
:type params: dict, optional
127135
128136
:raises APIException: an api exception with message and error type code
129137
@@ -135,7 +143,8 @@ def delete(self, url: str, json: dict = None, **kwargs) -> requests.Response:
135143
url = self._add_base_url(url)
136144
headers = self._generate_headers()
137145

138-
response = requests.delete(url, headers=headers, json=json, **kwargs)
146+
response = requests.delete(
147+
url, headers=headers, json=json, params=params, **kwargs)
139148
handle_error(response)
140149

141150
return response

datacrunch/instances/instances.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,16 +426,33 @@ def action(self, id_list: Union[List[str], str], action: str, volume_ids: Option
426426
self._http_client.put(INSTANCES_ENDPOINT, json=payload)
427427
return
428428

429-
def is_available(self, instance_type: str, is_spot: bool = None) -> bool:
429+
# TODO: use enum/const for location_code
430+
def is_available(self, instance_type: str, is_spot: bool = None, location_code: str = Locations.FIN_01) -> bool:
430431
"""Returns True if a specific instance type is now available for deployment
431432
432433
:param instance_type: instance type
433434
:type instance_type: str
434435
:param is_spot: Is spot instance
435436
:type is_spot: bool, optional
437+
:param location_code: datacenter location, defaults to "FIN-01"
438+
:type location_code: str, optional
436439
:return: True if available to deploy, False otherwise
437440
:rtype: bool
438441
"""
439-
query_param = '?isSpot=true' if is_spot else ''
440-
url = f'/instance-availability/{instance_type}{query_param}'
441-
return self._http_client.get(url).json()
442+
query_params = {'isSpot': is_spot, 'location_code': location_code}
443+
url = f'/instance-availability/{instance_type}'
444+
return self._http_client.get(url, query_params).json()
445+
446+
# TODO: use enum/const for location_code
447+
def get_availabilities(self, is_spot: bool = None, location_code: str = Locations.FIN_01) -> bool:
448+
"""Returns a list of available instance types
449+
450+
:param is_spot: Is spot instance
451+
:type is_spot: bool, optional
452+
:param location_code: datacenter location, defaults to "FIN-01"
453+
:type location_code: str, optional
454+
:return: list of available instance types in every location
455+
:rtype: list
456+
"""
457+
query_params = {'isSpot': is_spot, 'location_code': location_code}
458+
return self._http_client.get('/instance-availability', params=query_params).json()

datacrunch/locations/__init__.py

Whitespace-only changes.

datacrunch/locations/locations.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List
2+
3+
LOCATIONS_ENDPOINT = '/locations'
4+
5+
6+
class LocationsService:
7+
"""A service for interacting with the locations endpoint"""
8+
9+
def __init__(self, http_client) -> None:
10+
self._http_client = http_client
11+
12+
def get(self) -> List[dict]:
13+
"""Get all locations
14+
"""
15+
locations = self._http_client.get(LOCATIONS_ENDPOINT).json()
16+
return locations

0 commit comments

Comments
 (0)