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.2.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: py-easy-rest-0.3.2.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.2.tar.gz
Algorithm Hash digest
SHA256 1caf6ae06c2fbd75ea10bd3fb9063659c3737e7a10eb661e8e9a996cc9caf630
MD5 9c1271695833551318922fe2c9817901
BLAKE2b-256 00167be6defb13458e51ee2bf1b87ecb703873bd77a180cd9110e96cf62915fc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_easy_rest-0.3.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0c89d3d1d5440bdb4efc3670904414b3dee60b9abc0b8b36d131e785d196f51d
MD5 36bc58bfadee2d14285bc6b1acbbce11
BLAKE2b-256 d3a4d9580956f3408da29f023b339abf34c05cc5dc98911b60d8cb0404c5ada0

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