HTTP Component¶
A component used in ahttp_client are Query, Header, Path, and Body. In the HTTP request process in ahttp_client, a component defines as typing.Annotated.
When a list_repository_activites method is called, the request is called as shown below.
1@Session.single_session("https://api.github.com/")
2@request("GET", "/repos/{owner}/{repo}/activity")
3def list_repository_activites(
4 session,
5 owner: Annotated[str, Path],
6 repo: Annotated[str, Path],
7 activity_type: Annotated[str, Query],
8 authorization: Annotated[str, Header]
9) -> dict[str, Any]:
10 return await response.json()
11
12await list_repository_activites(
13 owner="gunyu1019",
14 repo="ahttp_client",
15 activity_type="push",
16 authorization="Bearer <GITHUB TOKEN>"
17)
18
19# curl https://api.github.com/repos/gunyu1019/ahttp_client/activity?activity_type=push \
20# -H "authorization: Bearer <GITHUB TOKEN>"
Note
An HTTP Component can be defined as typing.Union instead of typing.Annotated.
@Session.single_session("https://api.github.com/")
@request("GET", "/repos/{owner}/{repo}/activity")
def list_repository_activites(
session,
owner:str | Path,
repo: str | Path,
activity_type: str | Query,
authorization: str | Header # or, authorization: typing.Union[str, Header]
) -> dict[str, Any]:
return await response.json()
However, using typing.Annotated is recommended to follow correct Python syntax.
Usage by Component¶
Header - Configure the header for the HTTP component.
Body - Configure the body for the HTTP component.
Form - Configure the body with aiohttp.FormData
BodyJson - Configure the body with Dictionaries
Query - Insert a value inside the URL parameter.
Path - Insert the specified value(s) inside the path placeholder. The path placeholder is using curly brackets: {}
Warning
Defining the Body parameter at HTTP request decoration can only be of one of the following types: Body, BodyJson, or Form. If more than one Component is used, a TypeError can be thrown.
Custom Component Name¶
According to PEP 8 rules, the parameter names of method must be lowercase, with words separated by underscores. (reference)
The queries or headers in an API can be named in Camel Case, or Pascal Case.
To follow the PEP 8 rules as described, an ahttp_client package provides a custom component name.
1@Session.single_session("https://api.github.com/")
2@request("GET", "/repos/{owner}/{repo}/activity")
3def list_repository_activites(
4 session,
5 owner: Annotated[str, Path],
6 repo: Annotated[str, Path],
7 activity_type: Annotated[str, Query],
8 token: Annotated[str, Header.custom_name("Authorization")]
9) -> dict[str, Any]:
10 return await response.json()
11
12await list_repository_activites(
13 owner="gunyu1019",
14 repo="ahttp_client",
15 activity_type="push",
16 token="Bearer <GITHUB TOKEN>"
17)
18
19# curl https://api.github.com/repos/gunyu1019/ahttp_client/activity?activity_type=push \
20# -H "Authorization: Bearer <GITHUB TOKEN>"
As in the example above, insert the GITHUB API TOKEN in token argument of list_repository_activites method. During the calling process, the key of Header has been overridden to “Authorization”.
Warning
The custom component name feature only supports Header, Query, BodyJson, and Form. Using it in other components may cause a TypeError.