GithubHelp home page GithubHelp logo

foobarquaxx / django-roa Goto Github PK

View Code? Open in Web Editor NEW

This project forked from charles-vdulac/django-roa

0.0 1.0 0.0 1.63 MB

Fork of original Django ROA lib. Now ROA works directly with an API like Django Rest Framework

License: BSD 3-Clause "New" or "Revised" License

django-roa's Introduction

Django-ROA (Resource Oriented Architecture)

Use Django's ORM to model remote API resources.

Fork of original David Larlet Django ROA lib. Now ROA works directly with an API like Django Rest Framework

How does it works: Each time a request is passed to the database, the request is intercepted and transformed to an HTTP request to the remote server with the right method (GET, POST, PUT or DELETE) given the get_resource_url_* methods specified in the model's definition.

Documentation

Initial documentation:

Installation

$ pip install -e git+https://github.com/charles-vdulac/django-roa/.git@master#egg=django_roa

Fork getting started

If you have an API output like this (typical DRF output):

# GET http://api.example.com/articles/
# HTTP 200 OK
# Content-Type: application/json
# Vary: Accept
# Allow: GET, POST, HEAD, OPTIONS

{
  "count": 3,
  "next": null,
  "previous": null,
  "results": [
      {
          "id": 1,
          "headline": "John's first story",
          "pub_date": "2013-01-04",
          "reporter": {
              "id": 1,
              "account": {
                  "id": 1,
                  "email": "[email protected]"
              },
              "first_name": "John",
              "last_name": "Smith"
          }
      },
      ...
  ]
}

Your code will look like this:

from django.db import models
from django_roa import Model as ROAModel

class Article(ROAModel):
    id = models.IntegerField(primary_key=True)  # don't forget it !
    headline = models.CharField(max_length=100)
    pub_date = models.DateField()
    reporter = models.ForeignKey(Reporter, related_name='articles')

    api_base_name = 'articles'

    @classmethod
    def serializer(cls):
        from .serializers import ArticleSerializer
        return ArticleSerializer

    @classmethod
    def get_resource_url_list(cls):
        return u'http://api.example.com/{base_name}/'.format(
            base_name=cls.api_base_name,
        )

    def get_resource_url_count(self):
        return self.get_resource_url_list()
from rest_framework import serializers
from .models import Article

class ArticleSerializer(serializers.ModelSerializer):
    reporter = ReporterSerializer()
    class Meta:
        model = Article
        fields = ('id', 'headline', 'pub_date', 'reporter')

Refer to tests for full example.

Running tests

django-roa's People

Contributors

davidbgk avatar charles-vdulac avatar xordoquy avatar nchilada avatar wyplay-portal avatar jocelyndelalande avatar

Watchers

Med avatar

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.