Skip to content

Instantly share code, notes, and snippets.

@ianjuma
Created October 15, 2016 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ianjuma/7a11368efdbddf6b0c3a7b1865f0bf86 to your computer and use it in GitHub Desktop.
Save ianjuma/7a11368efdbddf6b0c3a7b1865f0bf86 to your computer and use it in GitHub Desktop.
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