Skip to main content

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

  1. Run pip install django-easy-services

  2. Add “services” to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = [
            ...
            'services',
    ]
    
  3. Run python manage.py migrate to create the services models.

  4. 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


Download files

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

Files for django-easy-services, version 0.3
Filename, size File type Python version Upload date Hashes
Filename, size django_easy_services-0.3-py3.8.egg (15.4 kB) File type Egg Python version 0.3 Upload date Hashes View
Filename, size django-easy-services-0.3.tar.gz (7.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page