From 41607aa7e97b7fee1bbf3b6d973354218177e7ce Mon Sep 17 00:00:00 2001 From: Keith Galli Date: Sat, 21 Oct 2023 11:25:43 -0400 Subject: [PATCH 1/3] Changed to using connexion library --- app.py | 147 ++++++----------------------------------------- handlers.py | 2 + requirements.txt | 4 +- swagger.yaml | 28 +++++++++ 4 files changed, 52 insertions(+), 129 deletions(-) create mode 100644 handlers.py create mode 100644 swagger.yaml diff --git a/app.py b/app.py index 997d548..3d17b2e 100644 --- a/app.py +++ b/app.py @@ -1,137 +1,28 @@ -from flask import Flask, jsonify, request -from flask_restful import Api, Resource -from flasgger import Swagger - +import connexion import book_review -app = Flask(__name__) -api = Api(app) -swagger = Swagger(app) - -class UppercaseText(Resource): - def get(self): - """ - This method responds to the GET request for this endpoint and returns the data in uppercase. - --- - tags: - - Text Processing - parameters: - - name: text - in: query - type: string - required: true - description: The text to be converted to uppercase - responses: - 200: - description: A successful GET request - content: - application/json: - schema: - type: object - properties: - text: - type: string - description: The text in uppercase - """ - text = request.args.get('text') - - return jsonify({"text": text.upper()}) - -class Records(Resource): - def get(self): - """ - This method responds to the GET request for returning a number of books. - --- - tags: - - Records - parameters: - - name: count - in: query - type: integer - required: false - description: The number of books to return - - name: sort - in: query - type: string - enum: ['ASC', 'DESC'] - required: false - description: Sort order for the books - responses: - 200: - description: A successful GET request - schema: - type: object - properties: - books: - type: array - items: - type: object - properties: - title: - type: string - description: The title of the book - author: - type: string - description: The author of the book - """ - - count = request.args.get('count') # Default to returning 10 books if count is not provided - sort = request.args.get('sort') +from flask import render_template # Remove: import Flask +import connexion - # Get all the books - books = book_review.get_all_records(count=count, sort=sort) +app = connexion.App(__name__, specification_dir="./") +app.add_api("swagger.yaml") - return {"books": books}, 200 - -class AddRecord(Resource): - def post(self): - """ - This method responds to the POST request for adding a new record to the DB table. - --- - tags: - - Records - parameters: - - in: body - name: body - required: true - schema: - id: BookReview - required: - - Book - - Rating - properties: - Book: - type: string - description: the name of the book - Rating: - type: integer - description: the rating of the book (1-10) - responses: - 200: - description: A successful POST request - 400: - description: Bad request, missing 'Book' or 'Rating' in the request body - """ +# def get_records(count=None, sort=None): +# books = book_review.get_all_records(count=count, sort=sort) +# return {"books": books} - data = request.json - print(data) - # Check if 'Book' and 'Rating' are present in the request body - if 'Book' not in data or 'Rating' not in data: - return {"message": "Bad request, missing 'Book' or 'Rating' in the request body"}, 400 - # Call the add_record function to add the record to the DB table - success = book_review.add_record(data) +# def post_add_record(): +# data = connexion.request.json +# if 'Book' not in data or 'Rating' not in data: +# return {"message": "Bad request, missing 'Book' or 'Rating' in the request body"}, 400 +# success = book_review.add_record(data) +# if success: +# return {"message": "Record added successfully"}, 200 +# else: +# return {"message": "Failed to add record"}, 500 - if success: - return {"message": "Record added successfully"}, 200 - else: - return {"message": "Failed to add record"}, 500 - - - -api.add_resource(AddRecord, "/add-record") -api.add_resource(Records, "/records") -api.add_resource(UppercaseText, "/uppercase") if __name__ == "__main__": - app.run(debug=True) \ No newline at end of file + app.run(debug=True, port=8080) + diff --git a/handlers.py b/handlers.py new file mode 100644 index 0000000..cb663ec --- /dev/null +++ b/handlers.py @@ -0,0 +1,2 @@ +def get_uppercase(text): + return {"text": text.upper()} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index edd09fa..b134c4d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,6 @@ flask flasgger flask_restful pyairtable -gunicorn \ No newline at end of file +gunicorn +connexion +connexion[swagger-ui] \ No newline at end of file diff --git a/swagger.yaml b/swagger.yaml new file mode 100644 index 0000000..0f57472 --- /dev/null +++ b/swagger.yaml @@ -0,0 +1,28 @@ +openapi: 3.0.0 +info: + title: My API + version: '1.0' +paths: + /uppercase: + get: + tags: + - Text Processing + parameters: + - name: text + in: query + schema: + type: string + required: true + description: The text to be converted to uppercase + responses: + '200': + description: A successful GET request + content: + application/json: + schema: + type: object + properties: + text: + type: string + description: The text in uppercase + operationId: handlers.get_uppercase \ No newline at end of file From ba330ef8eb2ef35021cc31df1350fc0f10626674 Mon Sep 17 00:00:00 2001 From: Keith Galli Date: Sat, 21 Oct 2023 11:40:16 -0400 Subject: [PATCH 2/3] Specify PyYAML version --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index b134c4d..4f2abd3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,5 +3,6 @@ flasgger flask_restful pyairtable gunicorn +PyYAML==6.0.1 connexion connexion[swagger-ui] \ No newline at end of file From 2ec6d0ba24c73ca8b4e7e49d97f1ecad56a52b2d Mon Sep 17 00:00:00 2001 From: Keith Galli Date: Sat, 21 Oct 2023 11:44:20 -0400 Subject: [PATCH 3/3] Another library additon --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 4f2abd3..243b277 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,5 +4,6 @@ flask_restful pyairtable gunicorn PyYAML==6.0.1 +openapi_spec_validator connexion connexion[swagger-ui] \ No newline at end of file