Skip to main content

Library for building web services using applipy

Project description

Applipy HTTP

pip install applipy_http

Applipy HTTP is a library that implements an AppHandle that starts a HTTP server, using aiohttp.

Basic Usage

Applipy HTTP is designed to be used by declaring the HTTP servers in the application configuration file and registering APIs to those servers.

First, lets declare a couple of HTTP servers, one anonymous and one with name demo:

# dev.yaml

app:
    name: http-demo
    modules:
        - applipy_http.HttpModule

http:
    host: 0.0.0.0
    port: 8080

    demo:
        host: 0.0.0.0
        port: 8081

Running applipy with the configuration above will result in an application that exposes two HTTP servers: one at 0.0.0.0:8080 and the other at 0.0.0.0:8081.

We can now register APIs to those servers and endpoints to the APIs. We do that by implementing them and binding them in modules.

Example

First, lets declare an API that has an endpoint /hello that returns Hello World! on GET and register that API to the anonymous HTTP server:

# hello.py

from aiohttp import web
from applipy import Module
from applipy_http import Api, Context, Endpoint, HttpModule, PathFormatter
from applipy_inject import with_names


# Endpoint implementation
class HelloEndpoint(Endpoint):

    def __init__(self):
        self.name = 'World'

    # handler for HTTP method GET
    async def get(self, request: web.Request, context: Context) -> web.StreamResponse:
        return web.Response(body=f'Hello {self.name}!')

    # handler for HTTP method POST
    async def post(self, request: web.Request, context: Context) -> web.StreamResponse:
        self.name = await request.text()
        return web.Response(body='Success')

    # path of the endpoint
    def path(self) -> str:
        return '/hello'


class HelloModule(Module):

    def configure(self, bind, register):
        bind(Endpoint, HelloEndpoint, name='hello')
        bind(PathFormatter, name='hello')

        # here we register the API to the anonymous HTTP server
        # (name argument in bind() is not set)
        bind(with_names(Api, 'hello'))

    @classmethod
    def depends_on(cls):
        return HttpModule,

Next, lets declare a second API with an endpoint /bye that returns Good Bye! on GET and register it to the demo HTTP server:

# goodbye.py

from aiohttp import web
from applipy import Module
from applipy_http import Api, Context, Endpoint, HttpModule, PathFormatter
from applipy_inject import with_names


class GoodByeEndpoint(Endpoint):

    async def get(self, request: web.Request, context: Context) -> web.StreamResponse:
        return web.Response(body="Good Bye!")

    def path(self) -> str:
        return '/bye'


class GoodByeModule(Module):

    def configure(self, bind, register):
        bind(Endpoint, GoodByeEndpoint, name='bye')
        bind(PathFormatter, name='bye')

        # here we register the API to the `demo` HTTP server
        # (name argument in bind() is set to `demo`)
        bind(with_names(Api, 'bye'), name='demo')

    @classmethod
    def depends_on(cls):
        return HttpModule,

Finally, lets update the configuration file to include our modules:

# dev.yaml

app:
    name: http-demo
    modules:
        - hello.HelloModule
        - goodbye.GoodByeModule

logging.level: INFO

http:
    host: 0.0.0.0
    port: 8080

    demo:
        host: 0.0.0.0
        port: 8081

To test it just install applipy_http (and pyyaml, because the config is in YAML and not in JSON) and run the application:

pip install applipy_http pyyaml
python -m applipy

The implemented endpoints should be available in:

Advanced Features

Check the docs at /docs for explanations on the advanced functionalities.

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

applipy_http-0.13.1.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

applipy_http-0.13.1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file applipy_http-0.13.1.tar.gz.

File metadata

  • Download URL: applipy_http-0.13.1.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for applipy_http-0.13.1.tar.gz
Algorithm Hash digest
SHA256 a36077e5d30b72cd30cbc69ba66279036e74d8b9d663e31c2f6cdcf5b79833c7
MD5 916d79a5df89df759054a9c09579363e
BLAKE2b-256 3a7a9c8434d6851b4b63bac92a9059138df74be4b6a91064427ce4da6abab68f

See more details on using hashes here.

File details

Details for the file applipy_http-0.13.1-py3-none-any.whl.

File metadata

  • Download URL: applipy_http-0.13.1-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for applipy_http-0.13.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2ab3f840a4342ca1f4c71a60445336da25254988d20e31b1ebd969d0b70f5769
MD5 7c94cc0d7e67ffe4ec8af9151a477162
BLAKE2b-256 467d81704b9d925c9e0fd99c343c81683d1d439185c0342e0692342a77d7cc07

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