|
| 1 | +# |
| 2 | +# This file is part of the Ingram Micro CloudBlue Connect Python OpenAPI Client. |
| 3 | +# |
| 4 | +# Copyright (c) 2021 Ingram Micro. All Rights Reserved. |
| 5 | +# |
| 6 | +import time |
| 7 | + |
| 8 | +import httpx |
| 9 | + |
| 10 | +import requests |
| 11 | + |
| 12 | +from httpx import HTTPError |
| 13 | + |
| 14 | +from requests.exceptions import RequestException |
| 15 | + |
| 16 | +from connect.client.exceptions import ClientError |
| 17 | + |
| 18 | + |
| 19 | +class SyncClientMixin: |
| 20 | + |
| 21 | + def get(self, url, **kwargs): |
| 22 | + return self.execute('get', url, **kwargs) |
| 23 | + |
| 24 | + def create(self, url, payload=None, **kwargs): |
| 25 | + kwargs = kwargs or {} |
| 26 | + |
| 27 | + if payload: |
| 28 | + kwargs['json'] = payload |
| 29 | + |
| 30 | + return self.execute('post', url, **kwargs) |
| 31 | + |
| 32 | + def update(self, url, payload=None, **kwargs): |
| 33 | + kwargs = kwargs or {} |
| 34 | + |
| 35 | + if payload: |
| 36 | + kwargs['json'] = payload |
| 37 | + |
| 38 | + return self.execute('put', url, **kwargs) |
| 39 | + |
| 40 | + def delete(self, url, **kwargs): |
| 41 | + return self.execute('delete', url, **kwargs) |
| 42 | + |
| 43 | + def execute(self, method, path, **kwargs): |
| 44 | + if ( |
| 45 | + self._use_specs |
| 46 | + and self._validate_using_specs |
| 47 | + and not self.specs.exists(method, path) |
| 48 | + ): |
| 49 | + # TODO more info, specs version, method etc |
| 50 | + raise ClientError(f'The path `{path}` does not exist.') |
| 51 | + |
| 52 | + url = f'{self.endpoint}/{path}' |
| 53 | + |
| 54 | + kwargs = self._prepare_call_kwargs(kwargs) |
| 55 | + |
| 56 | + self.response = None |
| 57 | + |
| 58 | + try: |
| 59 | + self._execute_http_call(method, url, kwargs) |
| 60 | + if self.response.status_code == 204: |
| 61 | + return None |
| 62 | + if self.response.headers['Content-Type'] == 'application/json': |
| 63 | + return self.response.json() |
| 64 | + else: |
| 65 | + return self.response.content |
| 66 | + |
| 67 | + except RequestException as re: |
| 68 | + api_error = self._get_api_error_details() or {} |
| 69 | + status_code = self.response.status_code if self.response is not None else None |
| 70 | + raise ClientError(status_code=status_code, **api_error) from re |
| 71 | + |
| 72 | + def _execute_http_call(self, method, url, kwargs): |
| 73 | + retry_count = 0 |
| 74 | + while True: |
| 75 | + self.response = requests.request(method, url, **kwargs) |
| 76 | + if ( # pragma: no branch |
| 77 | + self.response.status_code == 502 |
| 78 | + and retry_count < self.max_retries |
| 79 | + ): |
| 80 | + retry_count += 1 |
| 81 | + time.sleep(1) |
| 82 | + continue |
| 83 | + break # pragma: no cover |
| 84 | + if self.response.status_code >= 400: |
| 85 | + self.response.raise_for_status() |
| 86 | + |
| 87 | + |
| 88 | +class AsyncClientMixin: |
| 89 | + |
| 90 | + async def get(self, url, **kwargs): |
| 91 | + return await self.execute('get', url, **kwargs) |
| 92 | + |
| 93 | + async def create(self, url, payload=None, **kwargs): |
| 94 | + kwargs = kwargs or {} |
| 95 | + |
| 96 | + if payload: |
| 97 | + kwargs['json'] = payload |
| 98 | + |
| 99 | + return await self.execute('post', url, **kwargs) |
| 100 | + |
| 101 | + async def update(self, url, payload=None, **kwargs): |
| 102 | + kwargs = kwargs or {} |
| 103 | + |
| 104 | + if payload: |
| 105 | + kwargs['json'] = payload |
| 106 | + |
| 107 | + return await self.execute('put', url, **kwargs) |
| 108 | + |
| 109 | + async def delete(self, url, **kwargs): |
| 110 | + return await self.execute('delete', url, **kwargs) |
| 111 | + |
| 112 | + async def execute(self, method, path, **kwargs): |
| 113 | + if ( |
| 114 | + self._use_specs |
| 115 | + and self._validate_using_specs |
| 116 | + and not self.specs.exists(method, path) |
| 117 | + ): |
| 118 | + # TODO more info, specs version, method etc |
| 119 | + raise ClientError(f'The path `{path}` does not exist.') |
| 120 | + |
| 121 | + url = f'{self.endpoint}/{path}' |
| 122 | + |
| 123 | + kwargs = self._prepare_call_kwargs(kwargs) |
| 124 | + |
| 125 | + self.response = None |
| 126 | + |
| 127 | + try: |
| 128 | + await self._execute_http_call(method, url, kwargs) |
| 129 | + if self.response.status_code == 204: |
| 130 | + return None |
| 131 | + if self.response.headers.get('Content-Type') == 'application/json': |
| 132 | + return self.response.json() |
| 133 | + else: |
| 134 | + return self.response.content |
| 135 | + |
| 136 | + except HTTPError as re: |
| 137 | + api_error = self._get_api_error_details() or {} |
| 138 | + status_code = self.response.status_code if self.response is not None else None |
| 139 | + raise ClientError(status_code=status_code, **api_error) from re |
| 140 | + |
| 141 | + async def _execute_http_call(self, method, url, kwargs): |
| 142 | + retry_count = 0 |
| 143 | + while True: |
| 144 | + async with httpx.AsyncClient() as client: |
| 145 | + self.response = await client.request(method, url, **kwargs) |
| 146 | + if ( # pragma: no branch |
| 147 | + self.response.status_code == 502 |
| 148 | + and retry_count < self.max_retries |
| 149 | + ): |
| 150 | + retry_count += 1 |
| 151 | + time.sleep(1) |
| 152 | + continue |
| 153 | + break # pragma: no cover |
| 154 | + if self.response.status_code >= 400: |
| 155 | + self.response.raise_for_status() |
0 commit comments