diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/__pycache__/book.cpython-311.pyc b/__pycache__/book.cpython-311.pyc new file mode 100644 index 0000000..026f7f0 Binary files /dev/null and b/__pycache__/book.cpython-311.pyc differ diff --git a/__pycache__/book_review.cpython-311.pyc b/__pycache__/book_review.cpython-311.pyc new file mode 100644 index 0000000..394d961 Binary files /dev/null and b/__pycache__/book_review.cpython-311.pyc differ diff --git a/app.py b/app.py index 997d548..7849d5c 100644 --- a/app.py +++ b/app.py @@ -2,136 +2,160 @@ 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 + required: false + enum: ['ASC','DESC'] + description: The parameter to sort the reviews by (e.g., 'rating', 'book') + - name: max_records + in: query + type: integer + required: false + description: The maximum number of records to return responses: 200: description: A successful GET request content: application/json: schema: - type: object - properties: - text: - type: string - description: The text in uppercase + type: array + items: + type: object + properties: + Books: + type: string + ISBN: + type: string + Rating: + type: number + Notes: + type: string """ - text = request.args.get('text') + sort = request.args.get("sort", default=None) + max_records = request.args.get("max_records", default=10, type=int) - return jsonify({"text": text.upper()}) - -class Records(Resource): - def get(self): + 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: + book_reviews = br.get_book_ratings(max_records=max_records) + return book_reviews, 200 + + +class PostReview(Resource): + def post(self): """ - This method responds to the GET request for returning a number of books. + This method responds to the POST request for this endpoint and adds a new book review. --- tags: - - Records + - Book Reviews + consumes: + - application/json 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 + - in: body + name: body + description: Book review data + required: true + schema: + id: BookReview + required: + - Book + - Rating + - ISBN + properties: + Book: + type: string + description: The name of the book + Rating: + type: number + description: The review text + ISBN: + type: string + description: Book Serial Number + Notes: + type: string + description: Additional notes (optional) 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 + 201: + description: Review added successfully """ + data = request.get_json() + book = data.get("Book") + 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 - count = request.args.get('count') # Default to returning 10 books if count is not provided - sort = request.args.get('sort') + return {"message": "Review added successfully"}, 201 - # Get all the books - books = book_review.get_all_records(count=count, sort=sort) - return {"books": books}, 200 - -class AddRecord(Resource): - def post(self): +class UpdateReview(Resource): + def put(self): """ - This method responds to the POST request for adding a new record to the DB table. + This method responds to the PUT request for this endpoint and updates a book review. --- tags: - - Records + - Book Reviews + consumes: + - application/json parameters: - in: body name: body + description: Data to update the book review required: true schema: - id: BookReview + id: UpdateBookReview required: - - Book - - Rating + - ISBN + - Rating properties: - Book: - type: string - description: the name of the book - Rating: - type: integer - description: the rating of the book (1-10) + 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: A successful POST request - 400: - description: Bad request, missing 'Book' or 'Rating' in the request body + description: Review updated successfully """ + data = request.get_json() + isbn = data.get("ISBN") + rating = data.get("Rating") + notes = data.get("Notes", None) # 'notes' is optional - data = request.json - print(data) + # Here you would add logic to update the review data in your database + br.update_book_rating(isbn, rating, notes) - # 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) + return {"message": "Review updated successfully"}, 200 - if success: - return {"message": "Record added successfully"}, 200 - else: - return {"message": "Failed to add record"}, 500 - +api.add_resource(UpdateReview, "/update_review") +api.add_resource(PostReview, "/post_review") +api.add_resource(AllReviews, "/all_reviews") -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) diff --git a/app_get.py b/app_get.py new file mode 100644 index 0000000..9b41e58 --- /dev/null +++ b/app_get.py @@ -0,0 +1,13 @@ +import requests + +baseurl = "https://api-example-mbd8.onrender.com/process_text" + +params = { + "text": "Chicken Fucker lol LOLLOL", + "duplication_factor": 3000, + "capitalization": "UPPER", +} + +resp = requests.get(url=baseurl, params=params) + +print(resp.json()) diff --git a/book.py b/book.py new file mode 100644 index 0000000..e038c1b --- /dev/null +++ b/book.py @@ -0,0 +1,52 @@ +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(max_records=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, isbn, notes=None): + 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}'") + + 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 + + # Update the record using PATCH method + self.table.update(record_id, fields=updated_fields, typecast=True) + + +if __name__ == "__main__": + br = BookReview() diff --git a/book_review.py b/book_review.py index dd0725d..5184671 100644 --- a/book_review.py +++ b/book_review.py @@ -1,54 +1,61 @@ -import os +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) + 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__': - ## 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") +# 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") - # 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()) \ No newline at end of file +# ## Show all records +# print("All records!") +# print(table.all()) diff --git a/main_template.py b/main_template.py index 2b15666..7633bbb 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,65 @@ 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()}) + +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__": - app.run(debug=True) \ No newline at end of file + app.run(debug=True) 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