producti_gestio official documentation

producti_gestio

The Main module. A simple REST-API server creator.

Changelog

producti_gestio Version: 0.7.2

  • Fix installation
  • Add dependencies in setup.py
  • Automatic reading of requirements.txt in setup.py
  • Remove bin/producti-gestio, setup.py now will create the script from producti_gestio/__main__.py
Main Contributors:

Welcome to producti gestio! image0 image1 image2 image3 image4 image5

producti gestio (/producti gestio/, from Latin: Product management) is a simple API like web server written in Python. It could be used for your projects and it is useful for debugging. You can start a web server in few seconds and enjoy your development session.

Using producti-gestio, you are allowed to have a coffee break, because the entire server is managed by it!

Contents

Installation

From PyPi

Just use pip:

pip install producti_gestio

Or if you want to upgrade the package:

pip install --upgrade producti_gestio

From Github

Using Pip

Try using that piece of code:

pip install git+https://github.com/pyTeens/producti-gestio.git

Or if you want to upgrade the package

pip install --upgrade git+https://github.com/pyTeens/producti-gestio.git

Downloading files

In primis (from Latin, “firstable”), clone the repository:

git clone https://github.com/pyTeens/producti-gestio.git

Then, change directory:

cd producti-gestio

And finally, install the package:

sudo python3 setup.py install

Usage

Import the library

Just use import statement:

import producti_gestio

Create an handler function

You’re going to use a decorator that will create a Wrapper. Here an example:

from producti_gestio import Server

my_server = Server(allow_get=True) # Create the server instance

@my_server.on_request
def my_function(**kwargs):
    return {
        'response_code': 200, # The response code (see https://en.wikipedia.org/wiki/List_of_HTTP_status_codes)
        'response': { # A dictionary that will be encoded in JSON
            'ok': True,
            'message': 'Hello world!'
        }
    }

Then, if you call my_server.start() you’ll start the HTTPServer (using Threads):

And, if you’ll surf 127.0.0.1:8000 that will be the output:

{'ok': True, 'message': 'Hello world!'}

Get parameters, headers, etc…

Just look at the kwargs parameter. It contains a dictionary of all the informations you need.

  • Parameters -> kwargs['parameters']
  • Headers -> kwargs['header']
  • Request type -> kwargs['request-type']
  • Path requested -> kwargs['path']
  • Handler object -> kwargs['object']

Configuration

You can pass your own configuration to the server-creator function Here all the keyword arguments you can pass:

  • allow_get (default is False)
  • allow_post (default is True)
  • function (default is None, but it is needed, it’s the handler_function),
  • debug (default is False, if you want to print the Traceback under error_message in the JSON response when an Exception is caught)
  • ip (default is ‘127.0.0.1’)
  • port (default is 8000)

Run the server using decorators

Just call the handler function:

import producti_gestio

@producti_gestio.Decorator
def server_create(*args, **kwargs):
   """
   It create the server and
   launch it
   """
   def handler_function(*args, **kwargs):
       return ({
           'response_code': 200,
           'response': {
               'ok': True,
               'is_meme': True
           }
       })

   return handler_function

if __name__ == '__main__':
    server_create(allow_get=True)

Files

You’ll find lots of not understandable directory and files, so here a list and definitions of them:

  • producti_gestio - Main directory
    • producti_gestio/__init__.py - Init file, it included all classes
    • producti_gestio/__main__.py - It parses and processes all given parameters from the command line
    • producti_gestio/core - Directory for all important classes such as request_handler
      • producti_gestio/core/__init__.py - It includes all core classes
      • producti_gestio/core/check.py - It defines a check function that could be used a decorator for filters
      • producti_gestio/core/request_handler.py - The Handler of the requests, it passes parameters to the defined Handler function and then it send the JSON response
    • producti_gestio/decorator - Directory for all help-decorator classes
      • producti_gestio/decorator/__init__.py - It includes all decorator classes
      • producti_gestio/decorator/wrapper.py - The Decorator class, it is used to launch the Server class and define the Handler function
    • producti_gestio/exceptions - Directory for all the exception
      • producti_gestio/exceptions/__init__.py - It includes all the exceptions
      • producti_gestio/exceptions/exceptions.py - It defines all the exceptions
    • producti_gestio/filters - Directory for all the filters
      • producti_gestio/filters/__init__.py - It includes filter.py and filters.py
      • producti_gestio/filters/filter.py - It defines the Filter class
      • producti_gestio/filters/filters.py - It defines all the Filters
    • producti_gestio/handlers - Directory for the handlers
      • producti_gestio/handlers/__init__.py - It includes handler.py
      • producti_gestio/handlers/handler.py - It defines the Handler class
    • producti_gestio/project - Directory of some project generator tools
      • producti_gestio/project/__init__.py - It includes the project generator
      • producti_gestio/project/generator.py - Tools for code auto-generating
    • producti_gestio/server - The Server directory
      • producti_gestio/server/__init__.py - It includes all server classes
      • producti_gestio/server/server.py - The main class, it creates the server
    • producti_gestio/utils - Directory of some useful tools
      • producti_gestio/utils/__init__.py It includes the arguments parser
      • producti_gestio/utils/arguments_parser.py - It parses and processes the given arguments from the command line

How to contribute

In primis (“firstable”), you must read the code of conducts and the contributing document, then ask @hearot to enter the organization (pyTeens).

Copyright (c) 2018 pyTeens. All rights reserved.