Giter Site home page Giter Site logo

flask-inject's Introduction

Flask-Inject

pip install flask-inject

A micro dependency injection framework for Flask micro web framework :)

Usage

Add Inject framework to the Flask App. It will setup the all necessary handlers. The method will also return a global injector which you can call map, get and apply on it.

app = Flask(__name__)
inj = Inject(app)

Now you can attach some value to the global injector. When you call map on this global injector, it will attach the value to the corresponding key. Note that this is a global or shared injector, so the variables are shared cross all handlers. If you want have a injector for each different request, setup injector in before_request handler.

inj.map(version="v1.0")

You can also add dependency to the per request injector so that the dependency will not be shared across the different requests. Here we try to open a mysql connection for each request and in tear down method, we will close this connection. We will use @inject decorator to inject the dependencies to the handler.

The injector itself can be injeteced to the target function. So we just inject the injector itself to the function so that we can add new dependency to it. Note that the injector object here is a per request injector and its parent is the global injector inj we declared before.

@app.before_request
@inject("injector")
def before_request(injector):
    # Open mysql connection here
    mysql = "open mysql here"
    # Add mysql to the injector so that it will be available for handlers after before request
    injector.map(mysql=mysql)

Now we want to close the mysql connection in the teardown_request.

@app.teardown_request
@inject("mysql")
def teardown_request(exception, mysql):
    # close mysql here
    mysql = "close mysql here"

Suppose we need show the app version in this handler. We add the version to the global injector and as we mentioned before, the global injector is the parent injector of each per request injector. So here the version is also available.

Using @inject decorator to inject the dependencies to the handler.

@app.route("/version")
@inject("version")
def show_version(version):
    return version

Suppose we need access mysql database here. Since we add mysql to the injector before request. So just use @inject decorator to inject the dependencies to the handler.

@app.route("/mysql")
@inject("mysql")
def show_mysql(mysql):
    # do something with mysql
    mysql = "exec a query"
    return "success"

Injector is also available in the function decorator. Suppose we have an authentication decorator which checks the auth_token parameter in the url parameters.

def authentication(f):
    @inject("injector")
    @wraps(f)
    def decorated_function(injector, *args, **kwargs):
        # Check the aut_token here
        auth_token = request.args.get("auth_token")
        authorized = False
        if auth_token is not None and len(auth_token) > 0:
            authorized = True
        # Now we can map the result to the injector
        injector.map(authorized=authorized)
        return f(*args, **kwargs)

    return decorated_function

Then authorized is available for injecting to the handler.

Using @inject decorator to inject the dependencies to the handler.

You can use the : operator to rename the injected dependency.

@app.route("/auth")
@authentication
@inject("authorized:auth")
def show_auth(auth):
    if auth:
        return "200"
    return "401"

You can use the url parameter with the injector together in your handler.

@app.route("/auth/<int:user_id>")
@authentication
@inject("authorized:auth")
def show_auth_by_user(auth, user_id):
    if auth:
        return "200 with user id: " + str(user_id)
    return "401 with user id: " + str(user_id)

flask-inject's People

Contributors

imwithye avatar lynas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.