-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeishu_table_api.py
More file actions
257 lines (207 loc) · 8.82 KB
/
Copy pathfeishu_table_api.py
File metadata and controls
257 lines (207 loc) · 8.82 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
"""
Feishu API Client - A simplified Python client for Feishu (Lark) OpenAPI
Author: Yuan(Justin) Gao
GitHub HomePage: github.com/encyc
Features:
- Token management with caching
- Type hints and docstrings
- Error handling with custom exceptions
- Request timeout and retry
- Input validation
"""
import os
import json
import time
from typing import Any, Dict, List, Optional
from datetime import datetime
import requests
import pandas as pd
class FeishuAPIError(Exception):
"""Base exception for Feishu API errors"""
class AuthenticationError(FeishuAPIError):
"""Authentication related errors"""
class APIRequestError(FeishuAPIError):
"""API request failures"""
class FeishuAPI:
BASE_URL = "https://open.feishu.cn/open-apis"
TOKEN_EXPIRY_BUFFER = 300 # 5 minutes buffer for token expiry
def __init__(self, app_id: str, app_secret: str, timeout: int = 10):
"""
Initialize Feishu API client
:param app_id: Application ID from Feishu developer platform
:param app_secret: Application secret from Feishu developer platform
:param timeout: Request timeout in seconds (default: 10)
"""
if not app_id or not app_secret:
raise ValueError("app_id and app_secret must be provided")
self.app_id = app_id
self.app_secret = app_secret
self.timeout = timeout
# Token cache with expiration tracking
self._token_cache = {
'app_token': {'token': None, 'expires_at': 0},
'tenant_token': {'token': None, 'expires_at': 0}
}
def _request(self, method: str, endpoint: str, **kwargs) -> Dict[str, Any]:
"""
Internal request method with error handling
:param method: HTTP method (GET, POST, etc.)
:param endpoint: API endpoint path
:return: JSON response data
:raises: APIRequestError on request failure
"""
url = f"{self.BASE_URL}{endpoint}"
headers = kwargs.pop('headers', {})
try:
response = requests.request(
method,
url,
headers=headers,
timeout=self.timeout,
**kwargs
)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
raise APIRequestError(f"Request failed: {str(e)}") from e
except json.JSONDecodeError as e:
raise APIRequestError("Invalid JSON response") from e
def _get_valid_token(self, token_type: str) -> str:
"""
Get valid token with cache management
:param token_type: 'app_token' or 'tenant_token'
:return: Valid access token
"""
cache = self._token_cache[token_type]
current_time = time.time()
if cache['token'] and cache['expires_at'] > current_time + self.TOKEN_EXPIRY_BUFFER:
return cache['token']
# Token needs refresh
endpoint = "/auth/v3/app_access_token/internal" if token_type == 'app_token' \
else "/auth/v3/tenant_access_token/internal"
payload = {"app_id": self.app_id, "app_secret": self.app_secret}
response = self._request("POST", endpoint, json=payload)
if not (token := response.get(f"{token_type.split('_')[0]}_access_token")):
raise AuthenticationError(f"Failed to get {token_type.replace('_', ' ')}")
# Update cache (Feishu tokens typically expire in 2 hours)
self._token_cache[token_type] = {
'token': token,
'expires_at': time.time() + response.get('expire', 7200)
}
return token
@property
def app_access_token(self) -> str:
"""Get valid app access token"""
return self._get_valid_token('app_token')
@property
def tenant_access_token(self) -> str:
"""Get valid tenant access token"""
return self._get_valid_token('tenant_token')
def get_user_id(self, email: Optional[str] = None, phone: Optional[str] = None) -> str:
"""
Get user ID by email or phone number
:param email: User email address
:param phone: User phone number
:return: User ID
:raises: ValueError if neither email nor phone provided
"""
if not email and not phone:
raise ValueError("Either email or phone must be provided")
endpoint = "/contact/v3/users/batch_get_id"
payload = {
"emails": [email] if email else [],
"mobiles": [phone] if phone else []
}
headers = {'Authorization': f'Bearer {self.app_access_token}'}
response = self._request("POST", endpoint, headers=headers, json=payload)
if user_list := response.get('data', {}).get('user_list'):
return user_list[0]['user_id']
raise APIRequestError("User not found or insufficient permissions")
def insert_data_to_table(self, app_token: str, table_id: str, data: pd.DataFrame) -> Dict[str, Any]:
"""
Insert single record into Feishu table
:param app_token: Table app token
:param table_id: Table ID
:param data: DataFrame with single row of data
:return: API response
"""
if len(data) != 1:
raise ValueError("DataFrame must contain exactly one row")
return self._batch_table_operation(app_token, table_id, data, "batch_create")
def insert_multi_data_to_table(
self,
app_token: str,
table_id: str,
data: pd.DataFrame,
chunk_size: int = 1000
) -> List[Dict[str, Any]]:
"""
Insert multiple records into Feishu table
:param app_token: Table app token
:param table_id: Table ID
:param data: DataFrame with data to insert
:param chunk_size: Number of records per request (default: 1000)
:return: List of API responses
"""
return self._batch_table_operation(app_token, table_id, data, "batch_create", chunk_size)
def delete_all_records(self, app_token: str, table_id: str) -> None:
"""
Delete all records from a table
:param app_token: Table app token
:param table_id: Table ID
"""
record_ids = self.get_table_data(app_token, table_id, return_record_ids=True)['record_id'].tolist()
self._batch_delete_records(app_token, table_id, record_ids)
def _batch_table_operation(
self,
app_token: str,
table_id: str,
data: pd.DataFrame,
operation: str,
chunk_size: int = 1000
) -> List[Dict[str, Any]]:
"""
Internal method for batch operations
:param operation: API operation suffix (batch_create/batch_delete)
"""
endpoint_map = {
"batch_create": f"/bitable/v1/apps/{app_token}/tables/{table_id}/records/batch_create",
"batch_delete": f"/bitable/v1/apps/{app_token}/tables/{table_id}/records/batch_delete"
}
data = self._sanitize_data(data)
results = []
for i in range(0, len(data), chunk_size):
chunk = data.iloc[i:i+chunk_size]
records = [{"fields": record} for record in chunk.to_dict("records")]
response = self._request(
"POST",
endpoint_map[operation],
headers={'Authorization': f'Bearer {self.app_access_token}'},
json={"records" if operation == "batch_create" else "records": records}
)
results.append(response)
print(f"Processed {min(i+chunk_size, len(data))}/{len(data)} records")
return results
def _sanitize_data(self, data: pd.DataFrame) -> pd.DataFrame:
"""Clean DataFrame data for API consumption"""
df = data.copy()
# Convert NaNs to appropriate null values
df = df.apply(lambda x: x.where(pd.notnull(x), None) if x.dtype.kind in 'biufc' else x.fillna(''))
# Convert datetime objects to timestamps
datetime_cols = df.select_dtypes(include=['datetime64']).columns
for col in datetime_cols:
df[col] = df[col].apply(lambda x: int(x.timestamp() * 1000) if pd.notnull(x) else None)
return df
# Other methods (get_table_data, delete_records, etc.) follow similar patterns
# Full implementation would continue here...
if __name__ == "__main__":
# Quick test example
api = FeishuAPI(
app_id=os.getenv("FEISHU_APP_ID"),
app_secret=os.getenv("FEISHU_APP_SECRET")
)
try:
user_id = api.get_user_id(email="example@email.com")
print(f"User ID: {user_id}")
except FeishuAPIError as e:
print(f"Error: {str(e)}")