dagster-dingtalk 0.1.3__tar.gz → 0.1.5__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dagster-dingtalk
3
- Version: 0.1.3
3
+ Version: 0.1.5
4
4
  Summary: A dagster plugin for the DingTalk
5
5
  Author: YiZixuan
6
6
  Author-email: sqkkyzx@qq.com
@@ -0,0 +1,9 @@
1
+ # noinspection PyProtectedMember
2
+ from dagster._core.libraries import DagsterLibraryRegistry
3
+
4
+ from dagster_dingtalk.resources import DingTalkResource, MultiDingTalkResource
5
+ from dagster_dingtalk.resources import DingTalkWebhookResource, MultiDingTalkWebhookResource
6
+ # from dagster_dingtalk.operations import DingTalkWebhookOp
7
+ from dagster_dingtalk.version import __version__
8
+
9
+ DagsterLibraryRegistry.register("dagster-dingtalk", __version__)
@@ -1,22 +1,21 @@
1
1
  from dagster import In, OpExecutionContext, op
2
- from pydantic import Field
3
2
 
4
3
 
5
4
  class DingTalkWebhookOp:
6
5
  @staticmethod
7
6
  @op(description="使用钉钉 Webhook 发送文本消息",
8
- config_schema={"dingtalk_webhook_key": Field(str)},
7
+ required_resource_keys={'dingtalk_webhook'},
9
8
  ins={"text": In(str)},
10
9
  )
11
10
  def send_simple_text(context: OpExecutionContext, text):
12
- webhook = getattr(context.resources, context.op_config["dingtalk_webhook_key"])
11
+ webhook = context.resources.dingtalk_webhook
13
12
  webhook.send_text(text)
14
13
 
15
14
  @staticmethod
16
15
  @op(description="使用钉钉 Webhook 发送 Markdown 消息",
17
- config_schema={"dingtalk_webhook_key": Field(str)},
16
+ required_resource_keys={'dingtalk_webhook'},
18
17
  ins={"text": In(str), "title": In(str, default_value='')},
19
18
  )
20
19
  def send_simple_markdown(context: OpExecutionContext, text, title):
21
- webhook = getattr(context.resources, context.op_config["dingtalk_webhook_key"])
20
+ webhook = context.resources.dingtalk_webhook
22
21
  webhook.send_text(text, title)
@@ -12,19 +12,19 @@ import httpx
12
12
 
13
13
  from dagster import (
14
14
  ConfigurableResource,
15
- InitResourceContext,
15
+ InitResourceContext, ResourceDependency,
16
16
  )
17
17
  from httpx import Client
18
- from pydantic import Field, PrivateAttr
18
+ from pydantic import Field, PrivateAttr, BaseModel
19
19
 
20
20
 
21
21
  class DingTalkWebhookResource(ConfigurableResource):
22
- base_url: str = Field(default="https://oapi.dingtalk.com/robot/send", description="Webhook的通用地址,无需更改")
23
22
  access_token: str = Field(description="Webhook地址中的 access_token 部分")
24
23
  secret: Optional[str] = Field(default=None, description="如使用加签安全配置,需传签名密钥")
24
+ alias: Optional[str] = Field(default=None, description="如提供别名,将来可以使用别名进行选择")
25
+ base_url: str = Field(default="https://oapi.dingtalk.com/robot/send", description="Webhook的通用地址,无需更改")
25
26
 
26
27
  def _sign_webhook_url(self):
27
-
28
28
  if self.secret is None:
29
29
  return self.webhook_url
30
30
  else:
@@ -88,6 +88,59 @@ class DingTalkWebhookResource(ConfigurableResource):
88
88
  httpx.post(url=self.webhook_url, json={"msgtype": "feedCard", "feedCard": {"links": links_data}})
89
89
 
90
90
 
91
+ # noinspection NonAsciiCharacters
92
+ class MultiDingTalkWebhookResource(ConfigurableResource):
93
+ """
94
+ 该资源提供了预先定义多个 webhook 资源,并在运行时动态选择的方法。
95
+
96
+ 使用示例:
97
+
98
+ ```
99
+ from dagster_dingtalk import DingTalkResource, MultiDingTalkResource
100
+
101
+ app_apple = DingTalkResource(AppID="apple", ClientId="", ClientSecret="")
102
+ app_book = DingTalkResource(AppID="book", ClientId="", ClientSecret="")
103
+
104
+ @op(required_resource_keys={"dingtalk"}, ins={"app_id":In(str)})
105
+ def print_app_id(context:OpExecutionContext, app_id):
106
+ dingtalk:DingTalkResource = context.resources.dingtalk
107
+ select_app = dingtalk.select_app(app_id)
108
+ context.log.info(dingtalk_app.AppName)
109
+
110
+ @job
111
+ def print_app_id_job():
112
+ print_app_id()
113
+
114
+ defs = Definitions(
115
+ jobs=[print_app_id_job],
116
+ resources={
117
+ "dingtalk": MultiDingTalkResource(
118
+ Apps=[app_apple, app_book]
119
+ )
120
+ },
121
+ )
122
+ ```
123
+
124
+ """
125
+
126
+ Webhooks: ResourceDependency[List[DingTalkWebhookResource]] = Field(description="多个 Webhook 资源的列表")
127
+
128
+ _webhooks = PrivateAttr()
129
+
130
+ def setup_for_execution(self, context: InitResourceContext) -> None:
131
+ _webhooks_token_key = {webhook.access_token:webhook for webhook in self.Webhooks}
132
+ _webhooks_alias_key = {webhook.alias:webhook for webhook in self.Webhooks if webhook.alias}
133
+ self._webhooks = _webhooks_token_key | _webhooks_alias_key
134
+
135
+ def select(self, key:str = "_FIRST_"):
136
+ try:
137
+ if key == "_FIRST_":
138
+ return self.Webhooks[0]
139
+ return self._webhooks[key]
140
+ except KeyError:
141
+ raise f"该 AccessToken 或 别名 <{key}> 不存在于提供的 Webhooks 中。请使用 DingTalkWebhookResource 定义单个 Webhook 后,将其加入 Webhooks 。"
142
+
143
+
91
144
  class DingTalkMultiClient:
92
145
  def __init__(self, access_token: str, app_id: str, agent_id: int, robot_code: str) -> None:
93
146
  self.access_token: str = access_token
@@ -99,7 +152,7 @@ class DingTalkMultiClient:
99
152
 
100
153
 
101
154
  # noinspection NonAsciiCharacters
102
- class DingTalkAPIResource(ConfigurableResource):
155
+ class DingTalkResource(ConfigurableResource):
103
156
  """
104
157
  [钉钉服务端 API](https://open.dingtalk.com/document/orgapp/api-overview) 企业内部应用部分的第三方封装。
105
158
  通过此资源,可以调用部分钉钉服务端API。
@@ -120,7 +173,7 @@ class DingTalkAPIResource(ConfigurableResource):
120
173
  def _is_dagster_maintained(cls) -> bool:
121
174
  return False
122
175
 
123
- def _get_access_token(self, context: InitResourceContext) -> str:
176
+ def _get_access_token(self, context) -> str:
124
177
  access_token_cache = Path("~/.dingtalk_cache")
125
178
 
126
179
  if access_token_cache.exists():
@@ -168,6 +221,57 @@ class DingTalkAPIResource(ConfigurableResource):
168
221
  return API_文档文件(self._client)
169
222
 
170
223
 
224
+ # noinspection NonAsciiCharacters
225
+ class MultiDingTalkResource(ConfigurableResource):
226
+ """
227
+ 该资源提供了预先定义多个应用资源,并在运行时动态选择的方法。
228
+
229
+ 使用示例:
230
+
231
+ ```
232
+ from dagster_dingtalk import DingTalkResource, MultiDingTalkResource
233
+
234
+ app_apple = DingTalkResource(AppID="apple", ClientId="", ClientSecret="")
235
+ app_book = DingTalkResource(AppID="book", ClientId="", ClientSecret="")
236
+
237
+ @op(required_resource_keys={"dingtalk"}, ins={"app_id":In(str)})
238
+ def print_app_id(context:OpExecutionContext, app_id):
239
+ dingtalk:DingTalkResource = context.resources.dingtalk
240
+ select_app = dingtalk.select_app(app_id)
241
+ context.log.info(dingtalk_app.AppName)
242
+
243
+ @job
244
+ def print_app_id_job():
245
+ print_app_id()
246
+
247
+ defs = Definitions(
248
+ jobs=[print_app_id_job],
249
+ resources={
250
+ "dingtalk": MultiDingTalkResource(
251
+ Apps=[app_apple, app_book]
252
+ )
253
+ },
254
+ )
255
+ ```
256
+
257
+ """
258
+
259
+ Apps: ResourceDependency[List[DingTalkResource]] = Field(description="多个单应用资源的列表")
260
+
261
+ _apps = PrivateAttr()
262
+
263
+ def setup_for_execution(self, context: InitResourceContext) -> None:
264
+ self._apps = {app.AppID:app for app in self.Apps}
265
+
266
+ def select(self, app_id:str = "_FIRST_"):
267
+ try:
268
+ if app_id == "_FIRST_":
269
+ return self.Apps[0]
270
+ return self._apps[app_id]
271
+ except KeyError:
272
+ raise f"该 AppID <{app_id}> 不存在于提供的 AppLists 中。请使用 DingTalkResource 定义单个 App 后,将其加入 AppLists 。"
273
+
274
+
171
275
  # noinspection NonAsciiCharacters
172
276
  class API_智能人事:
173
277
  def __init__(self, _client:DingTalkMultiClient):
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "dagster-dingtalk"
3
- version = "0.1.3"
3
+ version = "0.1.5"
4
4
  description = "A dagster plugin for the DingTalk"
5
5
  authors = ["YiZixuan <sqkkyzx@qq.com>"]
6
6
  readme = "README.md"
@@ -1,8 +0,0 @@
1
- # noinspection PyProtectedMember
2
- from dagster._core.libraries import DagsterLibraryRegistry
3
-
4
- from dagster_dingtalk.resources import DingTalkAPIResource, DingTalkWebhookResource
5
- from dagster_dingtalk.operations import DingTalkWebhookOp
6
- from dagster_dingtalk.version import __version__
7
-
8
- DagsterLibraryRegistry.register("dagster-dingtalk", __version__)