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

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

Uploaded Source

Built Distribution

py_easy_rest-0.2.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py-easy-rest-0.2.0.tar.gz
  • Upload date:
  • Size: 11.6 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.2.0.tar.gz
Algorithm Hash digest
SHA256 f58e6c1fab6d8c910c793167029d06e044d47cad8a322e5e779b5d7a756a4d49
MD5 0be2708ea73e43fffe4bb1ee7f2a7741
BLAKE2b-256 3acf041588bac0f1c0865658e9d5cfb138594e6d0224ff24022056310beb2c85

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_easy_rest-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 14.9 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f0cc31693e0011195802150e8cb7fe101df6047c385f4c7c95e7e4cccb115d87
MD5 04de42c404aa24ff7b7c7af3da3982b7
BLAKE2b-256 623c80c4ad2473dc5d8263111eede08035c69d9689bc4dba936c11c17b603f5f

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