Skip to main content

Library with REST APIs Client code for Django

Project description

Para visualizar o README em português.

Django API Client

PyPI latest PyPI Version PyPI License CicleCI Status Coverage Docs Open Source? Yes!

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',
        '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'
    }
  ]
}
  • 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')
  • 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.orders.list

In your template you can use the forms and pagination snippets. E.g:

{% content %}

...
<div class="card card-navy card-outline">
  <div class="card-header">
    <h3 class="card-title">
      {% trans "Order List" %} : <small class="text-muted">{{ paginator.count }}</small>
    </h3>
    {% include "includes/form_paginate_by.html" with paginate_by=paginate_by range_pagination=range_pagination %}
    {% include "includes/form_search.html" with search=search %}
  </div>
  <div class="card-body table-responsive p-0">
    <table class="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>
      <tbody class="text-gray">
      {% for order in object_list %}
        <tr>
          <td><a href="{% 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>
  <div class="card-footer">
    {% if object_list|length != 0 or not object_list %}
      {% include "includes/list_paginator.html" with page_obj=page_obj paginator=paginator %}
    {% endif %}
  </div>
</div>
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.

  1. Fork the repository on GitHub.

  2. Make a branch off of master and commit your changes to it.

  3. Install requirements. pip install -r requirements-dev.txt

  4. Install pre-commit. pre-commit install

  5. Run the tests with cd test-django-project; py.test -vv -s

  6. Create a Pull Request with your contribution

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-api-client-0.3.7.tar.gz (67.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_api_client-0.3.7-py3-none-any.whl (20.9 kB view details)

Uploaded Python 3

File details

Details for the file django-api-client-0.3.7.tar.gz.

File metadata

  • Download URL: django-api-client-0.3.7.tar.gz
  • Upload date:
  • Size: 67.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.0

File hashes

Hashes for django-api-client-0.3.7.tar.gz
Algorithm Hash digest
SHA256 93d24470e75244d37bc42bc10caf700197531be08d642927fec3c52d6cfa8fdb
MD5 b543badd4a88a6732b0b4f9b66557757
BLAKE2b-256 e033b9d52f99d49531539f4be480ff5af05f9429c5046581bfb6273f2fc4e33b

See more details on using hashes here.

File details

Details for the file django_api_client-0.3.7-py3-none-any.whl.

File metadata

  • Download URL: django_api_client-0.3.7-py3-none-any.whl
  • Upload date:
  • Size: 20.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.0

File hashes

Hashes for django_api_client-0.3.7-py3-none-any.whl
Algorithm Hash digest
SHA256 0c161729975e65929948c5b83c40f77129ff36c834b940990e17e7d570ee7fac
MD5 2dba18c095db7fd75f542faa28669ac0
BLAKE2b-256 aa0246b0db312ed702ba0f0e48996d4cdc2c463c0f80502765ca7394874c7cc7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page