Created
October 15, 2016 10:22
-
-
Save ianjuma/7a11368efdbddf6b0c3a7b1865f0bf86 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from flask import Flask | |
from flask import jsonify | |
from flask import request | |
app = Flask('the-box-library') | |
books = [{ | |
'name': 'Harry Potter and the prisoner of Azkaban', | |
'author': 'JK. Rowling', | |
'category': 'Magic / Fiction', | |
'id': '4325345234' | |
}, | |
{ | |
'name': 'Harry Potter and the prisoner of Azkaban', | |
'author': 'JK. Rowling', | |
'category': 'Magic / Fiction', | |
'id': '4325345234' | |
}] | |
@app.route('/api/category/books/', methods=['GET','POST']) | |
def book_api(): | |
resp = '' | |
if request.method == 'GET': | |
resp = jsonify(books) | |
else: | |
name = request.values.get('name', None) | |
author = request.values.get('author', None) | |
category = request.values.get('category', None) | |
id_ = request.values.get('id', None) | |
new_book = { | |
'name': name, | |
'author': author, | |
'category': category, | |
'id': id_ | |
} | |
books.append(new_book) | |
resp = jsonify({'OK': 'Book added'}) | |
return resp | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment