REST API Consumer
This API consumer was originally created to easily consume a Django REST Framework API from a PyQT application on a Raspberry Pi. This, with the goal to achieve this in a similar way to Django ORM use.
With Python_API_Consumer, you can consume easily numerous API from any provider, in a easier way than a query a database. Nothing to learn, nothing to configure, you can just use it as shown below. This module do this for you:
- Build URL
- Format queries
- Send payloads
- Use appropriate REST methods
- Hydrate objects
- Handle errors
- Log errors
It will consume any REST API URL tree in a near future. And probably not only JSON format.
- asyncio
- requests
- functools
- inspect
- logging
class User(Model):
pass
user = User(url="https://example.org/api/user")
# GET https://example.org/api/user/5?format=json and hydrate instance
user.get(id=5)
# Give you the first name of this user id 5
print(user.first_name)
class Foo(Model):
item = "bar"
foo = Foo("ttps://example.org/api/bar")
limit = 10
# GET https://example.org/api/bar?format=json&limit=10 and create 10 hydrated instances of Foo from api/bar/
many_foo = foo.from_query(
options=[f"limit={limit}"],
limit=limit,
model_class=Foo
)
# The use of limit=limit parameter is used to add instances beyond the DRF page_size configuration.
# model_class=Foo is optional because foo is an instance of Foo
# many_foo is a list of 10 instances of Foo class
user.fisrt_name = "Alice"
user.save()
account = User("https://example.org/api/user")
account.fisrt_name = "Bob"
account.email = "[email protected]"
account.save()
account.delete()
You need any development, please create an issue or submit a pull request :) Enjoy !