Skip to content

eb3095/jpapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jpapi

Simple Python API Implementation using JSON, or "JSON Python API". Like Flask without the unneeded bits.

Description

This is to make making python webservers for apis easy and quick. The objective is to enable rapid development with little overhead.

Installation

PyPi

pip install jpapi

Manual

cd /usr/src
git clone https://github.com/eb3095/jpapi.git
cd jpapi/jpapi
python3 setup.py install

Usage example

Implementation is short and simple, as intended.

Code

import json

from jpapi import API, APIHandler, Endpoint

API_OBJ = None
FAILURE_TEMPLATE = {
    "error": {"code": 400, "message": "Invalid input"},
    "status": "fail",
}
RESPONSE_TEMPLATE = {"data": {"response": ""}, "status": "success"}


class GenericAPI(API):
    def __init__(self, config):
        super().__init__(config, "/v1", GenericAPIHandler)


class GenericAPIHandler(APIHandler):
    def init(self):
        GenericEndpoint(self)


class GenericEndpoint(Endpoint):
    def __init__(self, handler):
        self.path = "/generic/endpoint"
        self.required_fields = ["input", "another_value"]
        super().__init__(handler)

    def POST(self, data):
        inp = data["input"]
        # Do something
        result = "Hello!"
        return result, 200


def main():
    global API_OBJ
    with open("/etc/project/config.json") as f:
        config = json.load(f)
    API_OBJ = GenericAPI(config)
    API_OBJ.start()


if __name__ == "__main__":
    main()

Config

The config is simple, and as you can see, designed to work well with configs of the entire project.

{
    "generic": {
        "program_key": "snip",
        "program_settings": 25,
        "another_program_setting": 52
    },
    "webserver": {
        "api_key": "snip",
        "host": "127.0.0.1",
        "port": 5000
    }
}

About

Simple Python API Implementation

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors