项目作者: paolorechia

项目描述 :
Rest API Client
高级语言: Python
项目地址: git://github.com/paolorechia/rest-api-client.git
创建时间: 2021-07-10T21:51:40Z
项目社区:https://github.com/paolorechia/rest-api-client

开源协议:MIT License

下载


Python Rest API Client

pypi
coverage


Glues 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) Spec
  • Import Postman Collection
  • Export generated source code

Installation

  1. pip install py_rest_api_client

Usage

Here’s a set of examples you can use to understand how to use the library.
They all correspond to test cases under test/integration/.

Please note that authentication in general is provided by httpx_auth,
with the exception of a simple Bearer token.

Notion - Bearer Token Auth

  1. auth = BearerHeaderToken(bearer_token=notion_token)
  2. client = httpx.Client(auth=self.auth)
  3. api = RestAPI(
  4. api_url="https://api.notion.com/v1",
  5. driver=client,
  6. endpoints=[
  7. Endpoint(name="search", path="/search", method=HTTPMethod.POST),
  8. ],
  9. custom_headers={"Notion-Version": "2021-05-13"},
  10. )
  11. api.search(data={"query": "Test page"})

Pantry

  1. api = RestAPI(
  2. api_url="https://getpantry.cloud/apiv1",
  3. driver=client,
  4. # One can also pass endpoints to the constructor
  5. endpoints=[
  6. Endpoint(name="get_pantry", path="/pantry/{pantry_id}"),
  7. Endpoint(
  8. name="get_basket",
  9. # Path parameters can be provided in the format {variable_name}
  10. path="/pantry/{pantry_id}/basket/{basket_id}",
  11. ),
  12. Endpoint(
  13. # 'create_' is interpreted as POST
  14. name="create_basket",
  15. path="/pantry/{pantry_id}/basket/{basket_id}",
  16. ),
  17. Endpoint(
  18. # 'update_' is interpreted as PUT
  19. name="update_basket",
  20. path="/pantry/{pantry_id}/basket/{basket_id}",
  21. ),
  22. Endpoint(
  23. name="delete_basket",
  24. path="/pantry/{pantry_id}/basket/{basket_id}",
  25. ),
  26. ],
  27. )
  28. pantries = api.get_pantry(pantry_id="123")
  29. # Create/update/patch methods have an additional parameter called data
  30. # Which is passed as the BODY of the request
  31. api.create_basket(pantry_id="123", basket_id="234", data={"key": "value"})
  32. api.update_basket(pantry_id="123", basket_id="234", data={"key2": "value2"})
  33. basket = api.get_basket(pantry_id="123", basket_id="234")
  34. api.delete_basket(pantry_id="123", basket_id="234")

If pantry supported PATCH, we would declare the endpoint as:

  1. Endpoint(
  2. name="patch_basket", # No alias for patch exists
  3. path="/pantry/{pantry_id}/basket/{basket_id}",
  4. ),

Async methods

By default, async methods are created with the prefix _async.
For instance:

  1. await api.async_get_pantry(pantry_id="123")
  2. await api.async_create_basket(
  3. pantry_id="123", basket_id="234", data={"key": "value"}
  4. )
  5. await api.async_update_basket(
  6. pantry_id="123", basket_id="234", data={"key2": "value2"}
  7. )
  8. await api.async_get_basket(pantry_id="123", basket_id="234")
  9. await api.async_delete_basket(pantry_id="123", basket_id="234")

Chuck Norris

  1. CHUCK_BASE_URL = "https://api.chucknorris.io/jokes"
  2. # Imports
  3. from pydantic import BaseModel, HttpUrl
  4. from rest_api_client.lib import RestAPI, Endpoint, HTTPMethod
  5. import httpx
  6. # Optionally declare your model classes
  7. class JokeModel(BaseModel):
  8. id: str
  9. created_at: str
  10. updated_at: str
  11. icon_url: str
  12. categories: list
  13. url: str
  14. value: str
  15. # Declare your API endpoints
  16. endpoints = [
  17. # Fully descriptive declaration.
  18. Endpoint(
  19. name="get_joke",
  20. path="/random",
  21. method=HTTPMethod.GET,
  22. model=JokeModel,
  23. query_parameters=[("category", str)],
  24. ),
  25. # No model provided, result comes back as a dictionary.
  26. Endpoint(
  27. name="get_categories",
  28. path="/categories",
  29. method=HTTPMethod.GET,
  30. ),
  31. # Omit HTTP Method, it gets inferred from the endpoint name.
  32. Endpoint(name="get_search", path="/search", query_parameters=[("query", str)]),
  33. ]
  34. # Instantiate your HTTP client session. Should also work with requests
  35. with httpx.Client() as client:
  36. api = RestAPI(api_url=CHUCK_BASE_URL, driver=client)
  37. api.register_endpoints(endpoints)
  38. joke = api.call_endpoint("get_joke")
  39. joke2 = api.get_joke()
  40. categories = api.get_categories()
  41. search = api.get_search(query="something")
  42. print(joke)
  43. print(joke2)
  44. print(search)