ref: 0.0.1
rascunho/blueprints/api.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
from flask import Blueprint, abort, request from hashlib import sha1 from rascunho.types.document import Document from rascunho.database import db import json api = Blueprint("apiv1", __name__) @api.route('/api/v1', methods = ['POST']) def create(): params = json.loads(request.data.decode('utf-8')) doc = None content = params.get('text') if not content: abort(422) sha = sha1() sha.update(content.encode()) sha.update(request.remote_addr.encode()) existing_doc = Document.query.filter(Document.sha == sha.hexdigest()).one_or_none() if existing_doc: doc = existing_doc else: doc = Document() doc.sha = sha.hexdigest() doc.content = content db.add(doc) db.commit() db.flush() return doc.to_dict(), 201 @api.route('/api/v1/<sha>', methods = ['GET']) def show(sha): doc = Document.query.filter(Document.sha == sha).one_or_none() if not doc: abort(404) return doc.to_dict(), 200 |