Skip to main content

A simple back-end rest framework in python using aiohttp lib

Project description

apys - v3.1

Wellcome to apys! A simple backend restful framework!

LANGUAGE

Python >= 3.4.2

LIBRARIES

  • aiohttp - http client/server for asyncio

INSTALLATION

  1. Install python 3
    • Windows - Link
    • Ubuntu - sudo apt-get install python
    • Fedora - sudo yum install python
    • Arch - sudo pacman -S python
  2. Install PIP - Python libraries manager
    • Windows - Link
    • Ubuntu - sudo apt-get install pip
    • Fedora - sudo yum install pip
    • Arch - sudo pacman -S pip
  3. Install this framework using PIP
    • pip install apys

INITIALIZING PROJECT

$ apys --init

USING

DIRECTORIES

/config - json configuration files
/endpoints - backend endpoints
/filters - script files to execute before the endpoint  
/utils - script files to execute when server starts

CONFIG

Here are the configuration files used in the app. They will be send to the endpoint via param api.config

There are 3 special file names:

  • prod.json - The production configuration file
  • dev.json - The development configuration file
  • local.json - The local configuration file (ignore in git)

You can also force it to use a configuration with the --config or -c option:

$ apys -s --config=my_config

Note: If no config file is chosen, they will work as following: the api tries to load local.json, then dev.json, then prod.json

The current config special properties are the following:

{
    "log": {
        "file": {
            "debug": "string or false //default=false. debug log file, false for sys.stdout", 
            "error": "string or false //default=false. debug error file, false for sys.stderr",
            "(...)": "string or false //optional. you can specify any other log file, but you will have to tell the `api.debug` function to use it"
        },
        "color": "bool //default=true"
    },
    "server": {
        "port": "int //default=8080",
        "cors": "string or false //default=false"
    },
    "utils": ["string //default=[]. list of utils in order to load"],
    "(...)": "(...) //you can add any other key and access it via `api.config['my_key']`"
}

You can also use environment variables like $PORT (for PORT env var), and set a default value if no env var is found like $PORT|8080 or $PORT|8080|int (if type is needed)

ENDPOINTS

This will be your main dev dir

All files added here will be an endpoint automatically

i.e.: the file endpoints/hello/world.py will generate an endpoint /hello/world

The file's code will be the following:

filters = [
    'filter1',
    ['filter2', 'filter3']
]

def method(req, api):
    pass # process

Where method is the http request type:

  • post
  • get
  • put
  • delete
  • head
  • options
  • default - executed when a request is made for any of the above, but it is not implemented

process is what you wan the endpoint to do (your code)

filter1, filter2 and filter3 are the filters scripts (without .py) executed before the endpoint is called

If you put your filter inside an array the error they return will be returned only if ALL of them return some error

req is aiohttp's request, documentation

req's property body only works for json works as of now

api is the object that contains all api functionalities:

  • config - Configuration dictionary used in the actual scope
  • debug - function to log messages
  • error - function to log errors

Also api.web contains aiohttp.web

FILTERS

Code that will that will be called before every request.

method(req, api) - method being the type of http request

The function that will be executed before every request to the function with the same name on the endpoint.
Any result should be stored on the variable `req`, because it is the only local variable on the request.

always(req, api)

The function that will be executed before any request.
Note: this function will be executed before the other filters.

UTILS

Python files special functionality.

It needs to be inside a dir and has some special files

init.py

This file contains a function that will be called before initializing the api.

def init(api):
    pass
The function that will be executed on server startup
Only one time.

Useful for setting some api constants

cli.py

This file contains a function that will add a commandline argument.

The util flags will be --[util_name] and --[util_name_first_char]

util name is test, so flags should be --test and -t

class CLI:
    def __init__(self, result):
        # See `parser.add_argument` doc for information on these
        self.action = 'store_true'
        self.default = False
        self.help = 'It makes everything shine'

        # store the result of user input
        self.result = result

    def start(self, api, endpoints):
        pass

EXAMPLE

Look at the demos/ for examples:

  1. hello_world: a simple hello world app, to learn the basics
  2. calculator: a simpler app that resembles more a normal product
  3. log_to_file: an example of logging in files
  4. user_role: an advanced example on filters
  5. unit_testing: an advanced example on adding cli arguments

STARTING THE SERVER

There are 2 ways to start the server

  1. Execute apys -s from terminal on your root project folder (Recommended)

  2. Call the method start() from module apys.server


OBSERVATION

Both the framework and this page are in development, so, subjected to changes.

Version previous to v0.1.0 vas called pypolyback and used python 2.

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

apys-3.2.1.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

apys-3.2.1-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file apys-3.2.1.tar.gz.

File metadata

  • Download URL: apys-3.2.1.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for apys-3.2.1.tar.gz
Algorithm Hash digest
SHA256 63d3068e028fe589c94bcafcf0adaa822c5041d837777ef0d8cae15ed5614a83
MD5 e2570299392d5c95d47a8017bdfcbd08
BLAKE2b-256 a770c93974d98bfe5906a1c71c13bc57c1e21cefd05d7cf46f26c558c8c88c87

See more details on using hashes here.

File details

Details for the file apys-3.2.1-py3-none-any.whl.

File metadata

  • Download URL: apys-3.2.1-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for apys-3.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 90ef959f518286442fa1987820412f4670e2aa671d77cfb71a2279ca9d3d2be5
MD5 1c4ff186f2a66b8ab440470c9276cd6a
BLAKE2b-256 002777bb6e1bf9ca2c05011e05ff205e31639ec833618f2545c482493860e735

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