Skip to content

Commit 4eb9de9

Browse files
committed
API: rename request() to api_request(), remove request_to_api arg
1 parent f5574e5 commit 4eb9de9

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

test/test_storage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def test_create_storage_import(self, manager):
127127

128128
@responses.activate
129129
def test_upload_file_for_storage_import(self, manager):
130+
# TODO: this test probably doesn't correctly test for the actual format of the data being uploaded
130131
data = Mock.mock_post(
131132
"storage/01d4fcd4-e446-433b-8a9c-551a1284952e/import", ignore_data_field=True
132133
)

upcloud_api/api.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ class API:
1010
Handles basic HTTP communication with API.
1111
"""
1212

13-
api = 'api.upcloud.com'
14-
api_v = '1.3'
13+
api_root = 'https://api.upcloud.com/1.3'
1514

1615
def __init__(self, token, timeout=None):
1716
"""
@@ -20,27 +19,21 @@ def __init__(self, token, timeout=None):
2019
self.token = token
2120
self.timeout = timeout
2221

23-
def request(self, method, endpoint, body=None, params=None, timeout=-1, request_to_api=True):
22+
def api_request(self, method, endpoint, body=None, params=None, timeout=-1):
2423
"""
25-
Perform a request with a given body to a given endpoint in UpCloud's API or UpCloud's uploader session.
24+
Perform a request with a given JSON body to a given endpoint in UpCloud's API.
2625
2726
Handles errors with __error_middleware.
2827
"""
2928
if method not in {'GET', 'POST', 'PUT', 'PATCH', 'DELETE'}:
3029
raise Exception('Invalid/Forbidden HTTP method')
3130

32-
# TODO: revise the semantics of `request_to_api` and where it is set to False
33-
url = 'https://api.upcloud.com/' + self.api_v + endpoint if request_to_api else endpoint
31+
url = f'{self.api_root}{endpoint}'
3432
headers = {'Authorization': self.token, 'User-Agent': self._get_user_agent()}
3533

36-
headers['Content-Type'] = (
37-
'application/json' if request_to_api else 'application/octet-stream'
38-
)
39-
40-
if body and request_to_api:
34+
if body:
4135
data = json.dumps(body)
42-
elif body and not request_to_api:
43-
data = body
36+
headers['Content-Type'] = 'application/json'
4437
else:
4538
data = None
4639

@@ -61,33 +54,31 @@ def get_request(self, endpoint, params=None, timeout=-1):
6154
"""
6255
Perform a GET request to a given endpoint in UpCloud's API.
6356
"""
64-
return self.request('GET', endpoint, params=params, timeout=timeout)
57+
return self.api_request('GET', endpoint, params=params, timeout=timeout)
6558

6659
def post_request(self, endpoint, body=None, timeout=-1):
6760
"""
6861
Perform a POST request to a given endpoint in UpCloud's API.
6962
"""
70-
return self.request('POST', endpoint, body=body, timeout=timeout)
63+
return self.api_request('POST', endpoint, body=body, timeout=timeout)
7164

72-
def put_request(self, endpoint, body=None, timeout=-1, request_to_api=True):
65+
def put_request(self, endpoint, body=None, timeout=-1):
7366
"""
74-
Perform a PUT request to a given endpoint in UpCloud's API or UpCloud's uploader session.
67+
Perform a PUT request to a given endpoint in UpCloud's API.
7568
"""
76-
return self.request(
77-
'PUT', endpoint, body=body, timeout=timeout, request_to_api=request_to_api
78-
)
69+
return self.api_request('PUT', endpoint, body=body, timeout=timeout)
7970

8071
def patch_request(self, endpoint, body=None, timeout=-1):
8172
"""
8273
Perform a PATCH request to a given endpoint in UpCloud's API.
8374
"""
84-
return self.request('PATCH', endpoint, body=body, timeout=timeout)
75+
return self.api_request('PATCH', endpoint, body=body, timeout=timeout)
8576

8677
def delete_request(self, endpoint, timeout=-1):
8778
"""
8879
Perform a DELETE request to a given endpoint in UpCloud's API.
8980
"""
90-
return self.request('DELETE', endpoint, timeout=timeout)
81+
return self.api_request('DELETE', endpoint, timeout=timeout)
9182

9283
def __error_middleware(self, res, res_json):
9384
"""

upcloud_api/cloud_manager/storage_mixin.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,22 @@ def upload_file_for_storage_import(self, storage_import, file):
195195
"""
196196
Uploads a file directly to UpCloud's uploader session.
197197
"""
198-
url = storage_import.direct_upload_url
199-
data = get_raw_data_from_file(file)
200-
body = {'data': data}
201-
return self.api.put_request(url, body, timeout=600, request_to_api=False)
198+
# TODO: this should not buffer the entire `file` into memory
199+
200+
# This is importing and using `requests` directly since there doesn't
201+
# seem to be a point in adding a `.api.raw_request()` call to the `API` class.
202+
# That could be changed.
203+
204+
import requests
205+
206+
resp = requests.put(
207+
url=storage_import.direct_upload_url,
208+
data=get_raw_data_from_file(file),
209+
headers={'Content-type': 'application/octet-stream'},
210+
timeout=600,
211+
)
212+
resp.raise_for_status()
213+
return resp.json()
202214

203215
def get_storage_import_details(self, storage: str) -> StorageImport:
204216
"""

0 commit comments

Comments
 (0)