-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmessaging_channel.py
208 lines (165 loc) · 6.1 KB
/
messaging_channel.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
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
import json
class DSSMessagingChannelListItem(object):
"""
A generic messaging channel in DSS.
.. important::
Do not instantiate this class, use :meth:`dataikuapi.DSSClient.list_messaging_channels`
"""
def __init__(self, client, data):
self.client = client
self._data = data
if data is None:
self._data = dict()
else:
self._data = data
self._id = data.get("id", None)
self._type = data.get("type", None)
self._family = data.get("family", None)
@property
def id(self):
"""
ID of the messaging channel
:type: str
"""
return self._id
@property
def type(self):
"""
Type of the messaging channel
:type: str
"""
return self._type
@property
def family(self):
"""
Family of the messaging channel where relevant - e.g. "mail"
:type: str
"""
return self._family
def get_raw(self):
"""
:return: Gets the raw representation of this :class:`DSSMessagingChannelListItem`, any edit is reflected in the object.
:rtype: dict
"""
return self._data
def __repr__(self):
return self.__class__.__name__ + "(" + self._data.__repr__() + ")"
def get_as_messaging_channel(self):
"""
:return: The same messaging channel but as the appropriate object type
:rtype: DSSMessagingChannel
"""
if self._family == 'mail':
return DSSMailMessagingChannel(self, self._data)
else:
return DSSMessagingChannel(self, self._data)
class DSSMessagingChannel(object):
"""
A handle to interact with a messaging channel on the DSS instance.
A generic DSS messaging channel
.. important::
Do not instantiate this class directly, use :meth:`dataikuapi.DSSClient.get_messaging_channel`
"""
def __init__(self, client, data=None):
self.client = client
if data is None:
self._data = dict()
else:
self._data = data
self._id = data.get("id", None)
self._type = data.get("type", None)
self._family = data.get("family", None)
@property
def id(self):
"""
ID of the messaging channel
:rtype: str
"""
return self._id
@property
def type(self):
"""
Type of the messaging channel
:rtype: str
"""
return self._type
@property
def family(self):
"""
Family of the messaging channel where relevant - e.g. "mail"
:type: str
"""
return self._family
class DSSMailMessagingChannel(DSSMessagingChannel):
"""
A handle to interact with an email messaging channel on the DSS instance -
a subclass of :class:`DSSMessagingChannel`
.. important::
Do not instantiate this class directly, use :meth:`dataikuapi.DSSClient.get_messaging_channel`
"""
def __init__(self, client, data):
super().__init__(client, data)
self._sender = data.get("sender", None)
self._use_current_user_as_sender = data.get("useCurrentUserAsSender", None)
@property
def sender(self):
"""
Sender for the messaging channel, if present
:rtype: str
"""
return self._sender
@property
def use_current_user_as_sender(self):
"""
Indicates whether the messaging channel will use the address of the current user as sender.
If True and the current user has no associated email address, the sender property is used instead.
:rtype: bool
"""
return self._use_current_user_as_sender
def send(self, project_key, to, subject, body, attachments=None, plain_text=False, sender=None, cc=None, bcc=None):
"""
Send an email with or without attachments to a list of recipients
.. code-block:: python
channel = client.get_messaging_channel("mail-channel-id")
channel.send("PROJECT_KEY", ["[email protected]", "[email protected]"], "Hello there!", "<html><body>Some HTML body</body></html>")
channel = client.get_messaging_channel("other-mail-channel-id")
for file in paths:
with open(file) as f:
# Optionally include file type ("text/csv")
attachments.append(file, f.read(), "text/csv")
channel.send("PROJECT_KEY", ["[email protected]"], "Subject", "Body in plain text", attachments=attachments, False)
:param project_key: project issuing the email. The user must have "Write content" permission on the specified project.
:type project_key: str
:param to: email addresses of recipients
:type to: list[str]
:param subject: email subject
:type subject: str
:param body: email body (in plain text or HTML format)
:type body: str
:param attachments: files to be attached to the mail, defaults to None
:type attachments: list[BufferedReader]
:param plain_text: True to send email as plain text, False to send it as HTML. Defaults to False.
:type plain_text: bool
:param sender: sender email address. Use None to use the sender defined at the channel level.
:type sender: str
:param cc: email addresses of recipients in carbon copy
:type cc: list[str]
:param bcc: email addresses of recipients in blind carbon copy
:type bcc: list[str]
"""
payload = {
"from": sender,
"to": to,
"cc": cc,
"bcc": bcc,
"subject": subject,
"body": body,
"plainText": plain_text
}
files = [
('message', ('send-payload.json', json.dumps(payload), 'application/json')),
]
if attachments:
for attachment in attachments:
files.append(('attachments', attachment))
self.client._perform_http("POST", "/messaging-channels/%s/actions/send?projectKey=%s" % (self.id, project_key), stream=False, files=files)