diff --git a/arkindex/client.py b/arkindex/client.py index 31567a2e1b55f89a77f8aeb7ac653f2682b027f7..4f4657966e2161dba725917e55f213db5ca71619 100644 --- a/arkindex/client.py +++ b/arkindex/client.py @@ -4,6 +4,7 @@ Arkindex API Client import os import apistar import logging +import warnings import yaml from arkindex.auth import TokenSessionAuthentication from arkindex.pagination import ResponsePaginator @@ -33,6 +34,14 @@ def options_from_env(): return options +def _find_operation(schema, operation_id): + for path_object in schema['paths'].values(): + for operation in path_object.values(): + if operation['operationId'] == operation_id: + return operation + raise KeyError("Operation '{}' not found".format(operation_id)) + + class ArkindexClient(apistar.Client): """ An Arkindex API client. @@ -52,8 +61,16 @@ class ArkindexClient(apistar.Client): schema = yaml.safe_load(f.read()) super().__init__(schema, **kwargs) - # Remove domains from each endpoint; allows APIStar to properly handle our base URL + + # Post-processing of the parsed schema for link_info in self.document.walk_links(): + # Look for deprecated links + # https://github.com/encode/apistar/issues/664 + operation = _find_operation(schema, link_info.link.name) + link_info.link.deprecated = operation.get('deprecated', False) + + # Remove domains from each endpoint; allows APIStar to properly handle our base URL + # https://github.com/encode/apistar/issues/657 original_url = urlsplit(link_info.link.url) # Removes the scheme and netloc new_url = ('', '', *original_url[2:]) @@ -110,16 +127,19 @@ class ArkindexClient(apistar.Client): self.transport.session.auth.token = resp['auth_token'] return resp - def request(self, *args, **kwargs): + def request(self, operation_id, *args, **kwargs): """ Perform an API request. :param args: Arguments passed to the APIStar client. :param kwargs: Keyword arguments passed to the APIStar client. """ + link = self.lookup_operation(operation_id) + if link.deprecated: + warnings.warn("Endpoint '{}' is deprecated.".format(operation_id), DeprecationWarning) if self.sleep_duration: logger.debug('Delaying request by {:f} seconds...'.format(self.sleep_duration)) sleep(self.sleep_duration) - return super().request(*args, **kwargs) + return super().request(operation_id, *args, **kwargs) def custom_request(self, operation_id, content=None, encoding='text/plain', **params): """ @@ -136,6 +156,8 @@ class ArkindexClient(apistar.Client): url = self.get_url(link, params) query_params = self.get_query_params(link, params) + if link.deprecated: + warnings.warn("Endpoint '{}' is deprecated.".format(operation_id), DeprecationWarning) if self.sleep_duration: logger.debug('Delaying request by {:f} seconds...'.format(self.sleep_duration)) sleep(self.sleep_duration)