Library with REST APIs Client code for Django
Project description
Para visualizar o README em português.
Django API Client
The Django API Client is an API response wrapper, which allows you to iterate through the API as if they were using a local model / object in your project. Whether through the direct customer in an FBV or using personalized CBV (class-based displays), this framework tries to make this communication as easy as possible
Some reasons to use the Django API Client
If you work with microservices with APIs in multiple locations and want to continue using Django as a WebApp with the same capabilities to render data as if you were using native models
You want to separate your Django project to let one of them only with the API with DRF and the other as a WebApp with Templates (HTML), CSS, JS instead of using some JS frontend (ReactJS, AngularJS, etc.)
You want to use a third party API to list, create and change using the django template system
For more information, see our documentation at Github Pages
Requirements
Python 3.x
Django 2.0 or later
How to install
You can get Django API Client by using pip:
$ pip install django-api-client
If you want to install it from source, grab the git repository from GitHub and run setup.py:
$ git clone git@github.com:rhenter/django_api_client.git
$ cd django_api_client
$ python setup.py install
Settings
To enable django_api_client in your project you need to add it to INSTALLED_APPS in your projects settings.py file:
INSTALLED_APPS = (
...
'django_api_client',
...
)
Example
Also add the settings to access your API to settings.py:
DJANGO_API_CLIENT = {
'API': [
{
'NAME': 'production',
'URL_BASE': 'https://example.com/v1',
'ENDPOINTS': [
'/order/orders',
'/user/users',
...
],
'AUTHENTICATION_ACCESS_TOKEN': 'TOKEN'
},
{
'NAME': 'localhost',
'URL_BASE': 'http://localhost:8001/v1',
'ENDPOINTS': [
'/order/orders',
'/user/users',
...
],
'AUTHENTICATION_ACCESS_TOKEN': 'TOKEN'
}
]
}
Note: The details of the configuration will be better explained in the documentation
Create a clients.py file in the core folder of your project, if you haven’t, created it within your project folder to be simple to be imported from anywhere in the project with the following content:
from django_api_client.client import api_client_factory
api_client = api_client_factory('production')
- Note:
The name of this variable will be the name of the client that you can use throughout your project
It is recommended that the production use a set of configurations without configurations.py to change the simple way or the name of the API without the need to create several.
In our case, we have the option of “production” and “localhost”, the factory generates the customer according to the name used and the parameters identified in it
Now we are going to list the data using the normal Django template system
Let’s imagine which client has a project folder (folder containing the settings.py file)
from django_api_client.mixins import ClientAPIListMixin
from pasta_do_projeto.clients import api_client
class OrderListView(ClientAPIListMixin):
template_name = "template_name.html" # Path where is your template
page_title = 'Orders' # Generates a context variable to use in your template
page_base_url = reverse_lazy('order:list') # Information used in pagination, and the search
paginate_by = 50 # Number of items to generate the pagination
client_method = api_client.order.get_orders
- Note:
The client will generate a user-friendly structure for each endpoint. Example with the endpoint /order/orders/:
endpoint name: order
methods:
get_orders # GET: List
get_order # GET: Detail of a resource using an identifier
create_order # POST: Create a resource record
update_order # PUT / PATCH: Fully or partially updates a resource using an identifier
What does that mean?
That the customer will always generate the structure according to the names of their endpoints
Documentation
Check out the latest django-api-client documentation at Github Pages
Contributing
Please send pull requests, very much appreciated.
Fork the repository on GitHub.
Make a branch off of master and commit your changes to it.
Install requirements. pip install -r requirements-dev.txt
Install pre-commit. pre-commit install
Run the tests with cd test-django-project; py.test -vv -s
Create a Pull Request with your contribution
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
Hashes for django_api_client-0.3.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92e887c8bc19e1a994882eefc8c1ed047695765b5993b3bdc3ff7ee44fb41ecc |
|
MD5 | 0ab16cfee7cbb572b317fdc616e1c98c |
|
BLAKE2b-256 | 81fbcbc49226c270171995b270b4462583e3da9cf23c07d2d5330666abc24b20 |