Skip to main content

RPC API based on the Falcon Framework

Project description

Build Status

Falcon RPC

Background

A wrapper that utilizes the Falcon Framework to create a HTTP RPC API.

The motivation for this API is the Slack Web API.

NOTE: I suggest being familiar with Falcon and Slack's APIs on your own, first.

I found myself creating lots of APIs for various projects and realized that RESTful APIs are not really useful for the type of stuff I work on. While there is a certain degree of CRUD operations I usually need to do, a lot of the "resources" I created were for kicking of backend processing jobs, running async analytic workloads, executing complex queries, etc.

Most of the things I do just don't fit into REST. Frankly, instead of "bending" REST to fit my will I found API's like Slack that seemed perfect for being flexible enough to fit any use case. However, while I was bending REST for my bidding I became really fond of the Falcon Framework. I 100% understand Falcon is designed for REST but guess what, I can do whatever I want so here we are.

Quickstart

Clone the repo and install.

cd falcon_rpc
pip install -I .
import falcon_rpc


class Test:
    """``test`` method family """

    def ok(self, req):
        # /test.ok
        req.resp_body = {'foo': 'bar'}

    def error(self, req):
        # /test.error
        # can raise an error at anytime
        raise falcon_rpc.RPCError('dummy_error_code')

    def fatal(self, req):
        # /test.fatal
        not_a_dict = 1
        not_a_dict['nope'] = 2  # pylint: disable=unsupported-assignment-operation  # noqa

    def echo(self, req):
        # /test.echo
        req.resp_body = {'you_sent': req.params}

    def warn(self, req):
        # /test.warn
        req.resp_body = {'data': 'mostly good'}
        # setting a warning even though the request was handled
        req.set_warning('some_warning')


class Another:

    def show_me(self, req):
        # req.req is the original Falcon Request object
        req.resp_body = {'hello_there': req.req.remote_addr}


def start():
    rpc = falcon_rpc.RPC()
    rpc.add_family(Test())
    rpc.add_family(Another())
    return rpc()

This can be launched just like you would Falcon:

$ gunicorn -b :8000 "app:start()" --workers 2 --log-level DEBUG --reload

Method Families and Methods

Instead of adding routes to your API, you add method families, which are Python classes here.

Your API follows the /METHOD_FAMILY.method structure like Slack and the name of your Python class corresponds to the method family, and methods of that class are utilized as the actual method being called.

It is important to note that method family names are case insensitive, they will always be cast to lowercase.

Method names are case sensitive.

Method Handlers

Method handlers take one arg, an instance of RPCRequest.

The resp_body attribute of this object can be set with your response data.

Like Slack, if your request could be processed, but you need to add a warning, you can do so by using the set_warning method.

Tests

$ pip install -r test-requirements.txt
$ pytest -s -vv --cov falcon_rpc --cov-report=term-missing tests/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

falcon_rpc-0.5.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file falcon_rpc-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: falcon_rpc-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for falcon_rpc-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 75a51a6fa9043425117985b8e179bcc5d69a788cfd973d5d5cf7ec01e41fe924
MD5 51a92658472f8bbc54e3a42bc7183aca
BLAKE2b-256 d070e8fd8b3b928232f06addc1f6e81d66102b830eef404ec224ebec74b9e6bb

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