From a6a1242909262f9d4d1a5e76dfa9f77fc9a360bd Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sun, 10 Dec 2023 10:31:20 -0700 Subject: [PATCH 01/19] add endpoints --- __pycache__/book_review.cpython-311.pyc | Bin 0 -> 2552 bytes app.py | 82 ++++++++++++++++++++---- app_get.py | 9 +++ book_review.py | 33 ++++++---- main_template.py | 7 +- 5 files changed, 103 insertions(+), 28 deletions(-) create mode 100644 __pycache__/book_review.cpython-311.pyc create mode 100644 app_get.py diff --git a/__pycache__/book_review.cpython-311.pyc b/__pycache__/book_review.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..394d961f07746a4d92b2036711a57da239a90de7 GIT binary patch literal 2552 zcma(SOK%fLcy@RE*j+nup5TzcrVUCR0wFD-i4rOWlB$8kt>UOc?!j6-LvZn9J8K6> zL*-UTbt9yt2q}@ONStB2;l`2K5IB<)^EvJ5CdyO4X#d!9cnQy-5d=LNS z_bUhnnak0uE`UJx&<-Mq2uT#>tC$p0VpJ3mMc+#fc@aCi=9NfK1caW! zR~u0m!BIDHMLi;-B6S_P^-^z9c4(Vd>ZYD2V8=7~Y9lI>AaOqsq6+amKv9)=0s4px z&`;Zla=S{&fQhyfwN;ORIF`!AfFrGWB|hR`6`r@X>U*CA(*9m-;3$@KL_0uRXR{V( zPdaJGABc4QDLwCQ79+Km&693=tCm{7%L-J8h zB19kqA}G}Nn$v_sf-TJ&3A=4}VR>ct^5T4K&jKp|uJ-K)-J-#x3Epo^1 z$lgeqBZ%UO!_(-^-vD;dD!MOj2&{iYD8l*O(P}0U>Vd{Y zTovw%=>gz9@qxTjomV25^Jbf@DmLxk+OUB(#OJQor-mnShiyDJAGvbqIlHW96X{iW zn#dK~T|0zCTTSVAV~oZ#jF>hyGb|gDY*)@OD6?Hz{YH}7lAcU*WIU5gXPF;#+6pI( z89LKbCVwDJ(+)3<-O8ls*bQTBj;@W}UInhim~JE(H8Nwyz3i<_`YasJ(z|-fNCIId za~m|TCFq@V#yz`zm1bkW;~2wuPH|IGf4d4J>rxyH0*zBMzKxY)2=^P~F;0A(v5usj=^cI5O)(Zs1 z6+U!d2JbHo9}2@y1>18o!&14VZi@*L5?MP)v|Tq7%*;Y6jB#LXZj4>e2BA< zZ|@X#wFt%VisOSjXAnJ0A;QVFUjQ5RWy%g!1Dn6Nwc0fGBRDclryi#>~b% zW=sXyU%quQ&M0?LU>^a#IZTR9h~p9u4Ud!g8Gt<6_xQJ0EKd;T?)AMDYhWB^rM
UfHx^6jiF7Qs-XFP@xuZcI@QT*rlw|?0)vI`2sTH?2qfIdW zmj&VIgQZ%zM_6^u1PMu6=UophQzNOzsir#^-wHLe+7dNQeU-9fT+VS0c!U^_Q1wAx z^I}=|%_ftM6@wu$91iu^V#c%;sY zp^`FPR;EjMri^DSJagbiE~S;Jf$A^efifPj@IXZk=I1Jbo*ng(Y7Je0^c=cSo7-wB zFi{RnyddSkbbbL8)(i7`@rT`|5*{hz5etvhWfsnraIlPn77jL<_S>0 M5aB-Xs2ZRD0Nvg!4*&oF literal 0 HcmV?d00001 diff --git a/app.py b/app.py index 997d548..3d524bf 100644 --- a/app.py +++ b/app.py @@ -8,6 +8,7 @@ api = Api(app) swagger = Swagger(app) + class UppercaseText(Resource): def get(self): """ @@ -33,10 +34,63 @@ def get(self): type: string description: The text in uppercase """ - text = request.args.get('text') + text = request.args.get("text") + + return {"text": text.upper()}, 200 + + +class ProcessText(Resource): + def get(self): + """ + This method responds to the GET request for this endpoint and processes the text. + --- + tags: + - Text Processing + parameters: + - name: text + in: query + type: string + required: true + description: The text to be processed + - name: duplication_factor + in: query + type: integer + required: false + description: The number of times the text should be repeated + - name: capitalization + in: query + type: string + required: false + description: Specify 'UPPER', 'LOWER', or leave empty for no capitalization change + responses: + 200: + description: A successful GET request + content: + application/json: + schema: + type: object + properties: + result: + type: string + description: The processed text + """ + text = request.args.get("text") + duplication_factor = request.args.get("duplication_factor", default=1, type=int) + capitalization = request.args.get("capitalization", default=None, type=str) + + if not text: + return {"error": "Text field is required."}, 400 + + if capitalization == "UPPER": + text = text.upper() + elif capitalization == "LOWER": + text = text.lower() + + result_text = text * duplication_factor + + return {"result": result_text}, 200 + - return jsonify({"text": text.upper()}) - class Records(Resource): def get(self): """ @@ -75,14 +129,17 @@ def get(self): 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') + count = request.args.get( + "count" + ) # Default to returning 10 books if count is not provided + sort = request.args.get("sort") # Get all the books books = book_review.get_all_records(count=count, sort=sort) return {"books": books}, 200 - + + class AddRecord(Resource): def post(self): """ @@ -109,7 +166,7 @@ def post(self): responses: 200: description: A successful POST request - 400: + 400: description: Bad request, missing 'Book' or 'Rating' in the request body """ @@ -117,8 +174,10 @@ def post(self): 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 + 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) @@ -126,12 +185,13 @@ def post(self): 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") +api.add_resource(ProcessText, "/process_text") + if __name__ == "__main__": - app.run(debug=True) \ No newline at end of file + app.run(debug=True) diff --git a/app_get.py b/app_get.py new file mode 100644 index 0000000..87963f4 --- /dev/null +++ b/app_get.py @@ -0,0 +1,9 @@ +import requests + +baseurl = "http://127.0.0.1:5000/uppercase" + +params = {"text": "hello world py"} + +resp = requests.get(url=baseurl, params=params) + +print(resp.json()) diff --git a/book_review.py b/book_review.py index dd0725d..2b129bb 100644 --- a/book_review.py +++ b/book_review.py @@ -1,44 +1,49 @@ -import os +import os from pyairtable import Api -API_TOKEN = os.environ.get('AIRTABLE_TOKEN') +API_TOKEN = os.environ.get("AIRTABLE_TOKEN") -BASE_ID = 'appi1uzlLKn1TEKSw' -TABLE_ID = 'tblvMMAVHo901m2Ra' +BASE_ID = "appi1uzlLKn1TEKSw" +TABLE_ID = "tblvMMAVHo901m2Ra" api = Api(API_TOKEN) table = api.table(BASE_ID, TABLE_ID) + def get_all_records(count=None, sort=None): sort_param = [] - if sort and sort.upper()=='DESC': - sort_param = ['-Rating'] - elif sort and sort.upper()=='ASC': - sort_param = ['Rating'] + if sort and sort.upper() == "DESC": + sort_param = ["-Rating"] + elif sort and sort.upper() == "ASC": + sort_param = ["Rating"] return table.all(max_records=count, sort=sort_param) + def get_record_id(name): - return table.first(formula=f"Book='{name}'")['id'] + return table.first(formula=f"Book='{name}'")["id"] + def update_record(record_id, data): table.update(record_id, data) return True + def add_record(data): # require data contains a "Book" key and a "Rating" key (data is a dict) - if 'Book' not in data or 'Rating' not in data: + if "Book" not in data or "Rating" not in data: return False table.create(data) return True -if __name__ == '__main__': + +if __name__ == "__main__": ## Show getting certain records print("Show getting certain records") - print(table.all(formula="Rating < 5", sort=['-Rating'])) + print(table.all(formula="Rating < 5", sort=["-Rating"])) ## Show getting a single record print("Show getting a single record") @@ -46,9 +51,9 @@ def add_record(data): # Replace a record print("Replace a record") name = "Test Message" - record_id = table.first(formula=f"Book='{name}'")['id'] + record_id = table.first(formula=f"Book='{name}'")["id"] table.update(record_id, {"Rating": 5}) ## Show all records print("All records!") - print(table.all()) \ No newline at end of file + print(table.all()) diff --git a/main_template.py b/main_template.py index 2b15666..cf930dc 100644 --- a/main_template.py +++ b/main_template.py @@ -6,8 +6,8 @@ api = Api(app) swagger = Swagger(app) -class UppercaseText(Resource): +class UppercaseText(Resource): def get(self): """ This method responds to the GET request for this endpoint and returns the data in uppercase. @@ -32,11 +32,12 @@ def get(self): type: string description: The text in uppercase """ - text = request.args.get('text') + text = request.args.get("text") return jsonify({"text": text.upper()}) + api.add_resource(UppercaseText, "/uppercase") if __name__ == "__main__": - app.run(debug=True) \ No newline at end of file + app.run(debug=True) From b63738833f1f04410db77030c0682cad87011003 Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sun, 10 Dec 2023 10:52:57 -0700 Subject: [PATCH 02/19] corrected mistake in file name --- app.py | 51 ---------------------------------------------- app_get.py | 4 ++-- main_template.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 53 deletions(-) diff --git a/app.py b/app.py index 3d524bf..5f0ffac 100644 --- a/app.py +++ b/app.py @@ -39,57 +39,6 @@ def get(self): return {"text": text.upper()}, 200 -class ProcessText(Resource): - def get(self): - """ - This method responds to the GET request for this endpoint and processes the text. - --- - tags: - - Text Processing - parameters: - - name: text - in: query - type: string - required: true - description: The text to be processed - - name: duplication_factor - in: query - type: integer - required: false - description: The number of times the text should be repeated - - name: capitalization - in: query - type: string - required: false - description: Specify 'UPPER', 'LOWER', or leave empty for no capitalization change - responses: - 200: - description: A successful GET request - content: - application/json: - schema: - type: object - properties: - result: - type: string - description: The processed text - """ - text = request.args.get("text") - duplication_factor = request.args.get("duplication_factor", default=1, type=int) - capitalization = request.args.get("capitalization", default=None, type=str) - - if not text: - return {"error": "Text field is required."}, 400 - - if capitalization == "UPPER": - text = text.upper() - elif capitalization == "LOWER": - text = text.lower() - - result_text = text * duplication_factor - - return {"result": result_text}, 200 - class Records(Resource): def get(self): diff --git a/app_get.py b/app_get.py index 87963f4..972e424 100644 --- a/app_get.py +++ b/app_get.py @@ -1,8 +1,8 @@ import requests -baseurl = "http://127.0.0.1:5000/uppercase" +baseurl = "http://127.0.0.1:5000/process_text" -params = {"text": "hello world py"} +params = {"text": "hello world py", "duplication_factor": 30, "capitalization": "UPPER"} resp = requests.get(url=baseurl, params=params) diff --git a/main_template.py b/main_template.py index cf930dc..7633bbb 100644 --- a/main_template.py +++ b/main_template.py @@ -37,6 +37,59 @@ def get(self): return jsonify({"text": text.upper()}) +class ProcessText(Resource): + def get(self): + """ + This method responds to the GET request for this endpoint and processes the text. + --- + tags: + - Text Processing + parameters: + - name: text + in: query + type: string + required: true + description: The text to be processed + - name: duplication_factor + in: query + type: integer + required: false + description: The number of times the text should be repeated + - name: capitalization + in: query + type: string + required: false + description: Specify 'UPPER', 'LOWER', or leave empty for no capitalization change + responses: + 200: + description: A successful GET request + content: + application/json: + schema: + type: object + properties: + result: + type: string + description: The processed text + """ + text = request.args.get("text") + duplication_factor = request.args.get("duplication_factor", default=1, type=int) + capitalization = request.args.get("capitalization", default=None, type=str) + + if not text: + return {"error": "Text field is required."}, 400 + + if capitalization == "UPPER": + text = text.upper() + elif capitalization == "LOWER": + text = text.lower() + + result_text = text * duplication_factor + + return {"result": result_text}, 200 + + +api.add_resource(ProcessText, "/process_text") api.add_resource(UppercaseText, "/uppercase") if __name__ == "__main__": From 265025646a130813e5b4ac20dd043d3f68fc76f4 Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sun, 10 Dec 2023 14:49:28 -0700 Subject: [PATCH 03/19] add .gitignore, update api and add database --- .gitignore | 1 + app.py | 152 +++++++++++++------------------------------------ app_get.py | 8 ++- book.py | 37 ++++++++++++ book_review.py | 36 ++++++------ 5 files changed, 102 insertions(+), 132 deletions(-) create mode 100644 .gitignore create mode 100644 book.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/app.py b/app.py index 5f0ffac..cb8bc2f 100644 --- a/app.py +++ b/app.py @@ -2,144 +2,70 @@ from flask_restful import Api, Resource from flasgger import Swagger -import book_review +import book app = Flask(__name__) api = Api(app) swagger = Swagger(app) +br = book.BookReview() -class UppercaseText(Resource): +class AllReviews(Resource): def get(self): """ - This method responds to the GET request for this endpoint and returns the data in uppercase. + This method responds to the GET request for this endpoint and returns a list of book reviews. --- tags: - - Text Processing + - Book Reviews parameters: - - name: text + - name: sort 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 {"text": text.upper()}, 200 - - - -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 + enum:[ASC,DESC] + description: The parameter to sort the reviews by (e.g., 'rating', 'book') + - name: max_records in: query - type: string - enum: ['ASC', 'DESC'] + type: integer required: false - description: Sort order for the books + description: The maximum number of records to return 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") - - # Get all the books - books = book_review.get_all_records(count=count, sort=sort) - - 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 + content: + application/json: + schema: + type: array + items: + type: object + properties: + book_title: + type:string + description: The book title + + book_rating: + type: number + description: User rating + book_notes: + type: string + description: Notes are optional """ + sort = request.args.get("sort", default=None) + max_records = request.args.get("max_records", default=10, type=int) - 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) + if sort and sort not in ["ASC", "DESC"]: + return {"error": "Invalid sort value"}, 400 - if success: - return {"message": "Record added successfully"}, 200 + if sort == "ASC": + book_reviews = br.get_book_ratings(sort=sort, max_records=max_records) + elif sort == "DESC": + book_reviews = br.get_book_ratings(sort=sort, max_records=max_records) else: - return {"message": "Failed to add record"}, 500 + book_reviews = br.get_book_ratings(max_records=max_records) + return book_reviews, 200 -api.add_resource(AddRecord, "/add-record") -api.add_resource(Records, "/records") -api.add_resource(UppercaseText, "/uppercase") -api.add_resource(ProcessText, "/process_text") +api.add_resource(AllReviews, "/all_reviews") if __name__ == "__main__": diff --git a/app_get.py b/app_get.py index 972e424..9b41e58 100644 --- a/app_get.py +++ b/app_get.py @@ -1,8 +1,12 @@ import requests -baseurl = "http://127.0.0.1:5000/process_text" +baseurl = "https://api-example-mbd8.onrender.com/process_text" -params = {"text": "hello world py", "duplication_factor": 30, "capitalization": "UPPER"} +params = { + "text": "Chicken Fucker lol LOLLOL", + "duplication_factor": 3000, + "capitalization": "UPPER", +} resp = requests.get(url=baseurl, params=params) diff --git a/book.py b/book.py new file mode 100644 index 0000000..9e805ef --- /dev/null +++ b/book.py @@ -0,0 +1,37 @@ +import os +from pyairtable import Api +from dotenv import load_dotenv + +load_dotenv() + +API_TOKEN = os.environ.get("API_TOKEN") + +BASE_ID = os.environ.get("BASE_ID") +TABLE_ID = os.environ.get("TABLE_ID") +api = Api(API_TOKEN) +table = api.table(BASE_ID, TABLE_ID) + + +class BookReview: + def __init__(self): + self.api = Api(API_TOKEN) + self.table = self.api.table(BASE_ID, TABLE_ID) + + def get_book_ratings(self, sort=None, max_records=10): + if not sort: + return self.table.all(sort=rating)[:max_records] + elif sort == "ASC": + rating = ["Rating"] + elif sort == "DESC": + rating = ["-Rating"] + table = self.table.all(sort=rating)[:max_records] + return table + + def add_book_rating(self, book_title, rating, notes=None): + fields = {"Books": book_title, "Rating": rating, "Notes": notes} + self.table.create(fields=fields) + + +if __name__ == "__main__": + br = BookReview() + print(br.get_book_ratings(sort="DESC")) diff --git a/book_review.py b/book_review.py index 2b129bb..5184671 100644 --- a/book_review.py +++ b/book_review.py @@ -1,11 +1,13 @@ import os from pyairtable import Api +from dotenv import load_dotenv -API_TOKEN = os.environ.get("AIRTABLE_TOKEN") +load_dotenv() -BASE_ID = "appi1uzlLKn1TEKSw" -TABLE_ID = "tblvMMAVHo901m2Ra" +API_TOKEN = os.environ.get("API_TOKEN") +BASE_ID = os.environ.get("BASE_ID") +TABLE_ID = os.environ.get("TABLE_ID") api = Api(API_TOKEN) table = api.table(BASE_ID, TABLE_ID) @@ -40,20 +42,20 @@ def add_record(data): return True -if __name__ == "__main__": - ## Show getting certain records - print("Show getting certain records") - print(table.all(formula="Rating < 5", sort=["-Rating"])) +# if __name__ == "__main__": +# ## Show getting certain records +# print("Show getting certain records") +# print(table.all(formula="Rating < 5", sort=["-Rating"])) - ## Show getting a single record - print("Show getting a single record") +# ## Show getting a single record +# print("Show getting a single record") - # Replace a record - print("Replace a record") - name = "Test Message" - record_id = table.first(formula=f"Book='{name}'")["id"] - table.update(record_id, {"Rating": 5}) +# # Replace a record +# print("Replace a record") +# name = "Test Message" +# record_id = table.first(formula=f"Book='{name}'")["id"] +# table.update(record_id, {"Rating": 5}) - ## Show all records - print("All records!") - print(table.all()) +# ## Show all records +# print("All records!") +# print(table.all()) From 2eaed233453e97cb6c20d7100cad13eb81c3de58 Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sun, 10 Dec 2023 14:55:13 -0700 Subject: [PATCH 04/19] updated requirements.txt --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index edd09fa..e20cc43 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ flask flasgger flask_restful pyairtable -gunicorn \ No newline at end of file +gunicorn +python-dotenv \ No newline at end of file From d12384be8745e38702b4a2e85301ebf7a4043386 Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sun, 10 Dec 2023 15:11:13 -0700 Subject: [PATCH 05/19] fixed book.py --- book.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book.py b/book.py index 9e805ef..cdfa937 100644 --- a/book.py +++ b/book.py @@ -19,7 +19,7 @@ def __init__(self): def get_book_ratings(self, sort=None, max_records=10): if not sort: - return self.table.all(sort=rating)[:max_records] + return self.table.all(max_records=max_records) elif sort == "ASC": rating = ["Rating"] elif sort == "DESC": From 9be29d1d1837d74199f28c6dc7c53c7577201b85 Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sun, 10 Dec 2023 16:40:38 -0700 Subject: [PATCH 06/19] updated app.py --- .gitignore | 1 - app.py | 15 ++++----------- 2 files changed, 4 insertions(+), 12 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 2eea525..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -.env \ No newline at end of file diff --git a/app.py b/app.py index cb8bc2f..718ce56 100644 --- a/app.py +++ b/app.py @@ -22,7 +22,7 @@ def get(self): in: query type: string required: false - enum:[ASC,DESC] + enum: ['ASC','DESC'] description: The parameter to sort the reviews by (e.g., 'rating', 'book') - name: max_records in: query @@ -39,23 +39,16 @@ def get(self): items: type: object properties: - book_title: + Books: type:string - description: The book title - - book_rating: + Rating: type: number - description: User rating - book_notes: + Notes: type: string - description: Notes are optional """ sort = request.args.get("sort", default=None) max_records = request.args.get("max_records", default=10, type=int) - if sort and sort not in ["ASC", "DESC"]: - return {"error": "Invalid sort value"}, 400 - if sort == "ASC": book_reviews = br.get_book_ratings(sort=sort, max_records=max_records) elif sort == "DESC": From 8655e0acd8ece03afe5d270a5d7f99bbeb1c0ed0 Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sun, 10 Dec 2023 16:54:06 -0700 Subject: [PATCH 07/19] fixed env variables --- .env | 4 ++++ .gitignore | 1 + __pycache__/book.cpython-311.pyc | Bin 0 -> 2332 bytes 3 files changed, 5 insertions(+) create mode 100644 .env create mode 100644 .gitignore create mode 100644 __pycache__/book.cpython-311.pyc diff --git a/.env b/.env new file mode 100644 index 0000000..e113b7c --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +AIRTABLE_KEY=patSlGHgNeOxXm6sr.84a5db0305f1b3e1e5d89f81cab49d925404549e1c22f0b05e70349dd77815c4 +BASE_ID=appdlJYtHg6ba5gub +TABLE_ID=tblQGOS2RNSZmEmqT +API_TOKEN=patD5ysKD5xGy3sOQ.a6144172b38b7f3b1959d45c7b8ecf51ec215d1834d081cb4ebe86fb8cc290d1 \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6b8710a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.git diff --git a/__pycache__/book.cpython-311.pyc b/__pycache__/book.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..fa36982ad228b078b4c81181d61fc5738f684bef GIT binary patch literal 2332 zcma)6TWcFf6rR0FyQ_=s%62L{Nz=wAmQC#T#ckapuoLIf1hqtI2@3I|Xf}@G)upo= zH#Q1vz=f?5*bOfBLm%`~*C8(SC-f&sL6Bvk5a?5&4=OGc^3*fCmMkfSW_IVyxy(5; z^PMyEb0{PuX#Y$vtABC`{XvCh6Aqb!Ee4@&gb`*ml%gZ6F*!EHGL+{uF2|>M2C+!x zcfsXp(;qQNMfaLfP{r3Wv@w|9h3HSAdoAUE10SKwM|=YxUzd*r`(;&smsVEgN}#!y z_E8m8X_tkaLibuKz@m!?`_~W_yDUL9gry~SCYgYBf}Oae!l3I>!wv%VvNJh7PBw}v_cvyxL~OHWk;I*;<9r6^DFZS$3HiloL4TNcf{+n zb5|*sXwu;6OeLhWY$bHB#aC#g*P0<7sSLM%J-IUr5VPftQ4&32 z5-}g4Dne`0;nKydBN*x1no4MC5eckr-9k>Cx?P+)uP#q5Ev7Xs zI|Y73Ef%JVE5_YI{w#=R)d%TZQG<9 zw6*+0zQjWX`Dxh(a_I*OQ8NXCb*SOwImdS`ZDjL{4u5_=d2ZEz))Sx5v=oRDV@U`C zLGCmyCXg`jghnR)RO=(IuRBGjIX*=j>7lmXMb%JfdK7PxzB=5NnQkKur>j6rWcNfG zNRZyOBgeL8w&RcEdrO<~T4bgknW+Y6>`>q4m+QBzTlV0|&6IV)x9>NU4_3<1i_>haOLPe|G_57J z#9`(~4#4~W^z!>`XL{*F4Ult*80QG|*g6@6z>biBmqLd;`0iv?4eL#KeHo&r4fPOV zM|N>DvIg9gVb?ED{m3ccyK(MGW89AO>QEcZUAB|d|9v2U^6==dgTIYd`!2z6`D`&* zmY<8!XJWK6SQVo+ajY(mRhz#HSd2}Ox4@au_d4dIcP^jKsfyyrijphf`x@ngit_dS zwB}g|y?Nv~B_lwdAUBX(AVi{CN@%(0SAdwUufejyhYi#sq9_bBP$%hUg&3XkpTt{- zMyK1%rL%cOA;&-@Zv%<-Ic!09-O^f*+prEGgt24vN)%!H!-n z5~QK%&G3#fN*&nS#1V=ln>V}@k5MzNH21u(#7{kp;^YI+(RV@@fHpXWVQeIs?q{Q+ zYUgjGlhw}u1qzsfN1?Tl8F~>6mp|J`tS5FpsRhr}gJ(?Xg&Zu8Z%nRF?nGAHN{ z^xJ_@d1fQN9^V%HgliD&YO%5+seQIp5(@_3D( SsPhxm=HCzyKLnmGO#cEQN9&LP literal 0 HcmV?d00001 From e2ea4695f4d28b12095228c333bdde51e243c875 Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sun, 10 Dec 2023 17:06:58 -0700 Subject: [PATCH 08/19] rvmove env --- .env | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 .env diff --git a/.env b/.env deleted file mode 100644 index e113b7c..0000000 --- a/.env +++ /dev/null @@ -1,4 +0,0 @@ -AIRTABLE_KEY=patSlGHgNeOxXm6sr.84a5db0305f1b3e1e5d89f81cab49d925404549e1c22f0b05e70349dd77815c4 -BASE_ID=appdlJYtHg6ba5gub -TABLE_ID=tblQGOS2RNSZmEmqT -API_TOKEN=patD5ysKD5xGy3sOQ.a6144172b38b7f3b1959d45c7b8ecf51ec215d1834d081cb4ebe86fb8cc290d1 \ No newline at end of file From b49b984143bde1ae3749b60ef084776661f0af01 Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sun, 10 Dec 2023 17:07:55 -0700 Subject: [PATCH 09/19] Update .gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6b8710a..4c49bd7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -.git +.env From 574afced45461437a29774665cd124d1178904d5 Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sun, 10 Dec 2023 17:09:01 -0700 Subject: [PATCH 10/19] update book.py --- .gitignore | 2 +- book.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6b8710a..4c49bd7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -.git +.env diff --git a/book.py b/book.py index cdfa937..d1f5b29 100644 --- a/book.py +++ b/book.py @@ -34,4 +34,6 @@ def add_book_rating(self, book_title, rating, notes=None): if __name__ == "__main__": br = BookReview() + br.add_book_rating("The Art of War",rating=3.5, notes="over rated") + br.add_book_rating("Sideways", 5.5, notes="paul giamait") print(br.get_book_ratings(sort="DESC")) From 4d20a83a58778162154815008a512f615b902b3f Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sun, 10 Dec 2023 18:12:59 -0700 Subject: [PATCH 11/19] update POST request --- app.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ book.py | 3 --- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 718ce56..1c45925 100644 --- a/app.py +++ b/app.py @@ -58,6 +58,53 @@ def get(self): return book_reviews, 200 +class PostReview(Resource): + def post(self): + """ + This method responds to the POST request for this endpoint and adds a new book review. + --- + tags: + - Book Reviews + consumes: + - application/json + parameters: + - in: body + name: body + description: Book review data + required: true + schema: + type: object + required: + - book + - review + properties: + books: + type: string + description: The name of the book + rating: + type: number + description: The review text + notes: + type: string + description: Additional notes (optional) + responses: + 201: + description: Review added successfully + """ + data = request.get_json() + book = data.get("book") + rating = data.get("review") + notes = data.get("notes", None) + + # Here you would add logic to insert the review data into your database + br.add_book_rating(book, rating, notes) + + return {"message": "Review added successfully"}, 201 + + +api.add_resource(PostReview, "/post_review") + + api.add_resource(AllReviews, "/all_reviews") diff --git a/book.py b/book.py index d1f5b29..d0674f7 100644 --- a/book.py +++ b/book.py @@ -34,6 +34,3 @@ def add_book_rating(self, book_title, rating, notes=None): if __name__ == "__main__": br = BookReview() - br.add_book_rating("The Art of War",rating=3.5, notes="over rated") - br.add_book_rating("Sideways", 5.5, notes="paul giamait") - print(br.get_book_ratings(sort="DESC")) From 87c968509a880b0b2a794c45cb422f46198806b5 Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sun, 10 Dec 2023 18:53:28 -0700 Subject: [PATCH 12/19] fixed POST method --- app.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/app.py b/app.py index 1c45925..70bde4a 100644 --- a/app.py +++ b/app.py @@ -40,7 +40,7 @@ def get(self): type: object properties: Books: - type:string + type: string Rating: type: number Notes: @@ -73,18 +73,18 @@ def post(self): description: Book review data required: true schema: - type: object + id: BookReview required: - - book - - review + - Book + - Rating properties: - books: + Book: type: string description: The name of the book - rating: + Rating: type: number description: The review text - notes: + Notes: type: string description: Additional notes (optional) responses: @@ -92,9 +92,10 @@ def post(self): description: Review added successfully """ data = request.get_json() - book = data.get("book") - rating = data.get("review") - notes = data.get("notes", None) + book = data.get("Book") + rating = data.get("Rating") + notes = data.get("Notes", None) + # Here you would add logic to insert the review data into your database br.add_book_rating(book, rating, notes) From 90cc86ab12a4038aa61f18ccb2520d0cac8e19a6 Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sun, 10 Dec 2023 19:00:35 -0700 Subject: [PATCH 13/19] this commit --- __pycache__/book.cpython-311.pyc | Bin 2332 -> 2213 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/__pycache__/book.cpython-311.pyc b/__pycache__/book.cpython-311.pyc index fa36982ad228b078b4c81181d61fc5738f684bef..026f7f02087c44a0a3f268e43d252167557281c2 100644 GIT binary patch delta 183 zcmbOuv{X=iIWI340}wn^yHXmilW#p9tD!j#-mtT@vTqHl)o^2bOG*F~SdGc?z zMkY=E$sO$eTq+XYBHOK|H0xr{(u{D@()0LKl%2B!w656lcKQWuydiv)oZ E03Nz0WB>pF delta 307 zcmZ1~I7di*IWI340}y;SD@%RD%)sy%#DM`;DC6^#jp{axvf4}xsXWVoVyj^a7^3)q zV*IIm3j`)JvIy%kq;Rd_Ud9Mi0mKl1%41|mVGIUiO`gq7j4zlNS5D4mk!F;kh3T(TFrw61VzU1ZU|!lHcvhCZ+|ut;5CmIRv#0Nj;9W&i*H From 9bb50a603feeca91c1e156ab7d4ee731b04324d6 Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sat, 16 Dec 2023 12:39:21 -0700 Subject: [PATCH 14/19] added ISBN --- app.py | 9 ++++++--- book.py | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 70bde4a..3c36a87 100644 --- a/app.py +++ b/app.py @@ -41,6 +41,8 @@ def get(self): properties: Books: type: string + ISBN: + type: string Rating: type: number Notes: @@ -77,6 +79,7 @@ def post(self): required: - Book - Rating + - ISBN properties: Book: type: string @@ -84,6 +87,9 @@ def post(self): Rating: type: number description: The review text + ISBN: + type: string + description: Book Serial Number Notes: type: string description: Additional notes (optional) @@ -96,7 +102,6 @@ def post(self): rating = data.get("Rating") notes = data.get("Notes", None) - # Here you would add logic to insert the review data into your database br.add_book_rating(book, rating, notes) @@ -104,8 +109,6 @@ def post(self): api.add_resource(PostReview, "/post_review") - - api.add_resource(AllReviews, "/all_reviews") diff --git a/book.py b/book.py index d0674f7..713e476 100644 --- a/book.py +++ b/book.py @@ -27,8 +27,8 @@ def get_book_ratings(self, sort=None, max_records=10): table = self.table.all(sort=rating)[:max_records] return table - def add_book_rating(self, book_title, rating, notes=None): - fields = {"Books": book_title, "Rating": rating, "Notes": notes} + def add_book_rating(self, book_title, rating, isbn, notes=None): + fields = {"ISBN": isbn, "Books": book_title, "Rating": rating, "Notes": notes} self.table.create(fields=fields) From 5b745a7b36805600b8b28448779f29065c42a95a Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sat, 16 Dec 2023 12:57:00 -0700 Subject: [PATCH 15/19] update review --- app.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++- book.py | 14 ++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 3c36a87..25f156d 100644 --- a/app.py +++ b/app.py @@ -99,15 +99,61 @@ def post(self): """ data = request.get_json() book = data.get("Book") + isbn = data.get("ISBN") rating = data.get("Rating") notes = data.get("Notes", None) # Here you would add logic to insert the review data into your database - br.add_book_rating(book, rating, notes) + br.add_book_rating(book, isbn, rating, notes) return {"message": "Review added successfully"}, 201 +class UpdateReview(Resource): + def put(self): + """ + This method responds to the PUT request for this endpoint and updates a book review. + --- + tags: + - Book Reviews + consumes: + - application/json + parameters: + - in: body + name: body + description: Data to update the book review + required: true + schema: + id: UpdateBookReview + required: + - ISBN + - Rating + properties: + ISBN: + type: string + description: Book Serial Number + Rating: + type: number + description: Updated rating for the book + Notes: + type: string + description: Optional updated notes for the book + responses: + 200: + description: Review updated successfully + """ + data = request.get_json() + isbn = data.get("ISBN") + rating = data.get("Rating") + notes = data.get("Notes", None) # 'notes' is optional + + # Here you would add logic to update the review data in your database + br.update_book_rating(isbn, rating, notes) + + return {"message": "Review updated successfully"}, 200 + + +api.add_resource(UpdateReview, "/update_review") api.add_resource(PostReview, "/post_review") api.add_resource(AllReviews, "/all_reviews") diff --git a/book.py b/book.py index 713e476..2d85d32 100644 --- a/book.py +++ b/book.py @@ -31,6 +31,20 @@ def add_book_rating(self, book_title, rating, isbn, notes=None): fields = {"ISBN": isbn, "Books": book_title, "Rating": rating, "Notes": notes} self.table.create(fields=fields) + def update_book_rating(self, isbn, new_rating, new_notes=None): + # Find the record with the given ISBN + records = self.table.all(filterByFormula=f"ISBN='{isbn}'") + if not records: + return None # No record found with the given ISBN + + record_id = records[0]["id"] + updated_fields = {"Rating": new_rating} + + if new_notes is not None: + updated_fields["Notes"] = new_notes + + self.table.update(record_id, fields=updated_fields) + if __name__ == "__main__": br = BookReview() From 13972606c1b95ab0b9b0f59937e87e04b6ee9ba5 Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sat, 16 Dec 2023 13:12:27 -0700 Subject: [PATCH 16/19] update review --- book.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/book.py b/book.py index 2d85d32..325617f 100644 --- a/book.py +++ b/book.py @@ -31,20 +31,20 @@ def add_book_rating(self, book_title, rating, isbn, notes=None): fields = {"ISBN": isbn, "Books": book_title, "Rating": rating, "Notes": notes} self.table.create(fields=fields) - def update_book_rating(self, isbn, new_rating, new_notes=None): - # Find the record with the given ISBN - records = self.table.all(filterByFormula=f"ISBN='{isbn}'") - if not records: - return None # No record found with the given ISBN +def update_book_rating(self, isbn, new_rating, new_notes=None): + # Fetch records filtered by ISBN + records = self.table.all(formula=f"{{ISBN}} = '{isbn}'") - record_id = records[0]["id"] - updated_fields = {"Rating": new_rating} + if not records: + return None # No record found with the given ISBN - if new_notes is not None: - updated_fields["Notes"] = new_notes + record_id = records[0]['id'] + updated_fields = {"Rating": new_rating} - self.table.update(record_id, fields=updated_fields) + if new_notes is not necessarily None: + updated_fields["Notes"] = new_notes + self.table.update(record_id, fields=updated_fields) if __name__ == "__main__": br = BookReview() From 842529692b9546adb7aa0db6973d7dc5c4db79d7 Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sat, 16 Dec 2023 13:18:30 -0700 Subject: [PATCH 17/19] update method --- book.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/book.py b/book.py index 325617f..b3f74eb 100644 --- a/book.py +++ b/book.py @@ -31,6 +31,7 @@ def add_book_rating(self, book_title, rating, isbn, notes=None): fields = {"ISBN": isbn, "Books": book_title, "Rating": rating, "Notes": notes} self.table.create(fields=fields) + def update_book_rating(self, isbn, new_rating, new_notes=None): # Fetch records filtered by ISBN records = self.table.all(formula=f"{{ISBN}} = '{isbn}'") @@ -38,13 +39,14 @@ def update_book_rating(self, isbn, new_rating, new_notes=None): if not records: return None # No record found with the given ISBN - record_id = records[0]['id'] + record_id = records[0]["id"] updated_fields = {"Rating": new_rating} - if new_notes is not necessarily None: + if new_notes is not None: updated_fields["Notes"] = new_notes self.table.update(record_id, fields=updated_fields) + if __name__ == "__main__": br = BookReview() From 8e2c1513d9a5e0886aca7355904775100c494490 Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sat, 16 Dec 2023 18:20:57 -0700 Subject: [PATCH 18/19] update put to patch --- book.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/book.py b/book.py index b3f74eb..7e47f23 100644 --- a/book.py +++ b/book.py @@ -32,21 +32,22 @@ def add_book_rating(self, book_title, rating, isbn, notes=None): self.table.create(fields=fields) -def update_book_rating(self, isbn, new_rating, new_notes=None): - # Fetch records filtered by ISBN - records = self.table.all(formula=f"{{ISBN}} = '{isbn}'") + def update_book_rating(self, isbn, new_rating, new_notes=None): + # Fetch records filtered by ISBN + records = self.table.all(formula=f"{{ISBN}} = '{isbn}'") - if not records: - return None # No record found with the given ISBN + if not records: + return None # No record found with the given ISBN - record_id = records[0]["id"] - updated_fields = {"Rating": new_rating} + record_id = records[0]["id"] + updated_fields = {"Rating": new_rating} - if new_notes is not None: - updated_fields["Notes"] = new_notes + if new_notes is not None: + updated_fields["Notes"] = new_notes - self.table.update(record_id, fields=updated_fields) + # Update the record using PATCH method + self.table.update(record_id, fields=updated_fields, typecast=True) if __name__ == "__main__": - br = BookReview() + br = BookReview() \ No newline at end of file From 5a9257ce67a894d9a7f1e89df155dd153a6f5880 Mon Sep 17 00:00:00 2001 From: gbbpro <78455428+gbbpro@users.noreply.github.com> Date: Sun, 17 Dec 2023 12:35:57 -0700 Subject: [PATCH 19/19] fixed post --- app.py | 3 +-- book.py | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 25f156d..7849d5c 100644 --- a/app.py +++ b/app.py @@ -102,9 +102,8 @@ def post(self): isbn = data.get("ISBN") rating = data.get("Rating") notes = data.get("Notes", None) - + br.add_book_rating(book_title=book, rating=rating, isbn=isbn, notes=notes) # Here you would add logic to insert the review data into your database - br.add_book_rating(book, isbn, rating, notes) return {"message": "Review added successfully"}, 201 diff --git a/book.py b/book.py index 7e47f23..e038c1b 100644 --- a/book.py +++ b/book.py @@ -28,10 +28,9 @@ def get_book_ratings(self, sort=None, max_records=10): return table def add_book_rating(self, book_title, rating, isbn, notes=None): - fields = {"ISBN": isbn, "Books": book_title, "Rating": rating, "Notes": notes} + fields = {"Books": book_title, "Rating": rating, "Notes": notes, "ISBN": isbn} self.table.create(fields=fields) - def update_book_rating(self, isbn, new_rating, new_notes=None): # Fetch records filtered by ISBN records = self.table.all(formula=f"{{ISBN}} = '{isbn}'") @@ -50,4 +49,4 @@ def update_book_rating(self, isbn, new_rating, new_notes=None): if __name__ == "__main__": - br = BookReview() \ No newline at end of file + br = BookReview()