Skip to content
Snippets Groups Projects
Commit efff8a42 authored by Erwan Rouchet's avatar Erwan Rouchet Committed by Bastien Abadie
Browse files

Handle deprecated endpoints

parent 8486ae37
No related branches found
No related tags found
No related merge requests found
Pipeline #28254 passed
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment