Skip to content

Commit 477ac5d

Browse files
committed
adding stuff
1 parent 7c84c17 commit 477ac5d

17 files changed

Lines changed: 696 additions & 8 deletions

.DS_Store

8 KB
Binary file not shown.

.env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
TESTING_TOKEN=9876543210
2+
testing = 123
3+
AIRTABLE_API_KEY=patOstkQza7ECxkUO.3850dfefa32e1a5fb8ec1213bfb58da43eb76961a495d2cfd1a3455eb41b5ff1
4+
AIRTABLE_BASE_ID=app2X5z6J4Q5Q7J4
5+
AIRTABLE_TABLE_ID=tblYAreTaEsBGzRdG

Book_Review_class.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
from flask import Flask, jsonify, request
2+
from flask_restful import Api, Resource
3+
from flasgger import Swagger
4+
import book_review_module4
5+
6+
app = Flask(__name__)
7+
api = Api(app)
8+
swagger = Swagger(app)
9+
10+
br = book_review_module4.BookReview()
11+
class AllReview(Resource):
12+
def get(self):
13+
"""
14+
This method responds to the GET request for retrieving all book reviews.
15+
---
16+
tags:
17+
- Book Reviews
18+
parameters:
19+
- name: sort
20+
in: query
21+
type: string
22+
required: false
23+
enum: ["ASC", "DESC"]
24+
description: Sort reviews by rating in ascending or descending order (optional)
25+
- name: max_records
26+
in: query
27+
type: integer
28+
required: false
29+
description: Maximum number of records to retrieve (optional)
30+
responses:
31+
200:
32+
description: A successful GET request
33+
content:
34+
application/json:
35+
schema:
36+
type: array
37+
items:
38+
type: object
39+
properties:
40+
book_title:
41+
type: string
42+
description: The title of the book
43+
book_rating:
44+
type: number
45+
description: The rating of the book
46+
book_notes:
47+
type: string
48+
description: The notes of the book
49+
"""
50+
# Retrieve optional parameters from the query string
51+
sort = request.args.get('sort',default=None)
52+
max_records = int(request.args.get('max_records', default=10))
53+
54+
#Validate the sort parameter
55+
if sort and sort not in ["ASC", "DESC"]:
56+
return{"error": "Invalid value for 'sort' parameter"}, 400
57+
# Sort the reviews if sort parameter is provided
58+
if sort == "ASC":
59+
book_reviews = br.get_book_ratings(sort=sort, max_records=max_records)
60+
elif sort == "DESC":
61+
book_reviews = br.get_book_ratings(sort=sort, max_records=max_records)
62+
else:
63+
book_reviews = br.get_book_ratings(max_records=max_records)
64+
#delete below
65+
#else:
66+
# book_reviews_sorted = book_reviews
67+
68+
#(not needed handled in previous one) Limit the number of records if max_records parameter is provided
69+
#if max_records:
70+
# book_reviews_sorted = book_reviews_sorted[:int(max_records)]
71+
72+
return book_reviews, 200
73+
74+
class PostReview(Resource):
75+
def post(self):
76+
"""
77+
This method responds to the GET request for retrieving all book reviews.
78+
---
79+
tags:
80+
- Post Book Reviews
81+
parameters:
82+
- in: body
83+
name: body
84+
required: true
85+
schema:
86+
id: BookReview
87+
required:
88+
- book
89+
- rating
90+
properties:
91+
book:
92+
type: string
93+
description: The title of the book
94+
rating:
95+
type: integer
96+
description: Insert Book rating here (Required)
97+
notes:
98+
type: string
99+
description: Insert notes here (Required)
100+
101+
# - name: book_title
102+
# in: query
103+
# type: string
104+
# required: true
105+
# description: Insert Book Title here (Required)
106+
# - name: book_rating
107+
# in: query
108+
# type: integer
109+
# required: true
110+
# description: Insert Book Rating here, example 1.0 (Required)
111+
# - name: notes
112+
# in: query
113+
# type: string
114+
# required: false
115+
# description: Notes about the book / summary (optional)
116+
responses:
117+
200:
118+
description: A successful POST request
119+
400:
120+
description: Bad request, missing 'Book' or 'Rating' in the request body
121+
"""
122+
data = request.json()
123+
if not data:
124+
return{"error":"Request body must be in JSON format."}, 400
125+
126+
book = data.get("book")
127+
rating = data.get("rating")
128+
notes = data.get("notes","")
129+
130+
if not book or not rating:
131+
return {"error":"Both 'book' and 'rating' are required fields."}, 400
132+
133+
br.add_book_ratings(book, rating, notes)
134+
return{"message":"Book Review added successfully."},201
135+
136+
# Add the resource to the API
137+
api.add_resource(AllReview, '/all_reviews')
138+
api.add_resource(PostReview, '/post_reviews')
139+
140+
if __name__ == "__main__":
141+
app.run(debug=True)
1.62 KB
Binary file not shown.
2.38 KB
Binary file not shown.
286 Bytes
Binary file not shown.

book_review_module1.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
from dotenv import load_dotenv
3+
from pyairtable import Api
4+
5+
#this is from loading the .env file, which contains our AirTable API key.
6+
load_dotenv()
7+
env_path = '/Users/ottaaccount/Pyton_Class/python-api-example/.env'#add r in the right side of = sign for windows custom according to your own path
8+
load_dotenv(dotenv_path=env_path) # take environment variables from .env.
9+
#section ends here for the dot env load
10+
11+
#testing_variable = os.getenv("TESTING_TOKEN") just to test out getting an environmental variable
12+
13+
#to get airtable api
14+
#api_key = os.getenv("AIRTABLE_API_KEY")
15+
#print(testing_variable)
16+
#print(api_key)
17+
18+
api = Api(os.getenv("AIRTABLE_API_KEY"))#custom variable name in your dot env
19+
print(api)
20+
#table = api.table('appExampleBaseId', 'tblExampleTableId')
21+
table = api.table('apptYkb7hxYFchrJA', 'tblYAreTaEsBGzRdG')#custom according to your own path
22+
#table.all()
23+
print(table.all())
24+
25+
#to get certain section to like Rating only or Book title
26+
print(table.all()[0]['fields']['Rating'])
27+
print(table.all()[0]['fields']['Book'])
28+
print(table.all()[0]['fields']['Notes'])

book_review_module2.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
from dotenv import load_dotenv
3+
from pyairtable import Api
4+
5+
#this is from loading the .env file, which contains our AirTable API key.
6+
load_dotenv()
7+
env_path = '/Users/ottaaccount/Pyton_Class/python-api-example/.env'
8+
load_dotenv(dotenv_path=env_path) # take environment variables from .env.
9+
#section ends here for the dot env load
10+
11+
#testing_variable = os.getenv("TESTING_TOKEN") just to test out getting an environmental variable
12+
13+
#to get airtable api
14+
#api_key = os.getenv("AIRTABLE_API_KEY")
15+
#print(testing_variable)
16+
#print(api_key)
17+
18+
api = Api(os.getenv("AIRTABLE_API_KEY"))
19+
print(api)
20+
21+
#table = api.table('appExampleBaseId', 'tblExampleTableId') inside is the airtable base ID and table ID
22+
#table = api.table('apptYkb7hxYFchrJA', 'tblYAreTaEsBGzRdG')
23+
24+
#print(table.all())
25+
26+
#to get certain section to like Rating only or Book title
27+
#print(table.all()[0]['fields']['Rating'])
28+
29+
class BookReview:
30+
def __init__(self):
31+
self.api=Api(os.getenv("AIRTABLE_API_KEY"))
32+
self.table = self.api.table('apptYkb7hxYFchrJA','tblYAreTaEsBGzRdG')
33+
34+
def get_book_ratings(self):
35+
table = self.table.all()
36+
return table
37+
def add_book_ratings(self, book_title, book_rating, notes=None):
38+
pass
39+
40+
if __name__ == "__main__":
41+
br = BookReview()
42+
print(br.get_book_ratings())

book_review_module3.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import os
2+
from dotenv import load_dotenv
3+
from pyairtable import Api
4+
5+
#this is from loading the .env file, which contains our AirTable API key.
6+
load_dotenv()
7+
env_path = '/Users/ottaaccount/Pyton_Class/python-api-example/.env'
8+
load_dotenv(dotenv_path=env_path) # take environment variables from .env.
9+
#section ends here for the dot env load
10+
11+
#testing_variable = os.getenv("TESTING_TOKEN") just to test out getting an environmental variable
12+
13+
#to get airtable api
14+
#api_key = os.getenv("AIRTABLE_API_KEY")
15+
#print(testing_variable)
16+
#print(api_key)
17+
18+
api = Api(os.getenv("AIRTABLE_API_KEY"))
19+
print(api)
20+
21+
#table = api.table('appExampleBaseId', 'tblExampleTableId') inside is the airtable base ID and table ID
22+
#table = api.table('apptYkb7hxYFchrJA', 'tblYAreTaEsBGzRdG')
23+
24+
#print(table.all())
25+
26+
#to get certain section to like Rating only or Book title
27+
#print(table.all()[0]['fields']['Rating'])
28+
#print(table.all()[0]['fields']['Book'])
29+
#print(table.all()[0]['fields']['Notes'])
30+
31+
class BookReview:
32+
def __init__(self):
33+
#setting up authentication API key and which airtable documents
34+
self.api=Api(os.getenv("AIRTABLE_API_KEY")) #ini authentication ke airtablenya supaya bisa akses
35+
self.table = self.api.table('apptYkb7hxYFchrJA','tblYAreTaEsBGzRdG') # menghubungi airtable yang mana
36+
37+
def get_book_ratings(self): #store all data from airtable in a variable called table then return the value of it
38+
table = self.table.all()
39+
return table
40+
def add_book_ratings(self, book_title, book_rating, notes=None):
41+
fields = {"Book":book_title, "Rating":book_rating, "Notes":notes}
42+
self.table.create(fields=fields)
43+
44+
if __name__ == "__main__":
45+
br = BookReview()
46+
get_book_ratings = br.get_book_ratings()
47+
#print(br.add_book_ratings("Fantastic Beast : and what they gossiping about",2, "Al about some beasts hanging around and gossiping about their neighbour"))
48+
# print(br.add_book_ratings("Wilson and Fantastic Beast and how to tame them",9.5,"About Wilson catching pokemons"))
49+
print(br.add_book_ratings("Gavin Radiant Guide series 100",100,"Get to radiant under 1 seconds"))
50+
print(br.get_book_ratings())
51+

book_review_module4.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import os
2+
from dotenv import load_dotenv
3+
from pyairtable import Api
4+
5+
#this is from loading the .env file, which contains our AirTable API key.
6+
load_dotenv()
7+
env_path = '/Users/ottaaccount/Pyton_Class/python-api-example/.env'
8+
load_dotenv(dotenv_path=env_path) # take environment variables from .env.
9+
#section ends here for the dot env load
10+
11+
#testing_variable = os.getenv("TESTING_TOKEN") just to test out getting an environmental variable
12+
13+
#to get airtable api
14+
#api_key = os.getenv("AIRTABLE_API_KEY")
15+
#print(testing_variable)
16+
#print(api_key)
17+
18+
api = Api(os.getenv("AIRTABLE_API_KEY"))
19+
print(api)
20+
21+
#table = api.table('appExampleBaseId', 'tblExampleTableId') inside is the airtable base ID and table ID
22+
base_id = str("apptYkb7hxYFchrJA")
23+
table_id = str("tblYAreTaEsBGzRdG")
24+
table = api.table(base_id, table_id)
25+
26+
#print(table.all())
27+
28+
#to get certain section to like Rating only or Book title
29+
print(table.all()[0]['fields']['Rating'])
30+
31+
class BookReview:
32+
def __init__(self):
33+
self.api=Api(os.getenv("AIRTABLE_API_KEY"))
34+
self.table = self.api.table(base_id ,table_id)
35+
36+
def get_book_ratings(self, sort = None, max_records=10):
37+
"""if not sort:
38+
return self.table.all(max_records=max_records)"""
39+
if sort == "ASC":
40+
rating = ["Rating"]
41+
elif sort == "DESC":
42+
rating = ["-Rating"] #from the big to small
43+
else :
44+
return self.table.all(max_records=max_records)
45+
46+
table = self.table.all(sort=rating, max_records=max_records)
47+
return table
48+
49+
def add_book_ratings(self, book_title, book_rating, notes=None):
50+
fields = {"Book":book_title, "Rating":book_rating, "Notes":notes}
51+
self.table.create(fields=fields)
52+
53+
if __name__ == "__main__":
54+
br = BookReview()
55+
get_book_ratings = br.get_book_ratings()
56+
57+
#print(br.add_book_ratings("Fantastic Beast : and what they gossiping about",2, "Al about some beasts hanging around and gossiping about their neighbour"))
58+
"""
59+
print(get_book_ratings)
60+
get_book_ratings = br.get_book_ratings(sort= "DESC")
61+
print(get_book_ratings)
62+
"""
63+
get_book_ratings = br.get_book_ratings(sort= "ASC",max_records = 3)
64+
print(get_book_ratings)

0 commit comments

Comments
 (0)