Skip to main content

A small JSON API service template

Project description

RESTService

About

REST Service is small library. This is template for REST services. REST Service is wrapper for aiohttp framework. This library wraps class Application from framework and adds middleware for handle exceptions and make web response from it. Also throws config file to web handlers.

Installation

pip install restservice

Classes

  • RESTService - class makes aiohttp app with middleware who catchs exceptions and makes web response
  • RESTConfig - class makes config and gets params from yaml file
  • RESTHandler - class wraps aiohttp View class and throws config in handler
  • RESTError - class for generate exceptions

Example

from aiohttp.web import json_response, RouteTableDef
from restservice import RESTError, RESTService, RESTConfig, RESTHandler


routes = RouteTableDef()


# Create class Config with params as annotation
# config gets env variable ENVIRONMENT and find params in yaml file
# if config not found param with current env config gets param from DEFAULT or return None
class Config(RESTConfig):
    DB: str


# make func handler
async def func_handler(request):
    return json_response(await request.json())


# make class handler with config
@routes.view(r'/test/{user_id}')
class TestHandler(RESTHandler):
    # GET handler
    async def get(self):
        user_id = self.request.match_info.get('user_id')
        db_path = self.config.DB
        if user_id == 'exc':
            raise RESTError('USER_ID_ERROR', 'User id error.')

        return json_response({'user_id': user_id, 'db_path': db_path})


if __name__ == '__main__':
    app = RESTService()  # create aiohttp app with middleware
    app.config = Config('config.yaml')  # add config
    app.add_routes(routes)  # add route table
    app.router.add_get('/example', func_handler)  # add handler
    app.start()  # run app

example request not found page

curl http://localhost:8080/404

response

{
  "error": "HTTP_NOT_FOUND",
  "message": "Http not found.",
  "detail": null
}

Project details


Download files

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

Source Distribution

restservice-0.1.10.tar.gz (3.2 kB view details)

Uploaded Source

File details

Details for the file restservice-0.1.10.tar.gz.

File metadata

  • Download URL: restservice-0.1.10.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for restservice-0.1.10.tar.gz
Algorithm Hash digest
SHA256 3abd00a9367b91af325eaec2229ccaf929e9e31d642601d39f4815e7f5500165
MD5 a8879c3f9a2e9e2c47f7a52b6b33f1cd
BLAKE2b-256 f9bc96bd7a8d5a1612f5de97cb241acf82fe8e39fe9e426358ed1d7b5f9f3282

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page