A sample client to interface with Congress.gov.
| 33 | |
| 34 | |
| 35 | class CDGClient: |
| 36 | """ A sample client to interface with Congress.gov. """ |
| 37 | |
| 38 | def __init__( |
| 39 | self, |
| 40 | api_key, |
| 41 | api_version=API_VERSION, |
| 42 | response_format=RESPONSE_FORMAT, |
| 43 | raise_on_error=True, |
| 44 | ): |
| 45 | self.base_url = urljoin(ROOT_URL, api_version) + "/" |
| 46 | self._session = requests.Session() |
| 47 | |
| 48 | # do not use url parameters, even if offered, use headers |
| 49 | self._session.params = {"format": response_format} |
| 50 | self._session.headers.update({"x-api-key": api_key}) |
| 51 | |
| 52 | if raise_on_error: |
| 53 | self._session.hooks = { |
| 54 | "response": lambda r, *args, **kwargs: r.raise_for_status() |
| 55 | } |
| 56 | |
| 57 | def __getattr__(self, method_name): |
| 58 | """Find the session method dynamically and cache for later.""" |
| 59 | method = _MethodWrapper(self, method_name) |
| 60 | self.__dict__[method_name] = method |
| 61 | return method |