Django app for send sync requests to microservices or external APIs.
Project description
Microservice-Request
Application for make sync requests (REST API) between microservices or external APIs.
If you don’t have any microservice yet, please follow the link https://github.com/bandirom/DjangoTemplateWithDocker
And deploy the project use the instruction in a link above
Quick start
- Install the package
pip install django-microservice-request
Add “microservice_request” to your INSTALLED_APPS:
INSTALLED_APPS = [ ... 'microservice_request', ]
In settings.py set the follow settings. This settings needed for set access to your project like external service:
# Custom api key header API_KEY_HEADER = os.environ.get('API_KEY_HEADER', 'X-Custom-Header') # Custom api key API_KEY = os.environ.get('API_KEY', 'your-api-secret-key') # Requested header will be: # Authorization: X-Custom-Header your-api-secret-key # Add permission to restframework block REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ( 'microservice_request.permissions.HasApiKeyOrIsAuthenticated', ), }
- Now You can handle all requests from external services which have header:
Authorization: X-Custom-Header your-api-secret-key
For key generating recommend to use https://florimondmanca.github.io/djangorestframework-api-key/
4. Sending requests to an external services:
For example you need to send request to the Payment service with some specific headers
4.1 In settings.py:
PAYMENT_API_URL = "http://host.com:8080" PAYMENT_API_KEY = "payment secret api key"
4.2 create a file services.py:
from django.conf import settings from microservice_request.services import MicroServiceConnect class PaymentService(MicroServiceConnect): service = settings.PAYMENT_API_URL api_key = settings.PAYMENT_API_KEY
4.3 in your code (for example in views.py):
from .services import PaymentService def post(self, request, *args, **kwargs): """Ths method will work as proxy""" request_method = 'post' url = '/some/api/path/' data = { 'key': 'value', } return PaymentService.microservice_response( request, reverse_url=url, method=request_method, data=data, special_headers={'IfNeed': 'SomeAdditionalHeader'} )
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-microservice-request-0.1.4.7.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a4a135d66c184600b1451258b84a57860a3e095eb0955a720b73207679e9e8a |
|
MD5 | e3d59f4bc8ce21ff1b10fae1bea0f8e1 |
|
BLAKE2b-256 | 99575dc756ee9cdff438e71a867497732e852861d915ce2fc4fe2efe15929b8a |
Hashes for django_microservice_request-0.1.4.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e793ae803c06554cf6d042cb638c4e67bf76536912051bec02b3c5e0b57760c9 |
|
MD5 | 319fd117eb7f08f888045204e05a0081 |
|
BLAKE2b-256 | 4f9babbf9bb481a19e3f126f7022d90ffbbb3a3d4b5cd839b76dd88813d5734a |