Rapidly develop your API clients using decorators and annotations
Project description
Rapid Api Client
Library to rapidly develop API clients based on Pydantic and Httpx using decorators and annotations.
This project is largely inspired by FastAPI.
Usage
Install the project
pip install rapid-api-client
Declare your API using decorators and annotations (the method does not need any code, it will be generated by the decorator)
class GithubIssuesApi(RapidApi):
@get("/repos/{owner}/{repo}/issues", response_class=RootModel[List[Issue]])
def list_issues(self, owner: Annotated[str, Path()], repo: Annotated[str, Path()]): ...
Use it
for issue in GithubIssuesApi().list_issues("essembeh", "rapid-api-client").root:
print("Issue:", issue)
Features
Http method
Any HTTP method can be used with http
decorator
class MyApi(RapidApi)
@http("/anything") # default is GET
def get(self): ...
@http("/anything", method="POST")
def post(self): ...
@http("/anything", method="DELETE)
def delete(self): ...
Convenient decorators are available like get
, post
, delete
, put
, patch
class MyApi(RapidApi)
@get("/anything")
def get(self): ...
@post("/anything")
def post(self): ...
@delete("/anything")
def delete(self): ...
Response class
By default methods return a httpx.Response
object and the http return code is not tested (you have to call resp.raise_for_status()
if you need to ensure the response is OK).
But you can also specify a Pydantic model class to automatically parse the response.
Note: When a
response_class
is given, theraise_for_status()
is always called to ensure the http response is OK
class User(BaseModel): ...
class MyApi(RapidApi)
# this method return a httpx.Response
@get("/user/me")
def get_user_resp(self): ...
# this method returns a User class
@get("/user/me", response_class=User)
def get_user(self): ...
Path parameters
Like fastapi
you can use your method arguments to build the api path to call.
class MyApi(RapidApi)
@get("/user/{user_id}")
def get_user(self, user_id: Annotated[int, Path()]): ...
# Path parameters dans have a default value
@get("/user/{user_id}")
def get_user(self, user_id: Annotated[int, Path()] = 1): ...
Query parameters
You can add query parameters
to your request using the Query
annotation.
class MyApi(RapidApi)
@get("/issues")
def get_issues(self, sort: Annotated[str, Query()]): ...
# Query parameters can have a default value
@get("/issues")
def get_issues_default(self, sort: Annotated[str, Query()] = "date"): ...
# Query parameters can have an alias to change the key in the http request
@get("/issues")
def get_issues_alias(self, sort: Annotated[str, Query(alias="sort-by")] = "date"): ...
Header parameter
You can add headers
to your request using the Header
annotation.
class MyApi(RapidApi)
@get("/issues")
def get_issues(self, version: Annotated[str, Header()]): ...
# Headers can have a default value
@get("/issues")
def get_issues(self, version: Annotated[str, Header()] = "1"): ...
# Headers can have an alias to change the key in the http request
@get("/issues")
def get_issues(self, version: Annotated[str, Header(alias="X-API-Version")] = "1"): ...
Body parameter
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file rapid_api_client-0.1.0.tar.gz
.
File metadata
- Download URL: rapid_api_client-0.1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.10 Linux/6.8.0-1014-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1833fd5fee4cc8332c271efc9d36e69b888ce3369d97ef3f3078070ec3fe7d26 |
|
MD5 | 91b040f5873ff632a6dfa8ac3b600648 |
|
BLAKE2b-256 | e3de1c14106117ab1df5ddefebf20c48699de2d2da422e1eaaa925a6ef46c729 |
File details
Details for the file rapid_api_client-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: rapid_api_client-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.10 Linux/6.8.0-1014-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5fbefb696ecc868820591c9bacec5df4c09b76265c7f9014c14dbc68469ebf8 |
|
MD5 | a3911d3a637aff1e52b22bfed1452f47 |
|
BLAKE2b-256 | 1556f81ead1cf5a7335f1af922d692772ea1c9e43d1546863ca35d1e372c6ed6 |