GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - python-odin/odinweb: OdinWeb toolkit for building RESTful APIs using Odin and your favourite Python web framework.

BranchesTags
Open more actions menu

Latest commit

 

History

355 Commits
355 Commits

Folders and files

NameName
Last commit message
Last commit date
odinweb
odinweb
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
.landscape.yml
.landscape.yml
 
 
.travis.yml
.travis.yml
 
 
LICENSE
LICENSE
 
 
README.rst
README.rst
 
 
requirements-dev.txt
requirements-dev.txt
 
 
requirements.txt
requirements.txt
 
 
setup.cfg
setup.cfg
 
 
setup.py
setup.py
 
 

Repository files navigation

OdinWeb

A Restful API framework for Python that uses Odin Resources with native support for Swagger and an integrated Swagger-UI.

License https://img.shields.io/pypi/v/odinweb.svg?style=flat Travis CI Status Code cov Code Health Requirements Status Gitter.im

The initial development effort currently supports:

  • Flask
  • Bottle
  • Django - Odin/Django integration is already implemented with baldr. Odin Web is an evolution of the design of baldr. Baldr still includes other integration between django and odin, once merged Baldr will be depreciated

With the following frameworks to be included once a stable API is established:

  • Retort - A Flask/Bottle like framework for AWS Lambda/API Gateway

There are no plans at this point for other libraries although I'm open to suggestions/contributions. The effort required to integrate other libraries is minimal as Odin Web was designed to be agnostic of the framework it is running within.

Note

Odin Web is being developed very much with a view to dropping support for Python 2.7 in the future, back-ported versions of several Python 3.x features are utilised in the design (eg :py:mod:`enum` :py:mod:`http`).

Installation

An early Alpha release is on PyPI, the API has however undergone significant changes since this was first put out the best option is to checkout a tagged release from GitHub until a beta is ready that will solidify the API

Install the core library:

git clone [email protected]:python-odin/odinweb.git
cd odinweb
python setup.py install

Install your preferred web framework:

git clone [email protected]:python-odin/odinweb.flask.git
# or
git clone [email protected]:python-odin/odinweb.bottle.git
# or
git clone [email protected]:python-odin/odinweb.django.git

# Change into the appropriate directory then
python setup.py install

Quickstart

Odin Web is very much oriented around Resources so first define your resources:

import odin

class User(odin.Resource):
    """
    User resource
    """
    id = odin.IntegerField()
    username = odin.StringField()
    first_name = odin.StringField()
    last_name = odin.StringField()
    email = odin.EmailField()

Next define your API:

from odinweb import api

USERS = [
    User(1, 'pimpstar24', 'Bender', 'Rodreges', '[email protected]'),
    User(2, 'zoidberg', 'Zoidberg', '', '[email protected]'),
    User(3, 'amylove79', 'Amy', 'Wong', '[email protected]'),
]
USER_ID = len(USERS)


class UserApi(api.ResourceApi):
    resource = User
    tags = ['user']

    @api.listing
    def get_user_list(self, request, offset, limit):
        return USERS[offset:offset+limit], len(USERS)

    @api.create
    def create_user(self, request, user):
        global USER_ID

        # Add user to list
        USER_ID += 1
        user.id = USER_ID
        USERS.append(user)

        return user

    @api.detail
    def get_user(self, request, resource_id):
        """
        Get a user object
        """
        for user in USERS:
            if user.id == resource_id:
                return user

        raise api.Error.from_status(api.HTTPStatus.NOT_FOUND)

    @api.delete
    def delete_user(self, request, resource_id):
        for idx, user in enumerate(USERS):
            if user.id == resource_id:
                USERS.remove(user)
                return api.create_response(200)

        raise api.Error.from_status(api.HTTPStatus.NOT_FOUND)

This defines an API for listing, fetching and creating a users.

Finally hookup to your web framework, in this case Flask and enable swagger spec:

from flask import Flask
from odinweb.flask import ApiBlueprint
from odinweb.swagger import SwaggerSpec

app = flask.Flask(__name__)

app.register_blueprint(
    ApiBlueprint(
        # Use an API version
        api.ApiVersion(
            SwaggerSpec('Flask Example API', enable_ui=True),  # Support for Swagger!
            UserApi(),
        ),
        debug_enabled=True,  # Enable debug output
    ),
)

Start the flask app and you can browse to the swagger UI to try out the API:

http://localhost:5000/api/v1/swagger/ui

CORS

To enable CORS your API Interface (for Flask this is ApiBlueprint) needs to be wrapped with the CORS wrapper eg:

from flask import Flask
from odinweb.flask import ApiBlueprint
from odinweb.cors import CORS, AnyOrigin

app = flask.Flask(__name__)

app.register_blueprint(
    CORS(
        ApiBlueprint(
            api.ApiVersion(
                UserApi()
            )
        ),
        origins=AnyOrigin
    )
)

For customisation the CORS class can easily be inherited to customise how the origin is determined (handy if your application is behind a reverse proxy). The CORS wrapper also accepts max_age, allow_credentials, expose_headers and allow_headers options.

About

OdinWeb toolkit for building RESTful APIs using Odin and your favourite Python web framework.

Topics

apibottledjangoflaskodinpythonrestful

Resources

Readme
BSD-3-Clause license
Activity
Custom properties

Stars

3 stars

Watchers

0 watching

Forks

2 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.