- Notifications
You must be signed in to change notification settings - Fork3
The Django API Client is an API response wrapper, which translates Django's native calls when using a view to a particular REST API. Whether using the API client directly in a FBV (Function-based View) or using CBV (Class-based View), this library makes this communication as transparent and easy as possible
License
rhenter/django-api-client
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The Django API Client is an API response wrapper, which translates Django's native calls when using a view to a particular REST API. Whether using the API client directly in a FBV (Function-based View) or using CBV (Class-based View), this library makes this communication as transparent and easy as possible
Some reasons to use theDjango 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 atGithub Pages
- Python 3.x
- Django 2.0 or later
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
To enable django_api_client in your project you need to add it to INSTALLED_APPS in your projectssettings.py file:
INSTALLED_APPS= ( ...'django_api_client', ...)
- Also add the settings to access your API to settings.py:
DJANGO_API_CLIENT= {'API': [ {'NAME':'production','BASE_URL':'https://example.com','ENDPOINTS': ['/v1/order/orders','/v1/user/users', ... ],'AUTHENTICATION_ACCESS_TOKEN':'TOKEN' }, {'NAME':'localhost','BASE_URL':'http://localhost:8001','ENDPOINTS': ['/v1/order/orders','/v1/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:
fromdjango_api_client.clientimportapi_client_factoryapi_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 is located in a folder called clients on project folder (folder containing the settings.py file)
fromdjango_api_client.mixinsimportClientAPIListMixinfrompasta_do_projeto.clientsimportapi_clientclassOrderListView(ClientAPIListMixin):template_name="template_name.html"# Path where is your templatepage_title='Orders'# Generates a context variable to use in your templatepage_base_url=reverse_lazy('order:list')# Information used in pagination, and the searchpaginate_by=50# Number of items to generate the paginationclient_method=api_client.order.orders.list
Note
The client will generate a user-friendly structure for each endpoint. Example with the endpoint/order/orders/:
In your template you can use the forms and pagination snippets. E.g:
{%content%}...<divclass="card card-navy card-outline"> <divclass="card-header"> <h3class="card-title">{%trans"Order List"%} : <smallclass="text-muted">{{ paginator.count }}</small> </h3>{%include"includes/form_paginate_by.html"withpaginate_by=paginate_byrange_pagination=range_pagination%}{%include"includes/form_search.html"withsearch=search%} </div> <divclass="card-body table-responsive p-0"> <tableclass="table table-bordered table-hover table-striped"id="list-content"> <thead> <tr> <th>{%trans'Code'%}</th> <th>{%trans'Customer'%}</th> <th>{%trans'Product'%}</th> </tr> </thead> <tbodyclass="text-gray">{%fororderinobject_list%} <tr> <td><ahref="{% url 'order:detail' pk=order.id %}" </a> </td> <td>{{ order.id }}</td> <td>{{ order.customer.name|title }}</td> <td>{{ order.product.name|title }}</td> </tr>{%endfor%}{%endif%} </tbody> </table> </div> <divclass="card-footer">{%ifobject_list|length != 0ornotobject_list%}{%include"includes/list_paginator.html"withpage_obj=page_objpaginator=paginator%}{%endif%} </div></div>
Note
- Example using Bootstrap Styles(CSS)
- includes/form_search.html: Form with search input. This
include
support placeholder too. - includes/form_paginate_by.html: Select form to choose how many elements the page will be paginate. Ex: by (20, 40, 60, etc ...)
- includes/list_paginator.html: Block with pagination elements with the number of pages buttons, previous and next
endpoint name: ordermethods: - list # GET: List - get # GET: Detail of a resource using an identifier - create # POST: Create a resource record - update # PUT / PATCH: Fully or partially updates a resource using an identifier - delete # DELETE: delete a record in a resource using an identifier
Hint
- What does that mean?
- That the API Client will always generate the structure according to the names of their endpoints
Check out the latestdjango-api-client
documentation atGithub Pages
Please send pull requests, very much appreciated.
- Fork therepository 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
About
The Django API Client is an API response wrapper, which translates Django's native calls when using a view to a particular REST API. Whether using the API client directly in a FBV (Function-based View) or using CBV (Class-based View), this library makes this communication as transparent and easy as possible