Skip to content

Commit

Permalink
chore: test for update device error handling (#235) (#236)
Browse files Browse the repository at this point in the history
* [Automated] Version Bump from 0.3.9 to 0.3.10

* Add error handling for UpdateDevice call for status code 404

* Add error handling for UpdateDevice call for status code 400

* Add tests for UpdateDevice api call

* Add tests for UpdateDevice api call

* Change response handlers in device api to be handled by BaseApiManager

* Change response handlers in sim api and general api_manager.py

* Add function to anonymize response before recording

* Remove unnecessary print statement

---------

Co-authored-by: Mikhail Gubenko <[email protected]>
Co-authored-by: Th3Un1q3 <[email protected]>
  • Loading branch information
3 people authored Oct 22, 2024
1 parent 4274734 commit 98d54b2
Show file tree
Hide file tree
Showing 7 changed files with 1,203 additions and 77 deletions.
28 changes: 17 additions & 11 deletions emnify/api_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

MAIN_URL = os.environ.get('EMNIFY_SDK_API_ENDPOINT_URL', 'https://cdn.emnify.net/api')

MAX_PAGES_IN_PAGINATOR = 1000 # with regular page size 1000...2000 gives max 2_000_000 records
MAX_PAGES_IN_PAGINATOR = 1000 # with regular page size 1000...2000 gives max 2_000_000 records


class BaseApiManager:
"""
Expand All @@ -18,8 +19,10 @@ class BaseApiManager:
response_handlers = {
200: 'return_unwrapped',
201: 'return_success',
401: 'unauthorised',
204: 'return_success',
400: 'process_exception',
401: 'unauthorised',
404: 'process_exception',
409: 'process_exception',
422: 'process_exception'
}
Expand Down Expand Up @@ -47,7 +50,8 @@ def process_exception(self, response: requests.Response, client, data: dict = No
raise emnify_errors.ValidationErrorException(f'{response.json()}')

def return_paginator(
self, response: requests.Response, client, data: dict = None, files=None, path_params: dict = None, query_params: dict = None
self, response: requests.Response, client, data: dict = None, files=None, path_params: dict = None,
query_params: dict = None
) -> typing.Generator:
query_params = query_params or {}
page = query_params.get('page', 1) if query_params else 1
Expand Down Expand Up @@ -92,7 +96,7 @@ def call_api(self, client, data: dict = None, files=None, path_params: dict = No
"Unknown status code {status_code}".format(status_code=response.status_code)
)

return getattr(self, self.response_handlers[response.status_code])\
return getattr(self, self.response_handlers[response.status_code]) \
(response, client, data=data, files=files, query_params=query_params, path_params=path_params)

def make_get_request(self, main_url: str, method_name: str, headers: dict, params: str = None):
Expand All @@ -101,10 +105,12 @@ def make_get_request(self, main_url: str, method_name: str, headers: dict, param
def make_post_request(self, main_url: str, method_name: str, headers: dict, params: dict = None, data: dict = None):
return requests.post(self.resource_path(main_url, method_name), headers=headers, json=data, params=params)

def make_patch_request(self, main_url: str, method_name: str, headers: dict, params: dict = None, data: dict = None):
def make_patch_request(self, main_url: str, method_name: str, headers: dict, params: dict = None,
data: dict = None):
return requests.patch(self.resource_path(main_url, method_name), headers=headers, json=data, params=params)

def make_delete_request(self, main_url: str, method_name: str, headers: dict, params: dict = None, data: dict = None):
def make_delete_request(self, main_url: str, method_name: str, headers: dict, params: dict = None,
data: dict = None):
return requests.delete(self.resource_path(main_url, method_name), headers=headers, json=data, params=params)

def make_put_request(self, main_url: str, method_name: str, headers: dict, params: dict = None, data: dict = None):
Expand Down Expand Up @@ -158,11 +164,11 @@ class Authenticate(BaseApiManager):
request_url_prefix = emnify_constants.AuthenticateRequestsUrl.V1_AUTHENTICATE.value
request_method_name = emnify_constants.RequestsType.POST.value

response_handlers = {
200: 'return_unwrapped',
401: 'unauthorised',
404: 'unexpected_error'
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.response_handlers = self.response_handlers.copy() | {
404: 'unexpected_error'
}

def unauthorised(
self, response: requests.Response, client, data: dict = None, files=None, path_params: list = None, **kwargs
Expand Down
31 changes: 6 additions & 25 deletions emnify/modules/device/api_call_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
class GetAllDevicesApiCall(BaseApiManager):
request_url_prefix = '/v1/endpoint'
request_method_name = RequestsType.GET.value
response_handlers = {
200: 'return_paginator',
401: 'unauthorised',
}

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.response_handlers = self.response_handlers.copy() | {
200: 'return_paginator'
}


class GetEventsByDevice(BaseApiManager):
Expand All @@ -27,12 +29,6 @@ def __init__(self, *args, **kwargs):
class CreateDevice(BaseApiManager):
request_url_prefix = '/v1/endpoint'
request_method_name = RequestsType.POST.value
response_handlers = {
200: 'return_unwrapped',
201: 'return_success',
401: 'unauthorised',
422: 'process_exception'
}

def return_success(self, response: requests.Response, client, data: dict = None, *args, **kwargs) -> True:
return int(response.headers.get('Location').split('/')[-1])
Expand All @@ -46,10 +42,6 @@ class GetAllSmsFromDevice(BaseApiManager):
class SendSmsToDevice(BaseApiManager):
request_url_prefix = '/v1/endpoint/{endpoint_id}/sms'
request_method_name = RequestsType.POST.value
response_handlers = {
201: 'return_success',
401: 'unauthorised',
}


class RetrieveDevice(BaseApiManager):
Expand All @@ -60,11 +52,6 @@ class RetrieveDevice(BaseApiManager):
class UpdateDevice(BaseApiManager):
request_url_prefix = RequestUrls.ENDPOINT_IN_URL.value
request_method_name = RequestsType.PATCH.value
response_handlers = {
204: 'return_success',
401: 'unauthorised',
422: 'process_exception'
}


class DeleteDevice(BaseApiManager):
Expand Down Expand Up @@ -106,12 +93,6 @@ class GetDeviceConnectivity(BaseApiManager):
request_url_prefix = '/v1/endpoint/{endpoint_id}/connectivity'
request_method_name = RequestsType.GET.value

response_handlers = {
200: 'return_unwrapped',
401: 'unauthorised',
422: 'process_exception',
}

def process_exception(self, response: requests.Response, client, data: dict = None, *args, **kwargs):
raise ValidationErrorException(
'device_id is not valid'
Expand Down
16 changes: 0 additions & 16 deletions emnify/modules/sim/api_call_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ class SimActivateApi(BaseApiManager):
request_url_prefix = '/v1/sim_batch/bic/{bic}'
request_method_name = RequestsType.PATCH.value

response_handlers = {
200: 'return_unwrapped',
401: 'unauthorised',
422: 'process_exception'
}

def process_exception(self, response: requests.Response, client, data: dict = None, *args, **kwargs):
raise ValidationErrorException('Invalid bic number')

Expand All @@ -33,16 +27,6 @@ class SimUpdateApi(BaseApiManager):
request_url_prefix = '/v1/sim/{sim}'
request_method_name = RequestsType.PATCH.value

response_handlers = {
204: 'return_success',
401: 'unauthorised',
400: 'process_exception',
422: 'unauthorised'
}

def process_exception(self, response: requests.Response, client, data: dict = None, *args, **kwargs):
raise ValidationErrorException(response.json())


class SimRetrieveApi(BaseApiManager):
request_url_prefix = '/v1/sim/{sim}'
Expand Down
Loading

0 comments on commit 98d54b2

Please sign in to comment.