-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
71 lines (61 loc) · 2.5 KB
/
utils.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
65
66
67
68
69
70
71
NOTION_BASE_URL = 'https://api.notion.com/v1/'
import json
import requests
def get_icon(obj) -> str | None:
if not obj:
return None
if obj and 'icon' in obj and 'type' in obj['icon']:
icon = obj['icon'][obj['icon']['type']]
if obj['icon']['type'] == 'file':
# for whatever reason, notion provides us with fields we can't pass to the backend for file icons
del obj['icon'][obj['icon']['type']]['expiry_time']
return obj['icon'][obj['icon']['type']]
return None
def get_icon_type(obj) -> str | None:
if obj and 'icon' in obj and 'type' in obj['icon']:
return obj['icon']['type'] if obj['icon']['type'] != 'file' else 'external'
return None
class Bridge:
def __init__(self, api_key):
self.api_key = api_key
self.headers = self.get_headers()
def get_headers(self):
return {
'Notion-Version': '2022-06-28',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + self.api_key
}
def query(self, db_id, body):
r = requests.post(NOTION_BASE_URL + 'databases/' + db_id + '/query', headers=self.headers, data=json.dumps(body))
if r.status_code != 200:
print('UTIL: error querying URL ' + NOTION_BASE_URL + 'databases/' + db_id + '/query' + ': ')
print('received status code: ' + str(r.status_code))
raise Exception(r.json())
return r
def create_db_page(self, db_id, properties, icon, icon_type):
payload = {
'parent': {
'type': 'database_id',
'database_id': db_id,
},
'properties': properties,
}
if icon:
payload['icon'] = {
'type': icon_type,
icon_type: icon,
}
response = requests.post(NOTION_BASE_URL + 'pages', headers=self.headers, data=json.dumps(payload))
if response.status_code != 200:
print('UTIL: error encountered creating page')
print(response.json())
return response.json()
def update_db_page(self, properties_to_update, id):
payload = {
'properties': properties_to_update,
}
response = requests.patch(NOTION_BASE_URL + 'pages/' + id, headers=self.headers, data=json.dumps(payload))
if response.status_code != 200:
print('UTIL: error encountered updating page')
print(response.json())
return response