@@ -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 """
0 commit comments