forked from slackapi/python-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_client.py
More file actions
231 lines (201 loc) · 7.64 KB
/
Copy pathbase_client.py
File metadata and controls
231 lines (201 loc) · 7.64 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
"""A Python module for iteracting with Slack's Web API."""
# Standard Imports
from urllib.parse import urljoin
import platform
import sys
import logging
import asyncio
import inspect
# ThirdParty Imports
import aiohttp
# Internal Imports
from slack.web.slack_response import SlackResponse
import slack.version as ver
import slack.errors as err
class BaseClient:
BASE_URL = "https://www.slack.com/api/"
def __init__(
self,
token,
base_url=BASE_URL,
timeout=30,
loop=None,
ssl=None,
proxy=None,
run_async=False,
session=None,
):
self.token = token
self.base_url = base_url
self.timeout = timeout
self.ssl = ssl
self.proxy = proxy
self.run_async = run_async
self.session = session
self._logger = logging.getLogger(__name__)
self._event_loop = loop
def _set_event_loop(self):
if self.run_async:
self._event_loop = asyncio.get_event_loop()
else:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
self._event_loop = loop
def api_call(
self,
api_method: str,
*,
http_verb: str = "POST",
files: dict = None,
data: dict = None,
params: dict = None,
json: dict = None,
):
"""Create a request and execute the API call to Slack.
Args:
api_method (str): The target Slack API method.
e.g. 'chat.postMessage'
http_verb (str): HTTP Verb. e.g. 'POST'
files (dict): Files to multipart upload.
e.g. {imageORfile: file_objectORfile_path}
data: The body to attach to the request. If a dictionary is
provided, form-encoding will take place.
e.g. {'key1': 'value1', 'key2': 'value2'}
params (dict): The URL parameters to append to the URL.
e.g. {'key1': 'value1', 'key2': 'value2'}
json (dict): JSON for the body to attach to the request
(if files or data is not specified).
e.g. {'key1': 'value1', 'key2': 'value2'}
Returns:
(SlackResponse)
The server's response to an HTTP request. Data
from the response can be accessed like a dict.
If the response included 'next_cursor' it can
be iterated on to execute subsequent requests.
Raises:
SlackApiError: The following Slack API call failed:
'chat.postMessage'.
SlackRequestError: Json data can only be submitted as
POST requests.
"""
if json is not None and http_verb != "POST":
msg = "Json data can only be submitted as POST requests. GET requests should use the 'params' argument."
raise err.SlackRequestError(msg)
api_url = self._get_url(api_method)
headers = {
"User-Agent": self._get_user_agent(),
"Authorization": "Bearer {}".format(self.token),
}
if files is not None:
form_data = aiohttp.FormData()
for k, v in files.items():
if isinstance(v, str):
form_data.add_field(k, open(v, "rb"))
else:
form_data.add_field(k, v)
if data is not None:
for k, v in data.items():
form_data.add_field(k, str(v))
data = form_data
req_args = {
"headers": headers,
"data": data,
"params": params,
"json": json,
"ssl": self.ssl,
"proxy": self.proxy,
}
if self._event_loop is None:
self._set_event_loop()
future = asyncio.ensure_future(
self._send(http_verb=http_verb, api_url=api_url, req_args=req_args),
loop=self._event_loop,
)
if self.run_async:
return future
return self._event_loop.run_until_complete(future)
def _validate_xoxp_token(self):
"""Ensures that an xoxp token is used when the specified method is called.
Raises:
BotUserAccessError: If the API method is called with a Bot User OAuth Access Token.
"""
if self.token.startswith("xoxb"):
method_name = inspect.stack()[1][3]
msg = "The method '{}' cannot be called with a Bot Token.".format(
method_name
)
raise err.BotUserAccessError(msg)
def _get_url(self, api_method):
"""Joins the base Slack URL and an API method to form an absolute URL.
Args:
api_method (str): The Slack Web API method. e.g. 'chat.postMessage'
Returns:
The absolute API URL.
e.g. 'https://www.slack.com/api/chat.postMessage'
"""
return urljoin(self.base_url, api_method)
async def _send(self, http_verb, api_url, req_args):
"""Sends the request out for transmission.
Args:
http_verb (str): The HTTP verb. e.g. 'GET' or 'POST'.
api_url (str): The Slack API url. e.g. 'https://slack.com/api/chat.postMessage'
req_args (dict): The request arguments to be attached to the request.
e.g.
{
json: {
'attachments': [{"pretext": "pre-hello", "text": "text-world"}],
'channel': '#random'
}
}
Returns:
The response parsed into a SlackResponse object.
"""
res = await self._request(
http_verb=http_verb, api_url=api_url, req_args=req_args
)
data = {
"client": self,
"http_verb": http_verb,
"api_url": api_url,
"req_args": req_args,
}
return SlackResponse(**{**data, **res}).validate()
async def _request(self, *, http_verb, api_url, req_args):
"""Submit the HTTP request with the running session or a new session.
Returns:
A dictionary of the response data.
"""
if self.session and not self.session.closed:
async with self.session.request(http_verb, api_url, **req_args) as res:
self._logger.debug("Ran the request with existing session.")
return {
"data": await res.json(),
"headers": res.headers,
"status_code": res.status,
}
async with aiohttp.ClientSession(
loop=self._event_loop, timeout=aiohttp.ClientTimeout(total=self.timeout)
) as session:
async with session.request(http_verb, api_url, **req_args) as res:
self._logger.debug("Ran the request with a new session.")
return {
"data": await res.json(),
"headers": res.headers,
"status_code": res.status,
}
@staticmethod
def _get_user_agent():
"""Construct the user-agent header with the package info,
Python version and OS version.
Returns:
The user agent string.
e.g. 'Python/3.6.7 slack/2.0.0 Darwin/17.7.0'
"""
# __name__ returns all classes, we only want the client
client = "{0}/{1}".format(__name__.split(".")[0], ver.__version__)
python_version = "Python/{v.major}.{v.minor}.{v.micro}".format(
v=sys.version_info
)
system_info = "{0}/{1}".format(platform.system(), platform.release())
user_agent_string = " ".join([python_version, client, system_info])
return user_agent_string