Authentication¶
Overview¶
The Authentication feature allows you to simplify the process of adding authentication to your HTTP clients.
Authentication Types¶
DeclarativeX supports the following authentication types:
Class name | Location | Description |
---|---|---|
BasicAuth |
Header | Provide username and password, it will add base64-encoded username:password into Authorization header. |
BearerAuth |
Header | Provide a token and it will add Authorization: Bearer {token} to headers. |
HeaderAuth |
Header | Provide header name and token = {header_name}: {token} . |
QueryParamsAuth |
Query | Provide a key and value, it will add it to query params: {url}?{key}={value} |
BasicAuth¶
from declarativex import BasicAuth
auth = BasicAuth(username="my_username", password="my_password")
BearerAuth¶
from declarativex import BearerAuth
auth = BearerAuth("my_token")
HeaderAuth¶
from declarativex import HeaderAuth
auth = HeaderAuth(header_name="X-My-Header", value="my_token")
QueryParamsAuth¶
from declarativex import QueryParamsAuth
auth = QueryParamsAuth(key="key", value="my_token")
Usage¶
Class-based declaration¶
from declarativex import http, BaseClient, BearerAuth
class MyClient(BaseClient):
base_url = ...
auth = BearerAuth("my_token")
@http("GET", "/users")
def get_users(self) -> dict:
...
Function-based declaration¶
from declarativex import http, BearerAuth
@http("GET", "/users", auth=BearerAuth("my_token"))
def get_users() -> dict:
...
Note
If auth declared in both class and function, the function auth will be used because it has more prioritized parameters.