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

Add a timeout on all requests with a custom transport layer

parent 875dce8b
No related branches found
Tags 0.2.3-rc5
No related merge requests found
Pipeline #28352 passed
......@@ -7,8 +7,9 @@ import logging
import warnings
import yaml
from arkindex.auth import TokenSessionAuthentication
from arkindex.pagination import ResponsePaginator
from arkindex.encoders import XMLEncoder
from arkindex.pagination import ResponsePaginator
from arkindex.transports import ArkindexHTTPTransport
from io import IOBase, BufferedIOBase
from time import sleep
from urllib.parse import urlsplit, urlunsplit
......@@ -81,6 +82,9 @@ class ArkindexClient(apistar.Client):
def __repr__(self):
return '<{} on {}>'.format(self.__class__.__name__, self.document.url)
def init_transport(self, *args, **kwargs):
return ArkindexHTTPTransport(*args, **kwargs)
def configure(self, token=None, base_url=None, sleep=None):
"""
Reconfigure the API client.
......@@ -104,10 +108,6 @@ class ArkindexClient(apistar.Client):
# Add the Referer header to allow Django CSRF to function
self.transport.headers.setdefault('Referer', self.document.url)
# Add the XML encoder
if not any(isinstance(e, XMLEncoder) for e in self.transport.encoders):
self.transport.encoders.append(XMLEncoder())
def paginate(self, *args, **kwargs):
"""
Perform a usual request as done by APIStar, but handle paginated endpoints.
......@@ -147,7 +147,7 @@ class ArkindexClient(apistar.Client):
currently handle anything that is not JSON.
:param str operation_id: An OpenAPI operation ID.
:param content: Any kind of content to be send to
:param content: Any kind of content to be sent to
the APIStar transport layer directly.
:param str encoding: A MIME type to use as the Content-Type for the request.
:param params: Query parameters for the given operation.
......
from apistar.client.transports import HTTPTransport
from apistar.client.encoders import JSONEncoder, MultiPartEncoder, URLEncodedEncoder
from arkindex.encoders import XMLEncoder
class ArkindexHTTPTransport(HTTPTransport):
default_encoders = [
JSONEncoder(),
MultiPartEncoder(),
URLEncodedEncoder(),
XMLEncoder(),
]
def get_request_options(self, *args, **kwargs):
options = super().get_request_options(*args, **kwargs)
options['timeout'] = (30, 60)
return options
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