-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path__credential_async_template.py
More file actions
221 lines (181 loc) · 6 KB
/
Copy path__credential_async_template.py
File metadata and controls
221 lines (181 loc) · 6 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
"""Credential 高层 API / Credential High-Level API
此模块定义凭证资源的高级API。
This module defines the high-level API for credential resources.
"""
from typing import List, Optional
from agentrun.utils.config import Config
from agentrun.utils.model import PageableInput
from agentrun.utils.resource import ResourceBase
from .model import (
CredentialAuthType,
CredentialCreateInput,
CredentialImmutableProps,
CredentialListInput,
CredentialListOutput,
CredentialMutableProps,
CredentialSourceType,
CredentialSystemProps,
CredentialUpdateInput,
)
class Credential(
CredentialMutableProps,
CredentialImmutableProps,
CredentialSystemProps,
ResourceBase,
):
"""凭证资源 / Credential Resource
提供凭证的完整生命周期管理,包括创建、删除、更新、查询。
Provides complete lifecycle management for credentials, including create, delete, update, and query.
"""
@classmethod
def __get_client(cls, config: Optional[Config] = None):
"""获取客户端实例 / Get client instance
Args:
config: 配置对象,可选 / Configuration object, optional
Returns:
CredentialClient: 客户端实例 / Client instance
"""
from .client import CredentialClient
return CredentialClient(config=config)
@classmethod
async def create_async(
cls, input: CredentialCreateInput, config: Optional[Config] = None
):
"""创建凭证(异步)
Args:
input: 凭证输入参数
config: 配置
Returns:
Credential: 创建的凭证对象
"""
return await cls.__get_client(config=config).create_async(
input, config=config
)
@classmethod
async def delete_by_name_async(
cls, credential_name: str, config: Optional[Config] = None
):
"""根据名称删除凭证(异步)
Args:
credential_name: 凭证名称
config: 配置
"""
return await cls.__get_client(config=config).delete_async(
credential_name, config=config
)
@classmethod
async def update_by_name_async(
cls,
credential_name: str,
input: CredentialUpdateInput,
config: Optional[Config] = None,
):
"""根据名称更新凭证(异步)
Args:
credential_name: 凭证名称
input: 凭证更新输入参数
config: 配置
Returns:
Credential: 更新后的凭证对象
"""
return await cls.__get_client(config=config).update_async(
credential_name, input, config=config
)
@classmethod
async def get_by_name_async(
cls, credential_name: str, config: Optional[Config] = None
):
"""根据名称获取凭证(异步)
Args:
credential_name: 凭证名称
config: 配置
Returns:
Credential: 凭证对象
"""
return await cls.__get_client(config=config).get_async(
credential_name, config=config
)
@classmethod
async def _list_page_async(
cls, page_input: PageableInput, config: Config | None = None, **kwargs
):
return await cls.__get_client(config=config).list_async(
input=CredentialListInput(
**kwargs,
**page_input.model_dump(),
),
config=config,
)
@classmethod
async def list_all_async(
cls,
*,
credential_auth_type: Optional[CredentialAuthType] = None,
credential_name: Optional[str] = None,
credential_source_type: Optional[CredentialSourceType] = None,
provider: Optional[str] = None,
config: Optional[Config] = None,
) -> List[CredentialListOutput]:
return await cls._list_all_async(
lambda cred: cred.credential_id or "",
config=config,
credential_auth_type=credential_auth_type,
credential_name=credential_name,
credential_source_type=credential_source_type,
provider=provider,
)
async def update_async(
self, input: CredentialUpdateInput, config: Optional[Config] = None
):
"""更新凭证(异步)
Args:
input: 凭证更新输入参数
config: 配置
Returns:
Credential: 更新后的凭证对象
"""
if self.credential_name is None:
raise ValueError(
"credential_name is required to update a Credential"
)
result = await self.update_by_name_async(
self.credential_name, input, config=config
)
self.update_self(result)
return self
async def delete_async(self, config: Optional[Config] = None):
"""删除凭证(异步)
Args:
config: 配置
"""
if self.credential_name is None:
raise ValueError(
"credential_name is required to delete a Credential"
)
return await self.delete_by_name_async(
self.credential_name, config=config
)
async def get_async(self, config: Optional[Config] = None):
"""刷新凭证信息(异步)
Args:
config: 配置
Returns:
Credential: 刷新后的凭证对象
"""
if self.credential_name is None:
raise ValueError(
"credential_name is required to refresh a Credential"
)
result = await self.get_by_name_async(
self.credential_name, config=config
)
self.update_self(result)
return self
async def refresh_async(self, config: Optional[Config] = None):
"""刷新凭证信息(异步)
Args:
config: 配置
Returns:
Credential: 刷新后的凭证对象
"""
return await self.get_async(config=config)