Giter VIP home page Giter VIP logo

python-utils's Introduction

Python Utils

This is a python utils package consists of the following packages. A sample project using these modules can be seen at https://github.com/emreisikligil/lambda-flask-template

flaskjwt

flaskjwt adds jwt authorization support to flask views. All you have to do is to decorate authenticated views and flask jwt handles the rest. It passes extracted claims to the view function for further processing.

from pyutils.auth.flaskjwt import FlaskJWT, Claims

app = Flask()
auth = FlaskJWT("key", ["RS256"])

@app.route('/user/<user_id>', methods=['GET'])
@auth.authenticated
def get_user(user_id: str, claims: Claims):
    pass

modeltodict

When an SQLAlchemy model is decorated with @model2dict, todict() method is added to the model class which returns attributes as dict. Only mapped attributes are added to the dict. unmapped parameter can be set to add unmapped attributes to the final dict.

This method is needed to get proper attribute dict of the model object. _dict_ includes some additional attributes that are added by SQLAlchemy. That's why it cannot be used directly.

from pyutils.model2dict import model2dict

db = SQlAlchemy(app)

@model2dict(exclude=["meta"])
class MyTable(db.Model):
    __table_name__ = "my_table"
    id = db.Column(db.Integer, primary_key=True)
    data = db.Column(db.String(1024), nullable=False)
    meta = db.Column(db.String(1024), nullable=False)

row = MyTable.query.get(5)
d = row.todict()
print(d) # prints {"id": 5, "data": "..."}

schema_validation

Validates flask request body against a swagger (JSON) definition. Then it passes the body as a dict to the annotated function. If validation fails 422 - Unprocessable Entity exception is raised. The object that will be used to validate against should be defined under definitions of swagger spec. It does not follow references in the schema. To be able to use it all #ref keys should be dereferenced. You can use the following npm package for this purpose.

https://www.npmjs.com/package/swagger-cli

Dereferencing:

swagger-cli bundle -r -o swagger-flat.yml -t yaml swagger.yml

Usage:

from pyutils.schema_validation import SchemaValidation

schema = SchemaValidation("spec/swagger-flat.yml")

@app.route("/pets", methods=["POST"])
@schema.validate("AddPetRequest")
def add_pet(body): 
    # body is validated and contains the input as dict. 
    # No need to validate it again.

python-utils's People

Contributors

emreisikligil avatar

Watchers

 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.