Skip to main content

The simplest way to have a rest api

Project description

Lint Build and Test Upload Package Coverage Status PyPI version

py-easy-rest

It is a lib to create fast and scalable rest apis based on JSON schema in a very simple way. It is based on Sanic and it has built in extensions to add repositories and caches.

Getting Started

How to install

pip install py-easy-rest

Coding your app

It will use by default an in memory repository. It is not recommended to production applications. The next sections of this documentation will show you ways to integrate your application with Mongo DB and ways to use cache strategies. You can also create your own repositories and cache strategies to integrate your app with other technologies.

#main.py

from py_easy_rest.server import App


config = {
    "name": "Project Name",
    "schemas": [{
        "name": "Mock",
        "slug": "mock",
        "properties": {
            "name": {"type": "string"},
            "age": {"type": "integer"},
        },
        "required": ["name"],
    }]
}

pyrApp = App(config)

pyrApp.app.run(
    host='0.0.0.0',
    port=8000,
    debug=True,
    auto_reload=True,
)

Running it

python main.py

Now you can access http://localhost:8000/swagger to access your api documentation.

Integrating with Repositories

It is possible to add a repository to your application persist data into some data base. By default it will use a in memory repository, witch is not recommended to production environment.

To create your own repository, you just need to implement our Repo and pass it to the App:

pyrApp = App(config, repo=MyOwnRepo())

Mongo Repository

#main.py
from motor.motor_asyncio import AsyncIOMotorClient

from py_easy_rest.server import App
from py_easy_rest.repos.mongo import MongoRepo


config = {
    "name": "Project Name",
    "schemas": [{
        "name": "Mock",
        "slug": "mock",
        "properties": {
            "name": {"type": "string"},
            "age": {"type": "integer"},
        },
        "required": ["name"],
    }]
}

repo = MongoRepo()

pyrApp = App(config, repo=repo)

@pyrApp.app.listener('before_server_start')
def init(app, loop):
    mongo_db_instance = AsyncIOMotorClient("mongodb://localhost:27017/db")
    db = mongo_db_instance.get_default_database()
    collection = db["default"]
    repo.set_db_collection(collection)


pyrApp.app.run(
    host='0.0.0.0',
    port=8000,
    debug=True,
    auto_reload=True,
)

Integrating with Cache Strategies

It is possible to add a cache to your application. By default it will not use a cache, but you can choice a built in option or create your own cache.

To create your own cache, you just need to implement our Cache and pass it to the App:

pyrApp = App(config, cache=MyOwnCache())

Redis cache

#main.py
from py_easy_rest.server import App
from py_easy_rest.caches.redis import RedisCache


config = {
    "name": "Project Name",
    "schemas": [{
        "name": "Mock",
        "slug": "mock",
        "properties": {
            "name": {"type": "string"},
            "age": {"type": "integer"},
        },
        "required": ["name"],
    }]
}

cache = RedisCache("redis://localhost")

pyrApp = App(config, cache=cache)

pyrApp.app.run(
    host='0.0.0.0',
    port=8000,
    debug=True,
    auto_reload=True,
)

Memory cache

#main.py
from py_easy_rest.server import App
from py_easy_rest.caches.memory import MemoryCache


config = {
    "name": "Project Name",
    "schemas": [{
        "name": "Mock",
        "slug": "mock",
        "properties": {
            "name": {"type": "string"},
            "age": {"type": "integer"},
        },
        "required": ["name"],
    }]
}

cache = MemoryCache()

pyrApp = App(config, cache=cache)

pyrApp.app.run(
    host='0.0.0.0',
    port=8000,
    debug=True,
    auto_reload=True,
)

Middlewares and Listeners

An instance of a py_easy_rest.server.App has a property called app that is a Sanic app. You can use this property to add middlewares and listeners. Take a look at the docs: Middlewares, Take a look at the docs: Listeners

API Description

Properties you could pass to py_easy_rest.server.App:

Properties Required Default Description
api_config True None Object with project and schemas config
repo False MemoryRepo() Repository used as data resource
cache False DummyCache() Cache strategy
cache_list_seconds_ttl False 10 TTL to cache the list results in seconds
cache_get_seconds_ttl False 60 * 30 TTL to cache the get results

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

py-easy-rest-0.3.1.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

py_easy_rest-0.3.1-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file py-easy-rest-0.3.1.tar.gz.

File metadata

  • Download URL: py-easy-rest-0.3.1.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for py-easy-rest-0.3.1.tar.gz
Algorithm Hash digest
SHA256 f5aeaa79c4e6846d3af591e7c6b03b5efdb6ccf703d1eaca685d7dd9c079811b
MD5 3abf8008fa637df66d12a78df37c2e44
BLAKE2b-256 724b42ac397cae69a84d67fe40e00f4315807dd91ab221c4e972c2efdd990c68

See more details on using hashes here.

File details

Details for the file py_easy_rest-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: py_easy_rest-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for py_easy_rest-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d74554b27facd771c452840760fcfd0a5248946e3d90d283ea0291938a3dbb7e
MD5 2af6d6c09bb0932355678bfe9c00473c
BLAKE2b-256 31a466079fc1f9cd45018d4303fd01f72918d889052b3fde6b67605171bbad45

See more details on using hashes here.

Supported by

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