Code Monkey home page Code Monkey logo

flask-rest-starter's Introduction

flask-rest-starter

CircleCI

Base project to build REST APIs using Flask + Flask-RESTful + marshmallow.

This project contains the components needed to develop, test and deploy as docker image REST APIs with Flask. An example API, a countries CRUD, is included.

Local development environment

python3 -m venv venv
. ./venv/bin/activate
pip install -r requirements.txt

Run

flask run

Run tests

pytest

Build Docker image and run

docker -t myimage .
docker run 5000:80 myimage

After that, go and check http://localhost:5000/restexample/countries

Examples in /app/restexample

  • schemas.py defines input and output schemas used on the API
  • endpoints/*.py shows how to implement different REST API endpoints
  • __init__.py maps endpoints to url paths

This project groups endpoints into blueprints. If you create a new blueprint, register it in app/__init__.py

Example schema

class ListArgsSchema(Schema):
    limit = fields.Integer(required=False, validate=[
                           validate.Range(1, 100)], missing=100)
    offset = fields.Integer(required=False, validate=[
                            validate.Range(0)], missing=0)


class CountrySchema(Schema):
    code = fields.String(required=True, validate=[validate.Length(max=255)])
    name = fields.String(required=True, validate=[validate.Length(max=255)])

Example endpoints

class CountriesList(Resource):
    @output(CountrySchema, many=True)
    @qsargs(ListArgsSchema)
    def get(self, args_cleaned):
        limit = args_cleaned.get('limit')
        offset = args_cleaned.get('offset')

        countries = countries_list[offset:offset+limit]

        return countries, 200, {'x-next': request.base_url + "?offset=%d&limit=%d" % (offset+limit, limit)}

    @inputbody(CountrySchema)
    def post(self, data):
        country = Country()
        CountrySchema.toObject(data, country)
        countries_list.append(country)
        return None, 201

Note the decorators @output, @qsargs and @inputbody:

  • @qsargs: validates query string parameters using a marshmallow schema.
  • @inputbody: validates the request body using a marshmallow schema.
  • @output: transforms the method output using a marshmallow schema.

flask-rest-starter's People

Contributors

miguelgarcia 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.