-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(python): Python sample application for e2e testing
- Loading branch information
1 parent
64b0d8f
commit 0d9b5bd
Showing
2 changed files
with
91 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import uuid | ||
from flask import Flask, jsonify, request | ||
from flask_cors import CORS | ||
|
||
BOOKS = [ | ||
{ | ||
'id': uuid.uuid4().hex, | ||
'title': 'On the Road', | ||
'author': 'Jack Kerouac', | ||
'read': True | ||
}, | ||
{ | ||
'id': uuid.uuid4().hex, | ||
'title': 'Harry Potter and the Philosopher\'s Stone', | ||
'author': 'J. K. Rowling', | ||
'read': False | ||
}, | ||
{ | ||
'id': uuid.uuid4().hex, | ||
'title': 'Green Eggs and Ham', | ||
'author': 'Dr. Seuss', | ||
'read': True | ||
} | ||
] | ||
|
||
# configuration | ||
DEBUG = True | ||
|
||
# instantiate the app | ||
app = Flask(__name__) | ||
app.config.from_object(__name__) | ||
|
||
# enable CORS | ||
CORS(app, resources={r'/*': {'origins': '*'}}) | ||
|
||
# sanity check route | ||
@app.route('/ping', methods=['GET']) | ||
def ping_pong(): | ||
return jsonify('pong!') | ||
|
||
def remove_book(book_id): | ||
for book in BOOKS: | ||
if book['id'] == book_id: | ||
BOOKS.remove(book) | ||
return True | ||
return False | ||
|
||
@app.route('/books', methods=['GET', 'POST']) | ||
def all_books(): | ||
response_object = {'status': 'success'} | ||
if request.method == 'POST': | ||
post_data = request.get_json() | ||
|
||
# break the app here - change to BOOKSSSS | ||
BOOKS.append({ | ||
'id': uuid.uuid4().hex, | ||
'title': post_data.get('title'), | ||
'author': post_data.get('author'), | ||
'read': post_data.get('read') | ||
}) | ||
response_object['message'] = 'Book added!' | ||
else: | ||
response_object['books'] = BOOKS | ||
return jsonify(response_object) | ||
|
||
@app.route('/books/<book_id>', methods=['PUT', 'DELETE']) | ||
def single_book(book_id): | ||
response_object = {'status': 'success'} | ||
if request.method == 'PUT': | ||
post_data = request.get_json() | ||
remove_book(book_id) | ||
BOOKS.append({ | ||
'id': uuid.uuid4().hex, | ||
'title': post_data.get('title'), | ||
'author': post_data.get('author'), | ||
'read': post_data.get('read') | ||
}) | ||
response_object['message'] = 'Book updated!' | ||
if request.method == 'DELETE': | ||
remove_book(book_id) | ||
response_object['message'] = 'Book removed!' | ||
return jsonify(response_object) | ||
|
||
@app.route('/post_book', methods=['POST']) | ||
def somefunction(): | ||
return jsonify('Posted') | ||
|
||
if __name__ == '__main__': | ||
app.run() |
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,2 @@ | ||
Flask>=2.2.2 | ||
Flask-Cors==3.0.10 |