Skip to main content

Tools for setting up an API. Built on the FastAPI framework

Project description

coopapi

package for setting up a quick API built on the FastAPI framework

example

Setup a dummy pydantic dataclass that has some attributes

from pydantic.dataclasses import dataclass as pydandataclass, Field
from cooptools.randoms import a_string, a_phrase

class DummySchemaConfig:
    allow_population_by_field_name = True
    schema_extra = {
        "example": {
            'id': a_string(10),
            'desc': a_phrase(10),
            'active': True,
        }
    }

@pydandataclass(config=DummySchemaConfig)
class DummySchema:
    id: str = Field(...)
    desc: str = Field(...)
    active: bool = Field(...)

Using the dummySchema pydantic dataclass, stubb in some callbacks and create the api shell object. This will be used to setup the core funcationality of our API. Create a FastAPI() object that is used to host our router.

Note: multiple routers (shells) can be added to the same FastAPI() object such that you can build out an extensive API.

Finally, serve the app...

from examples.dummySchema import DummySchema
from coopapi import createRequestCallback, ApiShell, deleteRequestCallback
import uvicorn
from fastapi import FastAPI

# create the callbacks for what should be done when the various endpoints are called. This is likely a
# db interaction or a forwarding request.
create_callback: createRequestCallback = lambda r, t: t
delete_callback: deleteRequestCallback = lambda r, t: True

# setup the api itself, use a base FastAPI object and then an api_shell which has its router included in the
# base api
app = FastAPI()
api_shell = ApiShell(target_schema=DummySchema,
                     base_route='/dummy',
                     on_create_callback=create_callback,
                     on_delete_callback=delete_callback)
app.include_router(api_shell.router, tags=["dummy_shell"])


# serve the app
uvicorn.run(app, port=1219)

You can visit the docs site of the created api at http://localhost:1219/docs#/

img.png

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

coopapi-0.7.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

coopapi-0.7-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file coopapi-0.7.tar.gz.

File metadata

  • Download URL: coopapi-0.7.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for coopapi-0.7.tar.gz
Algorithm Hash digest
SHA256 72a0d20c69ca86bc5c1ccd8e341d601aa1c32c8d818e1eaaad90aa0ddbbb4d28
MD5 4c9fcd212e95d14ae72dbdc558785997
BLAKE2b-256 d718b8fe125b4c9554279ca12c3ac0cb08170b65b94b4210e5df819422d5ce53

See more details on using hashes here.

File details

Details for the file coopapi-0.7-py3-none-any.whl.

File metadata

  • Download URL: coopapi-0.7-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for coopapi-0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 0bafd7fbc7f85acf796e97f9c2b998cefd66d2556c96c294fc50fb0623c36fc7
MD5 e3d806d26723a07e309b8ff3765caf4f
BLAKE2b-256 1c78edc05d726f31bb35caf3fda226e3fd2541593b93c973f87398e8c73b3b69

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