Package: py-rest-api-client
Python Rest API ClientGlues pydantic and httpx to provide a simple REST API client, with dynamic generated methods.
It supports both synchronous and asynchronous formats.Roadmap:Auto generate models from OpenAPI (3) SpecExport generated source codeThird-Party Cloud Secret Manager SupportInstallationpipinstallpy_rest_api_clientUsageHere's a set of examples you can use to understand how to use the library.
They all correspond to test cases undertest/integration/.Please note that authentication in general is provided byhttpx_auth,
with the exception of a simple Bearer token.Notion - Bearer Token Authauth=BearerHeaderToken(bearer_token=notion_token)client=httpx.Client(auth=self.auth)api=RestAPI(api_url="https://api.notion.com/v1",driver=client,endpoints=[Endpoint(name="search",path="/search",method=HTTPMethod.POST),],custom_headers={"Notion-Version":"2021-05-13"},)api.search(data={"query":"Test page"})Pantryapi=RestAPI(api_url="https://getpantry.cloud/apiv1",driver=client,# One can also pass endpoints to the constructorendpoints=[Endpoint(name="get_pantry",path="/pantry/{pantry_id}"),Endpoint(name="get_basket",# Path parameters can be provided in the format {variable_name}path="/pantry/{pantry_id}/basket/{basket_id}",),Endpoint(# 'create_' is interpreted as POSTname="create_basket",path="/pantry/{pantry_id}/basket/{basket_id}",),Endpoint(# 'update_' is interpreted as PUTname="update_basket",path="/pantry/{pantry_id}/basket/{basket_id}",),Endpoint(name="delete_basket",path="/pantry/{pantry_id}/basket/{basket_id}",),],)pantries=api.get_pantry(pantry_id="123")# Create/update/patch methods have an additional parameter called data# Which is passed as the BODY of the requestapi.create_basket(pantry_id="123",basket_id="234",data={"key":"value"})api.update_basket(pantry_id="123",basket_id="234",data={"key2":"value2"})basket=api.get_basket(pantry_id="123",basket_id="234")api.delete_basket(pantry_id="123",basket_id="234")If pantry supported PATCH, we would declare the endpoint as:Endpoint(name="patch_basket",# No alias for patch existspath="/pantry/{pantry_id}/basket/{basket_id}",),Async methodsBy default, async methods are created with the prefix_async.
For instance:awaitapi.async_get_pantry(pantry_id="123")awaitapi.async_create_basket(pantry_id="123",basket_id="234",data={"key":"value"})awaitapi.async_update_basket(pantry_id="123",basket_id="234",data={"key2":"value2"})awaitapi.async_get_basket(pantry_id="123",basket_id="234")awaitapi.async_delete_basket(pantry_id="123",basket_id="234")Chuck NorrisCHUCK_BASE_URL="https://api.chucknorris.io/jokes"# ImportsfrompydanticimportBaseModel,HttpUrlfromrest_api_client.libimportRestAPI,Endpoint,HTTPMethodimporthttpx# Optionally declare your model classesclassJokeModel(BaseModel):id:strcreated_at:strupdated_at:stricon_url:strcategories:listurl:strvalue:str# Declare your API endpointsendpoints=[# Fully descriptive declaration.Endpoint(name="get_joke",path="/random",method=HTTPMethod.GET,model=JokeModel,query_parameters=[("category",str)],),# No model provided, result comes back as a dictionary.Endpoint(name="get_categories",path="/categories",method=HTTPMethod.GET,),# Omit HTTP Method, it gets inferred from the endpoint name.Endpoint(name="get_search",path="/search",query_parameters=[("query",str)]),]# Instantiate your HTTP client session. Should also work with requestswithhttpx.Client()asclient:api=RestAPI(api_url=CHUCK_BASE_URL,driver=client)api.register_endpoints(endpoints)joke=api.call_endpoint("get_joke")joke2=api.get_joke()categories=api.get_categories()search=api.get_search(query="something")print(joke)print(joke2)print(search)