Function Annotations
For programming in general, function parameters drive a function's
dynamic behavior; a function's output depends normally on its inputs.
With uplink
, function arguments parametrize an HTTP request, and you
indicate the dynamic parts of the request by appropriately annotating
those arguments with the classes detailed in this section.
uplink.Path
Substitution of a path variable in a URI template.
URI template parameters are enclosed in braces (e.g., {name}
). To map an argument to a declared URI parameter, use
the Path
annotation:
Then, invoking get_todo
with a consumer instance:
creates an HTTP request with a URL ending in /todos/100
.
Note
Any unannotated function argument that shares a name with a URL path parameter is implicitly annotated with this class at runtime.
For example, we could simplify the method from the previous example by matching the path variable and method argument names:
Source code in uplink/arguments.py
uplink.Query
Set a dynamic query parameter.
This annotation turns argument values into URL query
parameters. You can include it as function argument
annotation, in the format: <query argument>: uplink.Query
.
If the API endpoint you are trying to query uses q
as a query
parameter, you can add q: uplink.Query
to the consumer method to
set the q
search term at runtime.
Examples:
@get("/search/commits")
def search(self, search_term: Query("q")):
"""Search all commits with the given search term."""
To specify whether or not the query parameter is already URL encoded,
use the optional encoded
argument:
@get("/search/commits")
def search(self, search_term: Query("q", encoded=True)):
"""Search all commits with the given search term."""
To specify if and how None
values should be encoded, use the
optional encode_none
argument:
@get("/search/commits")
def search(self, search_term: Query("q"),
search_order: Query("o", encode_none="null")):
"""
Search all commits with the given search term using the
optional search order.
"""
PARAMETER | DESCRIPTION |
---|---|
name
|
The name of the query parameter.
DEFAULT:
|
encoded
|
Specifies whether the parameter name and value are already URL encoded.
DEFAULT:
|
type
|
The type of the query parameter.
DEFAULT:
|
encode_none
|
Specifies an optional string with which
DEFAULT:
|
Source code in uplink/arguments.py
uplink.QueryMap
A mapping of query arguments.
If the API you are using accepts multiple query arguments, you can
include them all in your function method by using the format:
<query argument>: uplink.QueryMap
PARAMETER | DESCRIPTION |
---|---|
encoded
|
Specifies whether the parameter name and value are already URL encoded.
DEFAULT:
|
type
|
The type of the query parameters.
DEFAULT:
|
Source code in uplink/arguments.py
uplink.Header
Pass a header as a method argument at runtime.
While uplink.headers
attaches request headers values
that are static across all requests sent from the decorated
consumer method, this annotation turns a method argument into a
dynamic request header.
To define an optional header, use the default value of None
:
@get("/repositories")
def fetch_repos(self, auth: Header("Authorization") = None):
"""List all public repositories."""
When the argument is not used, the header will not be added to the request.
Source code in uplink/arguments.py
uplink.HeaderMap
uplink.Field
Defines a form field to the request body.
Use together with the decorator uplink.form_url_encoded
and annotate each argument accepting a form field with
uplink.Field
.
@form_url_encoded
@post("/users/edit")
def update_user(self, first_name: Field, last_name: Field):
"""Update the current user."""
Source code in uplink/arguments.py
uplink.FieldMap
uplink.Part
uplink.PartMap
uplink.Body
Set the request body at runtime.
Use together with the decorator uplink.json
. The method
argument value will become the request's body when annotated
with uplink.Body
.
uplink.Url
uplink.Timeout
Passes a timeout as a method argument at runtime.
While uplink.timeout
attaches static timeout to all requests
sent from a consumer method, this class turns a method argument into a
dynamic timeout value.
Example
uplink.Context
Defines a name-value pair that is accessible to middleware at runtime.
Request middleware can leverage this annotation to give users control over the middleware's behavior.
Example
Consider a custom decorator @cache
(this would be a
subclass of uplink.decorators.MethodAnnotation
):
As its name suggests, the @cache
decorator enables
caching server responses so that, once a request is cached,
subsequent identical requests can be served by the cache
provider rather than adding load to the upstream service.
Importantly, the writers of the @cache
decorators can
allow users to pass their own cache provider implementation
through an argument annotated with Context
:
@cache(hours=3)
@get("users/user_id")
def get_user(self, user_id, cache_provider: Context)
"""Retrieves a single user."""
To add a name-value pair to the context of any request made from
a Consumer
instance, you
can use the Consumer.session.context
property. Alternatively, you can annotate a constructor argument of a
Consumer
subclass with Context
,
as explained [here][annotating constructor arguments].