Coder Social home page Coder Social logo

sw-rest-utils's Introduction

sw-rest-utils

REST helpers

Declaration

for example in my_app/rest.py

from django.conf import settings
from sw_rest_utils import BaseRest


class SomeResource(BaseRest):
    url = settings.OTHER_SERVICE_URL + '/rest/some_resource/'
    label = 'Super resource first'


class DynamicallyUrl(BaseRest):
    def __init__(self, pk):
        self.pk = pk

    def get_url(self):
        return settings.OTHER_SERVICE_URL + '/rest/some_resource/' + str(self.pk)


class ResourceWithGetParams(BaseRest):
    url = settings.OTHER_SERVICE_URL + '/rest/some_resource/'

    def __init__(self, some_filter_value):
        self.some_filter_value = some_filter_value

    def get_params(self):
        return {
            'some_filter_value': self.some_filter_value
        }


class ResourceWithPostParams(BaseRest):
    url = settings.OTHER_SERVICE_URL + '/rest/some_resource/'
    method = 'PUT'

    def __init__(self, some_value):
        self.some_value = some_value

    def get_data(self):
        return {
            'some_value': self.some_value
        }


class CustomResponseProcess(BaseRest):
    url = settings.OTHER_SERVICE_URL + '/rest/some_resource/'

    def process_request(self):
        result = super().process_request()
        result['total'] = result['foo'] + result['bar']
        return result


class CustomHeaders(BaseRest):
    def get_headers(self):
        return {'Authorization': 'Token {0}'.format(settings.AUTH_TOKEN)}

Usage

for example in my_app/views.py

from rest_framework.response import Response
import my_app.rest


def some_view(request):
    result = my_app.rest.SomeResource().process_request()
    value = result['some_response_value']
    return Response({'other_service_result': value})

For useing RestExceptionView add REST_EXCEPTION_PROCESSING flag at setings.py

...
USE_REST_EXCEPTION_VIEW = True
...

And add view in urls.py

from sw_rest_utils.views import RestException
...
url(r'^rest_exception/$', RestException.as_view(**{'template_name': 'app/my_template.html'}), name='rest_exception'),

...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.