The Base Consumer Class¶
Consumer¶
-
class
uplink.
Consumer
(base_url='', client=None, converters=(), auth=None, hooks=(), **kwargs)¶ Base consumer class with which to define custom consumers.
Example usage:
from uplink import Consumer, get class GitHub(Consumer): @get("/users/{user}") def get_user(self, user): pass client = GitHub("https://api.github.com/") client.get_user("prkumar").json() # {'login': 'prkumar', ... }
Parameters: - base_url (
str
, optional) – The base URL for any request sent from this consumer instance. - client (optional) – A supported HTTP client instance (e.g.,
a
requests.Session
) or an adapter (e.g.,RequestsClient
). - converters (
ConverterFactory
, optional) – One or more objects that encapsulate custom (de)serialization strategies for request properties and/or the response body. (E.g.,MarshmallowConverter
) - auth (
tuple
orcallable
, optional) – The authentication object for this consumer instance. - hooks (
TransactionHook
, optional) – One or more hooks to modify behavior of request execution and response handling (seeresponse_handler
orerror_handler
).
-
exceptions
¶ An enum of standard HTTP client exceptions that can be handled.
This property enables the handling of specific exceptions from the backing HTTP client.
Example
try: github.get_user(user_id) except github.exceptions.ServerTimeout: # Handle the timeout of the request ...
-
session
¶ The
Session
object for this consumer instance.Exposes the configuration of this
Consumer
instance and allows the persistence of certain properties across all requests sent from that instance.Example usage:
import uplink class MyConsumer(uplink.Consumer): def __init__(self, language): # Set this header for all requests of the instance. self.session.headers["Accept-Language"] = language ...
Returns: Session
- base_url (
Session¶
-
class
uplink.session.
Session
¶ The session of a
Consumer
instance.Exposes the configuration of a
Consumer
instance and allows the persistence of certain properties across all requests sent from that instance.-
auth
¶ The authentication object for this consumer instance.
-
base_url
¶ The base URL for any requests sent from this consumer instance.
-
headers
¶ A dictionary of headers to be sent on each request from this consumer instance.
-
inject
(hook, *more_hooks)¶ Add hooks (e.g., functions decorated with either
response_handler
orerror_handler
) to the session.
-
params
¶ A dictionary of querystring data to attach to each request from this consumer instance.
-