Skip to content

Commit

Permalink
feat(python): Python sample application for e2e testing
Browse files Browse the repository at this point in the history
  • Loading branch information
NSSPKrishna committed Jan 30, 2024
1 parent 64b0d8f commit 0d9b5bd
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
89 changes: 89 additions & 0 deletions test/deploy/linux/python/templates/app.py
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()
2 changes: 2 additions & 0 deletions test/deploy/linux/python/templates/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Flask>=2.2.2
Flask-Cors==3.0.10

0 comments on commit 0d9b5bd

Please sign in to comment.