A simple Django app to handle services request.
Project description
Services is a simple Django app to consume json services in a simple way. You can make requests directly, async or recursive using failover services automatically while the services return errors. Example:
from services.models import Service service_example = Service(name='simple_service', method='get', url='http://example.com/?query_param=<data>') url_data = {'<data>': 'example_data'} service_response = service_example.request(url_data=get_data) # By default http responses higher than 400 are not consider as successful, # this can we tweaked in the service instance by adding codes to accepted_codes or rejected_codes. if service_response['success']: response_content = service_response['content'] # content is a dict print(response_content)
Post example:
from services.models import Service service_example = Service(name='simple_service', method='post', url='http://example.com/', headers='{"Content-Type": "<content_type>"}', parameters= '{"query_param": "<data>"}') get_data = {'<data>': 'example_data'} header_data = {'<content_type>': 'application/json'} service_response = service_example.request(get_data=get_data, header_data=header_data) if service_response['success']: response_content = service_response['content'] # content is a dict print(response_content)
Or simpler:
from services.models import Service # This headers will be use for every request to this service service_example = Service(name='simple_service', method='post', url='http://example.com/', headers='{"Content-Type": "application/json"}') # parameters will be put directly to the body of the post request to the service parameters = {'query_param': 'example_data'} service_response = service_example.request(parameters=parameters) if service_response['success']: response_content = service_response['content'] # content is a dict print(response_content)
You can also configure failover services, so if the primary service return some error code (for now >= 400) the failover/s will we call one by one untill some of them return a non error code:
from services.models import Service, ServiceFailover service_01 = Service(name='registration_precheck', method='get', url='http://example_01.com/?query_param=<data>').save() service_02 = Service(name='registration_precheck', method='get', url='http://example_02.com/?query_param=<data>').save() service_03 = Service(name='registration_precheck', method='get', url='http://example_03.com/?query_param=<data>').save() ServiceFailover(service=service_01, failover=service_02, order=1).save() ServiceFailover(service=service_01, failover=service_03, order=2).save() url_data = {'<data>': 'example_data'} # Now if service_01 fails service_02 will be execute with the same context data, # the same for service_03 if service_02 fails in this call. service_response = service_example.request_recursive(url_data=get_data) if service_response['success']: response_content = service_response['content'] # content is a dict print(response_content)
If you want to publish information to some service but you don’t want to wait you can make async request:
from services.models import Service service_example = Service(name='simple_service', method='post', url='http://example.com/', headers='{"Content-Type": "application/json"}') parameters = {'query_param': 'example_data'} # max_retry is the amount of retries do you want to execute the request while the answer is a error (0 if you want to retry "forever"). # retry_interval is the amount of seconds you want to wait between retries. service_example.request_async(max_retry=10, retry_interval=1, parameters=parameters)
Quick start
Run pip install django-easy-services
Add “services” to your INSTALLED_APPS setting like this:
INSTALLED_APPS = [ ... 'services', ]
Run python manage.py migrate to create the services models.
Start the development server and visit http://127.0.0.1:8000/admin/services to create a service (you’ll need the Admin app enabled).
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_easy_services-0.3-py3.8.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55d58251df1e84d8c79a5da6700a9eb548bd15b9a3d90f88af80d9ab1d5ef2d3 |
|
MD5 | 284306edf4be221e71db6f057268e027 |
|
BLAKE2b-256 | d0d98ecaf0647940d6144615723c8532a5cdef083fdb0cd7d191dc6b843e0204 |