Giter Club home page Giter Club logo

flask-feme's Introduction

api

Project Setup

  1. pip install -r requirements.txt
  2. chmod +x unit-tests.sh
  3. ./unit-tests.sh

Project Run Gunicorn

gunicorn main:app -c settings.py

error solved

refer to https://stackoverflow.com/questions/58189683/typeerror-attrib-got-an-unexpected-keyword-argument-convert
TypeError: attrib() got an unexpected keyword argument 'convert'

pip install attrs==19.1.0

说明

本项目是参考 https://zhuanlan.zhihu.com/p/101544546 建立的基于flask的快速开发框架 特点如下:

  • 支持持久化-》postgresql
  • 支持单元测试
  • 支持restful api

This project template is very similar to the one written by Leo-G, found here: https://github.com/Leo-G/Flask-SQLALchemy-RESTFUL-API

Overview

This project is a simple Dog-Owner tracker. The owners are considered Users, and we've set up Users, and roles (among other things) through Flask-Security. The models (found in db > models.py) are declared and managed using SQLAlchemy. The Flask-Restful API can be seen throughout the project, though the dog.py and owner.py files in the resources directory will be of most use when figuring out how to set up the HTTP methods through class-based methods.

We're using Flask-JWT for authentication and authorization.

The configuration for Flask-migrate can be seen in the migrate.py file in the project root.

As in the project by Leo-G, we make use of marshmallow and marshmallow_jsonapi for JSON and data validation. Schemas are declared for every model in the models.py file, and validation is performed in the different resource files.

Pip Packages Needed:

  • flask
  • flask-security
  • flask-restful
  • flask-cors
  • flask-sqlalchemy
  • flask-migrate
  • flask-script
  • flask-jwt
  • marshmallow
  • marshmallow_jsonapi
  • pyscopg2

Before Running Server - Set Up Database:

Create your DB in Postgres using the credentials stored in the config/base_settings.py file (see the setup.md file for instructions on doing this), and then run the following from the project root:

python3 migrate.py db init
python3 migrate.py db migrate
python3 migrate.py db upgrade

JSON Request Format When No Authorization Needed

When sending a POST request in order to create a new object (in this case, a Dog), you will need to follow a very strict format, or the JSON validator will reject it. This format is the following:

{
    "data": {
        "type": "dog",
        "attributes": {
            "dog_type": "INSERT DOG TYPE HERE"
        }
    }
}

Note: If make sure the value for the "type" key matches the "type" value in the Meta class of whatever Schema used to validate your object. You also can input however many attributes for your object as you'd like in the "attributes" dictionary.

Protected API Endpoints

As you can see in the user.py file, all of the CRUD features for the User class are protected by the decorator jwt_required(). This means that a valid Authorization token is needed in order to reach those endpoints and perform that logic. In order to get a valid auth token, send a POST request to this url: /api/v1/auth with the Content-Type Header of application/json, passing in the following:

{
    "ownername": "EMAIL",
    "ownerpassword": "PASSWORD"
}

Note: Rather than the JWT standard of "username" and "password" as the authorization keys, we've specified different keys, really just to show that you can. These keys are declared in the respective config files.

Once you send that request, you should get the following back:

{
    "access_token": "AUTH_TOKEN"
}

Using this token, you can then call the protected endpoints by adding the following headers to your calls:

Authorization : JWT AUTH_TOKEN

Note: Again, you can change the "JWT" value in the Auth header in the config file. We just chose not to in order to not conflict with OAuth2 Bearer tokens (as per the JWT documentation).

Users - Setup

You'll notice that there are several lines of code under the "Bootstrap Several Users" section in main.py. These users will be created if there are no users in the database, once you run the server, and hit an endpoint. I recommend using Postman to hit the endpoint localhost:5000/api/v1/dogs to get these users started.

Note: Make sure that once you're aware of this code there and the users that they create if using this boilerplate for a production-level application!

todo

get user name from JWT

flask-jwt: current_identity

add logger file

Heroku deployment - One Click Deploy

Deploy

flask-feme's People

Contributors

mclitao avatar minikiller 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.