forked from miguelgrinberg/microblog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter 23: Application Programming Interfaces (APIs) (v0.23)
- Loading branch information
1 parent
182a53e
commit b657101
Showing
10 changed files
with
277 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from flask import Blueprint | ||
|
||
bp = Blueprint('api', __name__) | ||
|
||
from app.api import users, errors, tokens |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from flask import g | ||
from flask_httpauth import HTTPBasicAuth, HTTPTokenAuth | ||
from flask_login import current_user | ||
from app.models import User | ||
from app.api.errors import error_response | ||
|
||
basic_auth = HTTPBasicAuth() | ||
token_auth = HTTPTokenAuth() | ||
|
||
|
||
@basic_auth.verify_password | ||
def verify_password(username, password): | ||
user = User.query.filter_by(username=username).first() | ||
if user is None: | ||
return False | ||
g.current_user = user | ||
return user.check_password(password) | ||
|
||
|
||
@basic_auth.error_handler | ||
def basic_auth_error(): | ||
return error_response(401) | ||
|
||
|
||
@token_auth.verify_token | ||
def verify_token(token): | ||
g.current_user = User.check_token(token) if token else None | ||
return g.current_user is not None | ||
|
||
|
||
@token_auth.error_handler | ||
def token_auth_error(): | ||
return error_response(401) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from flask import jsonify | ||
from werkzeug.http import HTTP_STATUS_CODES | ||
|
||
|
||
def error_response(status_code, message=None): | ||
payload = {'error': HTTP_STATUS_CODES.get(status_code, 'Unknown error')} | ||
if message: | ||
payload['message'] = message | ||
response = jsonify(payload) | ||
response.status_code = status_code | ||
return response | ||
|
||
|
||
def bad_request(message): | ||
return error_response(400, message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from flask import jsonify, g | ||
from app import db | ||
from app.api import bp | ||
from app.api.auth import basic_auth, token_auth | ||
|
||
|
||
@bp.route('/tokens', methods=['POST']) | ||
@basic_auth.login_required | ||
def get_token(): | ||
token = g.current_user.get_token() | ||
db.session.commit() | ||
return jsonify({'token': token}) | ||
|
||
|
||
@bp.route('/tokens', methods=['DELETE']) | ||
@token_auth.login_required | ||
def revoke_token(): | ||
g.current_user.revoke_token() | ||
db.session.commit() | ||
return '', 204 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from flask import jsonify, request, url_for, g, abort | ||
from app import db | ||
from app.models import User | ||
from app.api import bp | ||
from app.api.auth import token_auth | ||
from app.api.errors import bad_request | ||
|
||
|
||
@bp.route('/users/<int:id>', methods=['GET']) | ||
@token_auth.login_required | ||
def get_user(id): | ||
return jsonify(User.query.get_or_404(id).to_dict()) | ||
|
||
|
||
@bp.route('/users', methods=['GET']) | ||
@token_auth.login_required | ||
def get_users(): | ||
page = request.args.get('page', 1, type=int) | ||
per_page = min(request.args.get('per_page', 10, type=int), 100) | ||
data = User.to_collection_dict(User.query, page, per_page, 'api.get_users') | ||
return jsonify(data) | ||
|
||
|
||
@bp.route('/users/<int:id>/followers', methods=['GET']) | ||
@token_auth.login_required | ||
def get_followers(id): | ||
user = User.query.get_or_404(id) | ||
page = request.args.get('page', 1, type=int) | ||
per_page = min(request.args.get('per_page', 10, type=int), 100) | ||
data = User.to_collection_dict(user.followers, page, per_page, | ||
'api.get_followers', id=id) | ||
return jsonify(data) | ||
|
||
|
||
@bp.route('/users/<int:id>/followed', methods=['GET']) | ||
@token_auth.login_required | ||
def get_followed(id): | ||
user = User.query.get_or_404(id) | ||
page = request.args.get('page', 1, type=int) | ||
per_page = min(request.args.get('per_page', 10, type=int), 100) | ||
data = User.to_collection_dict(user.followed, page, per_page, | ||
'api.get_followed', id=id) | ||
return jsonify(data) | ||
|
||
|
||
@bp.route('/users', methods=['POST']) | ||
def create_user(): | ||
data = request.get_json() or {} | ||
if 'username' not in data or 'email' not in data or 'password' not in data: | ||
return bad_request('must include username, email and password fields') | ||
if User.query.filter_by(username=data['username']).first(): | ||
return bad_request('please use a different username') | ||
if User.query.filter_by(email=data['email']).first(): | ||
return bad_request('please use a different email address') | ||
user = User() | ||
user.from_dict(data, new_user=True) | ||
db.session.add(user) | ||
db.session.commit() | ||
response = jsonify(user.to_dict()) | ||
response.status_code = 201 | ||
response.headers['Location'] = url_for('api.get_user', id=user.id) | ||
return response | ||
|
||
|
||
@bp.route('/users/<int:id>', methods=['PUT']) | ||
@token_auth.login_required | ||
def update_user(id): | ||
if g.current_user.id != id: | ||
abort(403) | ||
user = User.query.get_or_404(id) | ||
data = request.get_json() or {} | ||
if 'username' in data and data['username'] != user.username and \ | ||
User.query.filter_by(username=data['username']).first(): | ||
return bad_request('please use a different username') | ||
if 'email' in data and data['email'] != user.email and \ | ||
User.query.filter_by(email=data['email']).first(): | ||
return bad_request('please use a different email address') | ||
user.from_dict(data, new_user=False) | ||
db.session.commit() | ||
return jsonify(user.to_dict()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
from flask import render_template | ||
from flask import render_template, request | ||
from app import db | ||
from app.errors import bp | ||
from app.api.errors import error_response as api_error_response | ||
|
||
|
||
def wants_json_response(): | ||
return request.accept_mimetypes['application/json'] >= \ | ||
request.accept_mimetypes['text/html'] | ||
|
||
|
||
@bp.app_errorhandler(404) | ||
def not_found_error(error): | ||
if wants_json_response(): | ||
return api_error_response(404) | ||
return render_template('errors/404.html'), 404 | ||
|
||
|
||
@bp.app_errorhandler(500) | ||
def internal_error(error): | ||
db.session.rollback() | ||
if wants_json_response(): | ||
return api_error_response(500) | ||
return render_template('errors/500.html'), 500 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""user tokens | ||
Revision ID: 834b1a697901 | ||
Revises: c81bac34faab | ||
Create Date: 2017-11-05 18:41:07.996137 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '834b1a697901' | ||
down_revision = 'c81bac34faab' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column('user', sa.Column('token', sa.String(length=32), nullable=True)) | ||
op.add_column('user', sa.Column('token_expiration', sa.DateTime(), nullable=True)) | ||
op.create_index(op.f('ix_user_token'), 'user', ['token'], unique=True) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index(op.f('ix_user_token'), table_name='user') | ||
op.drop_column('user', 'token_expiration') | ||
op.drop_column('user', 'token') | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters