-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaserow.py
64 lines (49 loc) · 1.37 KB
/
baserow.py
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
62
63
64
import requests
import os
import json
from dotenv import load_dotenv
import pandas as pd
import datetime
load_dotenv() # read .env
headers = {
"Content-Type": "application/json",
"Authorization": f"Token {os.environ.get('BASEROW_TOKEN')}",
}
base_url = "https://api.baserow.io/api/"
def getData(table_id, count=False):
res = requests.get(
url=f"{base_url}database/rows/table/{table_id}/?user_field_names=true",
headers=headers,
)
if res.status_code == 200:
data = res.json()
res_data = data["results"]
df = pd.DataFrame(res_data)
if count:
return int(data["count"])
return df
else:
return []
def pushData(prod_id, discount, price):
currentts = datetime.datetime.utcnow().isoformat() + "Z"
json = {
"product_id": prod_id,
"created_at": currentts,
"modified_at": currentts,
"discount": discount,
"current_price": price,
}
res = requests.post(
url=f"{base_url}database/rows/table/96385/?user_field_names=true",
headers=headers,
json=json,
)
print("push", res.status_code)
def deleteData(ids):
json = {"items": ids}
res = requests.post(
url=f"{base_url}database/rows/table/96385/batch-delete/",
headers=headers,
json=json,
)
print("delete", res.status_code)