Skip to main content

Flasked is a package for creating an API-REST even easier than with Flask_Restful.

Project description

Flasked is a package designed to ease the creation of a REST API. It uses Flask and Flask-Restful in the background.

https://badge.fury.io/py/flasked.svg

Example

How to create a RESTFUL API faster than this?

from flasked import Flasked

class Root():
    def get(self):
        return "This is the result"

flasked = Flasked()
flasked["/"] = Root
flasked.run()

Installation

It is only supported Python 3.4.1 onwards:

sudo pip3 install flasked

Description

Flasked behaves like a Python’s dict; thus, in order to create more entry points, it is only required to assign the route with the class that manages the route. Example:

flasked["/"] = RouteRoot
flasked["/route2/"] = Route2
flasked["/route2/<variable>/"] = Route3

The routes available can be iterated in the same way as a Python’s dict:

print(list(flasked.keys()))

for route in flasked:
    print(route)

for route, cls in flasked.items():
    print("{}: {}".format(route, cls))

The classes that manages the routes do not require to inherit from any special class, since Flasked is able to manage them. Nonetheless, they have access to all the methods and attributes that a Resource class has (from Flask_Restful).

The HTTP methods are defined as method from the class that handles it:

class Root():
    def get(self):
        return {"result": "result for GET"}

    def post(self):
        return {"result": "result for POST"}

    def delete(self):
        return {"result": "result for DELETE"}

Also, when a variable is defined in an entry URL, the class method’s need to reflect it:

class Route3():
    def get(self, variable):
        return variable

flasked["/route2/<variable>/"] = Route3

In order to run flask, it is only required to execute the run() method. It has the same arguments as a Flask app run():

flasked.run(host="0.0.0.0", port=2234, threaded=True, debug=False)

The flask_restful API object and the Flask original APP object are directly accessible from the flasked object

# Flask_Restful API object
api = flasked.api

# Flask API object
flask_app = flasked.flask_app

ADVANCED

If the class that manages a route requires arguments to be injected in the constructor, it can be done in the following way:

class Route():
    def __init__(self, argument1, argument2):
        self.argument1 = argument1
        self.argument2 = argument2

    def get(self, variable):
        return variable

# The following 3 lines do the same:
flasked["/"] = Route, argument1, argument2                                          # First way
flasked["/"] = Route, {'args': [argument1, argument2]}                              # Second way
flasked["/"] = Route, {'kwargs': dict(argument1=argument1, argument2=argument2)}    # third way

IMPORTANT: Note that if the first way is taken, the argument1 can’t be a dictionary that contains the keywords ‘args’ or ‘kwargs’. Otherwise, it will be used as a source for the args and kwargs of the initializer. It is always preferred to use the second mixed with the third way.

This package is completely compatible with flask_restful. For more information, check Flask_Restful.

BASIC USE CASES

Basic usage examples are shown in the Usage-examples page from the wiki:

MANUAL TESTING

If you want to test your API REST manually, there’s an explanation on how to do it in this wiki page

LICENSE

It is released under the MIT license.

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

flasked-0.0.4.tar.gz (4.9 kB view hashes)

Uploaded Source

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