Skip to main content

A simple wrapper over Flask to speed up basic API deployments.

Project description

Symmetric

A simple wrapper over Flask to speed up basic API deployments.

Why Symmetric?

While Flask is a powerful tool to have, getting it to work from scratch can be a bit of a pain, specially if you have never used it before. The idea behind Symmetric is to be able to take any module and transform it into a working API, instead of having to design the module ground-up to work with Flask.

Installing

Install using pip!

pip install --user symmetric

Usage

Defining the API endpoints

from symmetric import router, app

The module consists of two main objects: the router decorator and the app object. Let's start with the app object:

if __name__ == '__main__':
    app.run()

Add that code to one of your modules. Then, run python module.py. A Flask instance will be spawned immediately and can be reached at http://127.0.0.1:5000. We don't have any endpoints yet, so we'll add some (given that app is a Flask object, any WSGI server can be used to run the API, gunicorn is a good example).

Let's now analyze our router decorator:

@router("/some-route", methods=["get"], response_code=200)

The decorator recieves 3 arguments: the route argument (the endpoint of the API to which the decorated function will map), the methods argument (a list of the methods accepted to connect to that endpoint, defaults in only GET requests) and the response_code argument (the response code of the endpoint if everything goes according to the plan. Defaults to 200).

Now let's imagine that we have the following method:

def some_function():
    return "Hello World!"

To transform that method into an API endpoint, all you need to do is add one line:

@router("/sample")
def some_function():
    return "Hello World!"

Run python module.py and send a GET request to http://127.0.0.1:5000/sample. You should get a Hello World! in response! (To try it with a browser, just click this link).

But what about methods with arguments? Of course they can be API'd too! Let's now say that you have the following function:

def another_function(a, b):
    return a + b

To transform that method into an API endpoint, all you need to do, again, is add one line:

@router("/add")
def another_function(a, b):
    return a + b

Querying API endpoints

To give parameters to a function, all we need to do is send a json body with the names of the parameters as keys. Let's see how! Run python module.py and send a GET request to http://127.0.0.1:5000/add, now using the requests module.

import requests

payload = {
    "a": 48,
    "b": 21
}
response = requests.get("http://127.0.0.1:5000/add", json=payload)
print(response.json())

We got a 69 response! (48 + 21 = 69). Of course, you can return dictionaries from your methods and those will get returned as a json body in the response object automagically!

With this in mind, you can transform any existing project into a usable API very quickly!

The whole example

To sum up, if the original module.py file looked like this before symmetric:

def some_function():
    return "Hello World!"


def another_function(a, b):
    return a + b

The complete final module.py file with symmetric should look like this:

from symmetric import router, app


@router("/sample")
def some_function():
    return "Hello World!"


@router("/add")
def another_function(a, b):
    return a + b


if __name__ == '__main__':
    app.run()

To run the server, just run python module.py. Now, you can send GET requests to http://127.0.0.1:5000/sample and http://127.0.0.1:5000/add. Here is a simple file to get you started querying your API:

import requests


def call_sample():
    response = requests.get("http://127.0.0.1:5000/sample")
    return response.text


def call_add():
    payload = {
        "a": 48,
        "b": 21
    }
    response = requests.get("http://127.0.0.1:5000/add", json=payload)
    return response.json()


if __name__ == '__main__':
    print(call_sample())
    print(call_add())

Developing

Clone the repository:

git clone https://github.com/daleal/symmetric.git

cd symmetric

Recreate environment:

./environment.sh

. .venv/bin/activate

Build the project:

poetry build

Push to TestPyPi:

poetry config repositories.testpypi https://test.pypi.org/legacy/
poetry publish -r testpypi

Download from TestPyPi:

deactivate
rm -rf .testing-venv
python3 -m venv .testing-venv
. .testing-venv/bin/activate
python -m pip install --index-url https://test.pypi.org/simple/ symmetric

Push to PyPi:

poetry publish

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

Symmetric-0.0.5.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

Symmetric-0.0.5-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file Symmetric-0.0.5.tar.gz.

File metadata

  • Download URL: Symmetric-0.0.5.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for Symmetric-0.0.5.tar.gz
Algorithm Hash digest
SHA256 27c8e4cbdbc9940bed58ed2e3f8807ab6b23412380e17695f06e41756f2b3386
MD5 67b226402709106d6bd6404ca4c9ce52
BLAKE2b-256 ad5ece02766945b57e409ef37e10135140bc1c16252ca5efdae62094ecfa8519

See more details on using hashes here.

File details

Details for the file Symmetric-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: Symmetric-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for Symmetric-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 92aa234496ea29c75453d89eaf7f4a3d665c62f04bc676d135afaf507a3e9cbd
MD5 62e441e7790876b9b10b114dca267b9f
BLAKE2b-256 b843ce851aea76404fdce9953430269d4aa50ffc30cb099e849e30ce4eb568f1

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