Skip to content

Commit e6d2b5c

Browse files
committed
add auth_as_user method
1 parent 798a38c commit e6d2b5c

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

seatable_api/exception.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ class AuthExpiredError(ConnectionError):
44

55
def __str__(self):
66
return "The authorization has been expired"
7+
8+
class UserAuthMissingError(Exception):
9+
10+
def __str__(self):
11+
return 'User authorization is missing'

seatable_api/main.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# https://requests.readthedocs.io
99
import requests
1010

11-
from seatable_api.exception import AuthExpiredError
11+
from seatable_api.exception import AuthExpiredError, UserAuthMissingError
1212
from seatable_api.message import get_sender_by_account
1313
from .constants import ROW_FILTER_KEYS, ColumnTypes
1414
from .constants import RENAME_COLUMN, RESIZE_COLUMN, FREEZE_COLUMN, MOVE_COLUMN, MODIFY_COLUMN_TYPE, DELETE_COLUMN
@@ -64,8 +64,10 @@ def __init__(self, token, server_url):
6464
self.dtable_server_url = None
6565
self.dtable_db_url = None
6666
self.jwt_token = None
67+
self.user_jwt_token = None
6768
self.jwt_exp = None
6869
self.headers = None
70+
self.user_headers = None
6971
self.workspace_id = None
7072
self.dtable_uuid = None
7173
self.dtable_name = None
@@ -118,6 +120,24 @@ def auth(self, with_socket_io=False):
118120
self.socketIO = SocketIO(base)
119121
self.socketIO._connect()
120122

123+
def auth_as_user(self, login, password):
124+
"""Auth to SeaTable as user
125+
"""
126+
self.jwt_exp = datetime.now() + timedelta(days=3)
127+
url = self.server_url + '/api/v2.1/dtable/app-user-access-token/'
128+
headers = parse_headers(self.token)
129+
data = {'login': login, 'password': password}
130+
response = requests.post(url, json=data, headers=headers, timeout=self.timeout)
131+
data = parse_response(response)
132+
133+
self.dtable_server_url = parse_server_url(data.get('dtable_server'))
134+
self.dtable_db_url = parse_server_url(data.get('dtable_db', ''))
135+
self.user_jwt_token = data.get('access_token')
136+
self.user_headers = parse_headers(self.user_jwt_token)
137+
self.workspace_id = data.get('workspace_id')
138+
self.dtable_uuid = data.get('dtable_uuid')
139+
self.dtable_name = data.get('dtable_name')
140+
121141
def _metadata_server_url(self):
122142
return self.dtable_server_url + '/api/v1/dtables/' + self.dtable_uuid + '/metadata/'
123143

@@ -232,7 +252,7 @@ def _update_or_delete_comment_url(self, comment_id):
232252

233253
def _get_comments_within_days_url(self):
234254
url = '%(server_url)s/api/v2.1/dtables/%(dtable_uuid)s/comments-within-days/' % {
235-
'server_url': self.dtable_server_url,
255+
'server_url': self.server_url,
236256
'dtable_uuid': self.dtable_uuid
237257
}
238258
return url
@@ -996,10 +1016,12 @@ def add_comment(self, table_id, row_id, comment):
9961016
:param comment: str
9971017
:return: success response dict {'success': True}
9981018
"""
1019+
if not self.user_headers:
1020+
raise UserAuthMissingError('Please call Base.auth_as_user before call this method')
9991021
url = self._add_comment_url()
10001022
params = {'table_id': table_id, 'row_id': row_id}
10011023
json_data = {'comment': str(comment)}
1002-
response = requests.post(url, params=params, json=json_data, headers=self.headers, timeout=self.timeout)
1024+
response = requests.post(url, params=params, json=json_data, headers=self.user_headers, timeout=self.timeout)
10031025
return parse_response(response)
10041026

10051027
def get_comments_count(self, row_id):
@@ -1009,7 +1031,8 @@ def get_comments_count(self, row_id):
10091031
"""
10101032
url = self._get_comments_count_url()
10111033
params = {'row_id': row_id}
1012-
response = requests.get(url, params=params, headers=self.headers, timeout=self.timeout)
1034+
headers = self.headers or self.user_headers or None
1035+
response = requests.get(url, params=params, headers=headers, timeout=self.timeout)
10131036
return parse_response(response)['count']
10141037

10151038
def get_comments(self, row_id, page=1, per_page=25):
@@ -1025,7 +1048,8 @@ def get_comments(self, row_id, page=1, per_page=25):
10251048
'page': page,
10261049
'per_page': per_page
10271050
}
1028-
response = requests.get(url, params=params, headers=self.headers, timeout=self.timeout)
1051+
headers = self.headers or self.user_headers or None
1052+
response = requests.get(url, params=params, headers=headers, timeout=self.timeout)
10291053
return parse_response(response)
10301054

10311055
def resolve_comment(self, comment_id, resolved=True):
@@ -1034,19 +1058,23 @@ def resolve_comment(self, comment_id, resolved=True):
10341058
:param resolved: bool
10351059
:return: success response dict {'success': True}
10361060
"""
1061+
if not self.user_headers:
1062+
raise UserAuthMissingError('Please call Base.auth_as_user before call this method')
10371063
url = self._update_or_delete_comment_url(comment_id)
1038-
options = {'resovled': 'true' if resolved else 'false'}
1064+
options = {'resolved': 'true' if resolved else 'false'}
10391065
data = {'options': options}
1040-
response = requests.get(url, json=data, headers=self.headers, timeout=self.timeout)
1066+
response = requests.put(url, json=data, headers=self.user_headers, timeout=self.timeout)
10411067
return parse_response(response)
10421068

10431069
def delete_comment(self, comment_id):
10441070
"""
10451071
:param comment_id: str
10461072
:return: success response dict {'success': True}
10471073
"""
1074+
if not self.user_headers:
1075+
raise UserAuthMissingError('Please call Base.auth_as_user before call this method')
10481076
url = self._update_or_delete_comment_url(comment_id)
1049-
response = requests.delete(url, headers=self.headers, timeout=self.timeout)
1077+
response = requests.delete(url, headers=self.user_headers, timeout=self.timeout)
10501078
return parse_response(response)
10511079

10521080
def get_comments_within_days(self, days=3):
@@ -1056,7 +1084,8 @@ def get_comments_within_days(self, days=3):
10561084
"""
10571085
url = self._get_comments_within_days_url()
10581086
params = {'days': days}
1059-
response = requests.get(url, params=params, headers=self.headers, timeout=self.timeout)
1087+
headers = self.headers or self.user_headers or None
1088+
response = requests.get(url, params=params, headers=headers, timeout=self.timeout)
10601089
return parse_response(response)['comment_list']
10611090

10621091

0 commit comments

Comments
 (0)