|
| 1 | +import os |
| 2 | +import json |
| 3 | +import urllib3 |
| 4 | +import inspect |
| 5 | +from enum import Enum |
| 6 | +from typing import Optional, Any |
| 7 | + |
| 8 | +base = 'https://api.polygon.io' |
| 9 | +env_key = "POLYGON_API_KEY" |
| 10 | + |
| 11 | +# https://urllib3.readthedocs.io/en/stable/reference/urllib3.poolmanager.html |
| 12 | +class BaseClient: |
| 13 | + def __init__( |
| 14 | + self, |
| 15 | + api_key: Optional[str] = os.getenv(env_key), |
| 16 | + connect_timeout: float = 10.0, |
| 17 | + read_timeout: float = 10.0, |
| 18 | + num_pools: int = 10, |
| 19 | + retries = 3, |
| 20 | + base: str = base |
| 21 | + ): |
| 22 | + if api_key is None: |
| 23 | + raise Exception(f"Must specify env var {env_key} or pass api_key in constructor") |
| 24 | + self.API_KEY = api_key |
| 25 | + self.BASE = base |
| 26 | + |
| 27 | + # https://urllib3.readthedocs.io/en/stable/reference/urllib3.connectionpool.html#urllib3.HTTPConnectionPool |
| 28 | + self.client = urllib3.PoolManager(num_pools=num_pools, headers={ |
| 29 | + 'Authorization': 'Bearer ' + self.API_KEY |
| 30 | + }) |
| 31 | + self.timeout=urllib3.Timeout(connect=connect_timeout, read=read_timeout) |
| 32 | + self.retries = retries |
| 33 | + |
| 34 | + def _decode(self, resp): |
| 35 | + return json.loads(resp.data.decode('utf-8')) |
| 36 | + |
| 37 | + def _get(self, path: str, params: Optional[dict] = None, resultKey: Optional[str] = None, deserializer = None, raw: bool = False) -> Any: |
| 38 | + if params is None: |
| 39 | + params = {} |
| 40 | + params = {str(k): str(v) for k, v in params.items() if v is not None} |
| 41 | + resp = self.client.request('GET', self.BASE + path, fields=params, retries=self.retries) |
| 42 | + |
| 43 | + if resp.status != 200: |
| 44 | + raise Exception(resp.data.decode('utf-8')) |
| 45 | + |
| 46 | + if raw: |
| 47 | + return resp |
| 48 | + |
| 49 | + obj = self._decode(resp) |
| 50 | + |
| 51 | + if resultKey: |
| 52 | + obj = obj[resultKey] |
| 53 | + |
| 54 | + if deserializer: |
| 55 | + obj = [deserializer(o) for o in obj] |
| 56 | + |
| 57 | + return obj |
| 58 | + |
| 59 | + def _get_params(self, fn, caller_locals): |
| 60 | + params = caller_locals["params"] |
| 61 | + if params is None: |
| 62 | + params = {} |
| 63 | + # https://docs.python.org/3.7/library/inspect.html#inspect.Signature |
| 64 | + for argname, v in inspect.signature(fn).parameters.items(): |
| 65 | + # https://docs.python.org/3.7/library/inspect.html#inspect.Parameter |
| 66 | + if argname in ['params', 'raw']: |
| 67 | + continue |
| 68 | + if v.default != v.empty: |
| 69 | + # timestamp_lt -> timestamp.lt |
| 70 | + val = caller_locals.get(argname, v.default) |
| 71 | + if isinstance(val, Enum): |
| 72 | + val = val.value |
| 73 | + if val is not None: |
| 74 | + params[argname.replace("_", ".")] = val |
| 75 | + |
| 76 | + return params |
| 77 | + |
| 78 | + def _paginate(self, path: str, params: dict, raw: bool, deserializer): |
| 79 | + while True: |
| 80 | + resp = self._get(path=path, params=params, deserializer=deserializer, raw=True) |
| 81 | + if raw: |
| 82 | + return resp |
| 83 | + decoded = self._decode(resp) |
| 84 | + for t in decoded["results"]: |
| 85 | + yield deserializer(t) |
| 86 | + if "next_url" in decoded: |
| 87 | + path = decoded["next_url"].replace(self.BASE, '') |
| 88 | + params = {} |
| 89 | + else: |
| 90 | + return |
0 commit comments