未加星标

Building RESTful APIs With Flask: An ORM With SQLAlchemy

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

In thefirst part of this three-part tutorial series, we saw how to write RESTful APIs all by ourselves using Flask as the web framework. The previous approach provided a whole lot of flexibility but also included writing a lot of code that otherwise could have been avoided in more generic cases.

In this part, we will use a Flask extension, Flask-Restless , which simply generates RESTful APIs for database models defined with SQLAlchemy. I will take the same sample application as in the last part of this series to maintain context and continuity.

Installing Dependencies

While continuing with the application from the first part, we need to install only one dependency:

$ pip install Flask-Restless The Application

Flask-Restless makes addingRESTful API interfaces to models written with SQLAlchemy a piece of cake.First, add the REST APIManager from the flask.ext.restless extension to the application configuration file.

flask_app/my_app/__init__.py

from flask.ext.restless import APIManager
manager = APIManager(app, flask_sqlalchemy_db=db)

Just adding the above couple of lines to the existing code should suffice.

flask_app/my_app/catalog/views.py

This file comprisesthe bulk of the changes from the previous part. Below is the complete rewritten file.

from flask import Blueprint
from my_app import manager
from my_app.catalog.models import Product
catalog = Blueprint('catalog', __name__)
@catalog.route('/')
@catalog.route('/home')
def home():
return "Welcome to the Catalog Home."
manager.create_api(Product, methods=['GET', 'POST'])

It is pretty self-explanatory how the above code would work. We just imported the manager that was created in aprevious file, and it is used to create an API for the Product model with the listed methods . We can add more methods like DELETE , PUT , PATCH , etc. as needed.

Application in Action

Let's test this application by creating some products and listing them. The endpoint created by this extension by default is http://localhost:5000/api/product .

As I did in the last part of this tutorial series, I will test this using the requests library via terminal.

>>> import requests
>>> import json
>>> res = requests.get('http://127.0.0.1:5000/api/product')
>>> res.json()
{u'total_pages': 0, u'objects': [], u'num_results': 0, u'page': 1}
>>> d = {'name': u'iPhone', 'price': 549.00}
>>> res = requests.post('http://127.0.0.1:5000/api/product', data=json.dumps(d), headers={'Content-Type': 'application/json'})
>>> res.json()
{u'price': 549.0, u'id': 1, u'name': u'iPhone'}
>>> d = {'name': u'iPad', 'price': 649.00}
>>> res = requests.post('http://127.0.0.1:5000/api/product', data=json.dumps(d), headers={'Content-Type': 'application/json'})
>>> res.json()
{u'price': 649.0, u'id': 2, u'name': u'iPad'}
>>> res = requests.get('http://127.0.0.1:5000/api/product')
>>> res.json()
{u'total_pages': 1, u'objects': [{u'price': 549.0, u'id': 1, u'name': u'iPhone'}, {u'price': 649.0, u'id': 2, u'name': u'iPad'}], u'num_results': 2, u'page': 1} How to Customize

It is really handy to have the RESTful APIs created automatically, but each application has some business logic which calls for customizations, validations, and clever/securehandling of requests as needed.

Here, request preprocessors and postprocessors come to the rescue.As the names signify, methods designated aspreprocessors run before the processing of therequest, andmethods designated aspostprocessors run after the processing of the request. create_api() is the place where they are definedas dictionaries of the request type ( GET , POST , etc.) and themethods as listwhich willact as preprocessors or postprocessors on the specified request. Below is a template example:

manager.create_api(
Product,
methods=['GET', 'POST', 'DELETE'],
preprocessors={
'GET_SINGLE': ['a_preprocessor_for_single_get'],
'GET_MANY': ['another_preprocessor_for_many_get'],
'POST': ['a_preprocessor_for_post']
},
postprocessors={
'DELETE': ['a_postprocessor_for_delete']
}
)

The GET , PUT , and PATCH requests have the flexibility of beingfired for single as well asmultiple records; therefore, they have two types each. In the code above, notice GET_SINGLE and GET_MANY for GET requests.

The preprocessors and postprocessors accept different parametersfor each type of requestand work without any return value. This is left for you to try on your own.

Conclusion

In this part of this tutorial series, we saw how to create a RESTful API using Flaskjust by adding a couple of lines to a SQLAlchemy-based model.

In the next and lastpart of this series, I will cover how to create a RESTful API using another popular Flask extension, but this time, the API will be independent of the modeling tool used for thedatabase.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: RESTSQLiPadiPhoneUT
分页:12
转载请注明
本文标题:Building RESTful APIs With Flask: An ORM With SQLAlchemy
本站链接:http://www.codesec.net/view/482165.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(35)