|
| 1 | +import os |
| 2 | +from oauthlib.oauth2 import BackendApplicationClient, TokenExpiredError |
| 3 | +from requests_oauthlib import OAuth2Session |
| 4 | +from nypl_py_utils.functions.log_helper import create_log |
| 5 | + |
| 6 | + |
| 7 | +class PlatformApiClient: |
| 8 | + """ |
| 9 | + Client for interacting with NYPL Platform API endpoints |
| 10 | + """ |
| 11 | + |
| 12 | + def __init__(self, client_id=None, client_secret=None, base_url=None, |
| 13 | + token_url=None): |
| 14 | + self.client_id = client_id \ |
| 15 | + or os.environ.get('NYPL_API_CLIENT_ID', None) |
| 16 | + self.client_secret = client_secret \ |
| 17 | + or os.environ.get('NYPL_API_CLIENT_SECRET', None) |
| 18 | + self.token_url = token_url \ |
| 19 | + or os.environ.get('NYPL_API_TOKEN_URL', None) |
| 20 | + self.base_url = base_url \ |
| 21 | + or os.environ.get('NYPL_API_BASE_URL', None) |
| 22 | + |
| 23 | + self.client = None |
| 24 | + self.token = None |
| 25 | + |
| 26 | + self.logger = create_log('platform_api_client') |
| 27 | + |
| 28 | + def get(self, request_path, **kwargs): |
| 29 | + """ |
| 30 | + Issue an HTTP GET on the given request_path |
| 31 | + """ |
| 32 | + return self._do_http_method('GET', request_path, **kwargs) |
| 33 | + |
| 34 | + def post(self, request_path, json, **kwargs): |
| 35 | + """ |
| 36 | + Issue an HTTP POST on the given request_path with given JSON body |
| 37 | + """ |
| 38 | + return self._do_http_method('GET', request_path, **kwargs) |
| 39 | + kwargs['json'] = json |
| 40 | + return self._do_http_method('POST', request_path, **kwargs) |
| 41 | + |
| 42 | + def patch(self, request_path, json, **kwargs): |
| 43 | + """ |
| 44 | + Issue an HTTP PATCH on the given request_path with given JSON body |
| 45 | + """ |
| 46 | + kwargs['json'] = json |
| 47 | + return self._do_http_method('PATCH', request_path, **kwargs) |
| 48 | + |
| 49 | + def delete(self, request_path, **kwargs): |
| 50 | + """ |
| 51 | + Issue an HTTP DELETE on the given request_path |
| 52 | + """ |
| 53 | + return self._do_http_method('DELETE', request_path, **kwargs) |
| 54 | + |
| 55 | + def _do_http_method(self, method, request_path, **kwargs): |
| 56 | + """ |
| 57 | + Issue an HTTP method call on on the given request_path |
| 58 | + """ |
| 59 | + if not self.client: |
| 60 | + self._create_oauth_client() |
| 61 | + |
| 62 | + url = f'{self.base_url}/{request_path}' |
| 63 | + self.logger.debug(f'{method} {url}') |
| 64 | + |
| 65 | + try: |
| 66 | + return self.oauth_client.request(method, url, **kwargs).json() |
| 67 | + except TokenExpiredError as e: |
| 68 | + print(f'TokenExpiredError encountered: {e}') |
| 69 | + self._generate_access_token() |
| 70 | + |
| 71 | + return self._do_http_method(method, request_path, **kwargs) |
| 72 | + except TimeoutError as e: |
| 73 | + print(f'TimeoutError encountered: {e}') |
| 74 | + return {} |
| 75 | + |
| 76 | + def _create_oauth_client(self): |
| 77 | + """ |
| 78 | + Creates an authenticated a OAuth2Session instance for later requests |
| 79 | + """ |
| 80 | + self._generate_access_token() |
| 81 | + self.oauth_client = OAuth2Session(self.client_id, token=self.token) |
| 82 | + |
| 83 | + def _generate_access_token(self): |
| 84 | + """ |
| 85 | + Fetch and store a fresh token |
| 86 | + """ |
| 87 | + client = BackendApplicationClient(client_id=self.client_id) |
| 88 | + oauth = OAuth2Session(client=client) |
| 89 | + self.logger.debug(f'Refreshing token via @{self.token_url}') |
| 90 | + self.token = oauth.fetch_token( |
| 91 | + token_url=self.token_url, |
| 92 | + client_id=self.client_id, |
| 93 | + client_secret=self.client_secret |
| 94 | + ) |
0 commit comments