forked from KeithGalli/python-api-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook_review.py
More file actions
61 lines (41 loc) · 1.41 KB
/
book_review.py
File metadata and controls
61 lines (41 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)
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"]
return table.all(max_records=count, sort=sort_param)
def get_record_id(name):
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:
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")
# # 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())