|
| 1 | +import io |
| 2 | +import json |
| 3 | + |
| 4 | +import requests |
| 5 | + |
| 6 | +from . import exceptions |
| 7 | + |
| 8 | +__all__ = ['Client'] |
| 9 | + |
| 10 | + |
| 11 | +class Client: |
| 12 | + """ |
| 13 | + A client for the GeoUK API. |
| 14 | + """ |
| 15 | + |
| 16 | + def __init__(self, api_key, api_base_url='https://api.geouk.xyz'): |
| 17 | + |
| 18 | + # A key used to authenticate API calls to an account |
| 19 | + self._api_key = api_key |
| 20 | + |
| 21 | + # The base URL to use when calling the API |
| 22 | + self._api_base_url = api_base_url |
| 23 | + |
| 24 | + # NOTE: Rate limiting information is only available after a request |
| 25 | + # has been made. |
| 26 | + |
| 27 | + # The maximum number of requests per second that can be made with the |
| 28 | + # given API key. |
| 29 | + self._rate_limit = None |
| 30 | + |
| 31 | + # The time (seconds since epoch) when the current rate limit will |
| 32 | + # reset. |
| 33 | + self._rate_limit_reset = None |
| 34 | + |
| 35 | + # The number of requests remaining within the current limit before the |
| 36 | + # next reset. |
| 37 | + self._rate_limit_remaining = None |
| 38 | + |
| 39 | + @property |
| 40 | + def rate_limit(self): |
| 41 | + return self._rate_limit |
| 42 | + |
| 43 | + @property |
| 44 | + def rate_limit_reset(self): |
| 45 | + return self._rate_limit_reset |
| 46 | + |
| 47 | + @property |
| 48 | + def rate_limit_remaining(self): |
| 49 | + return self._rate_limit_remaining |
| 50 | + |
| 51 | + def __call__(self, |
| 52 | + method, |
| 53 | + path, |
| 54 | + params=None, |
| 55 | + data=None, |
| 56 | + json_type_body=None, |
| 57 | + files=None, |
| 58 | + download=False |
| 59 | + ): |
| 60 | + """Call the API""" |
| 61 | + |
| 62 | + # Build headers |
| 63 | + headers = {'X-GeoUK-APIKey': self._api_key} |
| 64 | + |
| 65 | + if json_type_body: |
| 66 | + headers['Content-Type'] = 'application/json' |
| 67 | + |
| 68 | + if not download: |
| 69 | + headers['Accept'] = 'application/json' |
| 70 | + |
| 71 | + if params: |
| 72 | + # Filter out parameters set to `None` |
| 73 | + params = {k: v for k, v in params.items() if v is not None} |
| 74 | + |
| 75 | + # Make the request |
| 76 | + r = getattr(requests, method.lower())( |
| 77 | + f'{self._api_base_url}/{path}', |
| 78 | + headers=headers, |
| 79 | + params=params, |
| 80 | + data=data, |
| 81 | + json=json_type_body, |
| 82 | + files=files |
| 83 | + ) |
| 84 | + |
| 85 | + # Update the rate limit |
| 86 | + if 'X-GeoUK-RateLimit-Limit' in r.headers: |
| 87 | + self._rate_limit = int(r.headers['X-GeoUK-RateLimit-Limit']) |
| 88 | + self._rate_limit_reset \ |
| 89 | + = float(r.headers['X-GeoUK-RateLimit-Reset']) |
| 90 | + self._rate_limit_remaining \ |
| 91 | + = int(r.headers['X-GeoUK-RateLimit-Remaining']) |
| 92 | + |
| 93 | + # Handle a successful response |
| 94 | + if r.status_code in [200, 204]: |
| 95 | + |
| 96 | + if download: |
| 97 | + return io.BytesIO(r.content) |
| 98 | + |
| 99 | + if r.headers.get('Content-Type', '')\ |
| 100 | + .startswith('application/json'): |
| 101 | + |
| 102 | + return r.json() |
| 103 | + |
| 104 | + return None |
| 105 | + |
| 106 | + # Raise an error related to the response |
| 107 | + try: |
| 108 | + error = r.json() |
| 109 | + |
| 110 | + except json.decoder.JSONDecodeError: |
| 111 | + pass |
| 112 | + |
| 113 | + finally: |
| 114 | + if not isinstance(error, dict): |
| 115 | + error = {} |
| 116 | + |
| 117 | + error_cls = exceptions.GeoUKException.get_class_by_status_code( |
| 118 | + r.status_code |
| 119 | + ) |
| 120 | + |
| 121 | + raise error_cls( |
| 122 | + r.status_code, |
| 123 | + error.get('hint'), |
| 124 | + error.get('arg_errors') |
| 125 | + ) |
| 126 | + |
| 127 | + |
0 commit comments