Skip to content

Commit 8880aab

Browse files
authored
New Views API & Updates for internal request method (#540)
* Some responses don't return json. Correcting initial assumption. * Removing deprecated loop parameter * Adding new views API method.
1 parent 22ac861 commit 8880aab

3 files changed

Lines changed: 36 additions & 18 deletions

File tree

slack/rtm/client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ async def _connect_and_read(self):
323323
try:
324324
self._connection_attempts += 1
325325
async with aiohttp.ClientSession(
326-
loop=self._event_loop,
327326
timeout=aiohttp.ClientTimeout(total=self.timeout),
328327
) as session:
329328
self._session = session

slack/web/base_client.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -245,24 +245,28 @@ async def _request(self, *, http_verb, api_url, req_args):
245245
Returns:
246246
A dictionary of the response data.
247247
"""
248+
session = None
248249
if self.session and not self.session.closed:
249-
async with self.session.request(http_verb, api_url, **req_args) as res:
250-
return {
251-
"data": await res.json(),
252-
"headers": res.headers,
253-
"status_code": res.status,
254-
}
255-
async with aiohttp.ClientSession(
256-
loop=self._event_loop,
257-
timeout=aiohttp.ClientTimeout(total=self.timeout),
258-
auth=req_args.pop("auth"),
259-
) as session:
260-
async with session.request(http_verb, api_url, **req_args) as res:
261-
return {
262-
"data": await res.json(),
263-
"headers": res.headers,
264-
"status_code": res.status,
265-
}
250+
session = self.session
251+
else:
252+
session = aiohttp.ClientSession(
253+
timeout=aiohttp.ClientTimeout(total=self.timeout),
254+
auth=req_args.pop("auth", None),
255+
)
256+
257+
response = None
258+
async with session.request(http_verb, api_url, **req_args) as res:
259+
data = {}
260+
try:
261+
data = await res.json()
262+
except aiohttp.ContentTypeError:
263+
self._logger.debug(
264+
f"No response data returned from the following API call: {api_url}."
265+
)
266+
response = {"data": data, "headers": res.headers, "status_code": res.status}
267+
268+
await session.close()
269+
return response
266270

267271
@staticmethod
268272
def _get_user_agent():

slack/web/client.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,3 +1577,18 @@ def views_update(
15771577
raise e.SlackRequestError("Either view_id or external_id is required.")
15781578

15791579
return self.api_call("views.update", json=kwargs)
1580+
1581+
def views_publish(
1582+
self, *, user_id: str, view: dict, **kwargs
1583+
) -> Union[Future, SlackResponse]:
1584+
"""Publish a static view for a User.
1585+
Create or update the view that comprises an
1586+
app's Home tab (https://api.slack.com/surfaces/tabs)
1587+
for a specific user.
1588+
Args:
1589+
user_id (str): id of the user you want publish a view to.
1590+
e.g. 'U0BPQUNTA'
1591+
view (dict): The view payload.
1592+
"""
1593+
kwargs.update({"user_id": user_id, "view": view})
1594+
return self.api_call("views.publish", json=kwargs)

0 commit comments

Comments
 (0)