Skip to main content

Routing utilities for WSGI apps using horseman.

Project description

Routing capabilities for horseman nodes.

This package introduces a ‘Routes’ component that allows registration of endpoints on one or several HTTP Methods. Routes can be looked up by URL or by name.

Example

Below is an example of a barebone API, handling a GET request on ‘/’ and returning a JSON response.

import logging
from bjoern import run
from horseman.meta import SentryNode, Overhead, APIView
from horseman.response import Response
from roughrider.routing import Routes


class Request(Overhead):

    data = None

    def __init__(self, environ):
        self.environ = environ

    def extract(self):
        self.data = 'somedata'


class RootNode(SentryNode):

    def __init__(self):
        self.routes = Routes()

    def resolve(self, path: str, environ: dict):
        route = self.routes.match_method(path, environ['REQUEST_METHOD'])
        if route is not None:
            request = Request(environ)
            return route.endpoint(request, **route.params)

    def handle_exception(self, exc_info, environ):
        logging.error(exc_info)


app = RootNode()

@app.routes.register('/')
class View(APIView):

    def GET(self, overhead):
        return Response.to_json(200, {"Result": "OK"})

run(
    host="0.0.0.0",
    port=8080,
    reuse_port=True,
    wsgi_app=app,
)

CHANGES

0.2.1 (2022-03-15)

  • Fixed bug existing while registering 2 routes with the same name.

0.2 (2021-10-09)

  • Code structure was reshuffled for more readability.

0.1 (2021-10-08)

  • Initial release

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

roughrider.routing-0.2.1.tar.gz (1.1 MB view details)

Uploaded Source

File details

Details for the file roughrider.routing-0.2.1.tar.gz.

File metadata

  • Download URL: roughrider.routing-0.2.1.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

File hashes

Hashes for roughrider.routing-0.2.1.tar.gz
Algorithm Hash digest
SHA256 b8585518a40691e5ec03fd6745dbde66aec6c0d50296e73e5bc40858f7a8f5aa
MD5 bca9523807726471e8a14471b4114187
BLAKE2b-256 2f33fbbb9b0644a07563ba40c98f2e479169fdcabb34c53200d162cde2ce99bc

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