Skip to content

Commit 4be23e7

Browse files
committed
fix: added suggestions from @seratch
1 parent 16e211d commit 4be23e7

File tree

2 files changed

+55
-86
lines changed

2 files changed

+55
-86
lines changed

slack_sdk/oauth/installation_store/sqlalchemy/__init__.py

Lines changed: 53 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -222,21 +222,7 @@ def find_bot(
222222
with self.engine.connect() as conn:
223223
result: object = conn.execute(query)
224224
for row in result.mappings(): # type: ignore[attr-defined]
225-
return Bot(
226-
app_id=row["app_id"],
227-
enterprise_id=row["enterprise_id"],
228-
enterprise_name=row["enterprise_name"],
229-
team_id=row["team_id"],
230-
team_name=row["team_name"],
231-
bot_token=row["bot_token"],
232-
bot_id=row["bot_id"],
233-
bot_user_id=row["bot_user_id"],
234-
bot_scopes=row["bot_scopes"],
235-
bot_refresh_token=row["bot_refresh_token"],
236-
bot_token_expires_at=row["bot_token_expires_at"],
237-
is_enterprise_install=row["is_enterprise_install"],
238-
installed_at=row["installed_at"],
239-
)
225+
return self.build_bot_entity(row)
240226
return None
241227

242228
def find_installation(
@@ -270,33 +256,8 @@ def find_installation(
270256
with self.engine.connect() as conn:
271257
result: object = conn.execute(query)
272258
for row in result.mappings(): # type: ignore[attr-defined]
273-
installation = Installation(
274-
app_id=row["app_id"],
275-
enterprise_id=row["enterprise_id"],
276-
enterprise_name=row["enterprise_name"],
277-
enterprise_url=row["enterprise_url"],
278-
team_id=row["team_id"],
279-
team_name=row["team_name"],
280-
bot_token=row["bot_token"],
281-
bot_id=row["bot_id"],
282-
bot_user_id=row["bot_user_id"],
283-
bot_scopes=row["bot_scopes"],
284-
bot_refresh_token=row["bot_refresh_token"],
285-
bot_token_expires_at=row["bot_token_expires_at"],
286-
user_id=row["user_id"],
287-
user_token=row["user_token"],
288-
user_scopes=row["user_scopes"],
289-
user_refresh_token=row["user_refresh_token"],
290-
user_token_expires_at=row["user_token_expires_at"],
291-
# Only the incoming webhook issued in the latest installation is set in this logic
292-
incoming_webhook_url=row["incoming_webhook_url"],
293-
incoming_webhook_channel=row["incoming_webhook_channel"],
294-
incoming_webhook_channel_id=row["incoming_webhook_channel_id"],
295-
incoming_webhook_configuration_url=row["incoming_webhook_configuration_url"],
296-
is_enterprise_install=row["is_enterprise_install"],
297-
token_type=row["token_type"],
298-
installed_at=row["installed_at"],
299-
)
259+
(row)
260+
installation = self.build_installation_entity(row)
300261

301262
has_user_installation = user_id is not None and installation is not None
302263
no_bot_token_installation = installation is not None and installation.bot_token is None
@@ -366,6 +327,54 @@ def delete_installation(
366327
)
367328
conn.execute(deletion)
368329

330+
@classmethod
331+
def build_installation_entity(row) -> Installation:
332+
return Installation(
333+
app_id=row["app_id"],
334+
enterprise_id=row["enterprise_id"],
335+
enterprise_name=row["enterprise_name"],
336+
enterprise_url=row["enterprise_url"],
337+
team_id=row["team_id"],
338+
team_name=row["team_name"],
339+
bot_token=row["bot_token"],
340+
bot_id=row["bot_id"],
341+
bot_user_id=row["bot_user_id"],
342+
bot_scopes=row["bot_scopes"],
343+
bot_refresh_token=row["bot_refresh_token"],
344+
bot_token_expires_at=row["bot_token_expires_at"],
345+
user_id=row["user_id"],
346+
user_token=row["user_token"],
347+
user_scopes=row["user_scopes"],
348+
user_refresh_token=row["user_refresh_token"],
349+
user_token_expires_at=row["user_token_expires_at"],
350+
# Only the incoming webhook issued in the latest installation is set in this logic
351+
incoming_webhook_url=row["incoming_webhook_url"],
352+
incoming_webhook_channel=row["incoming_webhook_channel"],
353+
incoming_webhook_channel_id=row["incoming_webhook_channel_id"],
354+
incoming_webhook_configuration_url=row["incoming_webhook_configuration_url"],
355+
is_enterprise_install=row["is_enterprise_install"],
356+
token_type=row["token_type"],
357+
installed_at=row["installed_at"],
358+
)
359+
360+
@classmethod
361+
def build_bot_entity(row) -> Bot:
362+
return Bot(
363+
app_id=row["app_id"],
364+
enterprise_id=row["enterprise_id"],
365+
enterprise_name=row["enterprise_name"],
366+
team_id=row["team_id"],
367+
team_name=row["team_name"],
368+
bot_token=row["bot_token"],
369+
bot_id=row["bot_id"],
370+
bot_user_id=row["bot_user_id"],
371+
bot_scopes=row["bot_scopes"],
372+
bot_refresh_token=row["bot_refresh_token"],
373+
bot_token_expires_at=row["bot_token_expires_at"],
374+
is_enterprise_install=row["is_enterprise_install"],
375+
installed_at=row["installed_at"],
376+
)
377+
369378

370379
class AsyncSQLAlchemyInstallationStore(AsyncInstallationStore):
371380
default_bots_table_name: str = "slack_bots"
@@ -493,21 +502,7 @@ async def async_find_bot(
493502
async with self.engine.connect() as conn:
494503
result: object = await conn.execute(query)
495504
for row in result.mappings(): # type: ignore[attr-defined]
496-
return Bot(
497-
app_id=row["app_id"],
498-
enterprise_id=row["enterprise_id"],
499-
enterprise_name=row["enterprise_name"],
500-
team_id=row["team_id"],
501-
team_name=row["team_name"],
502-
bot_token=row["bot_token"],
503-
bot_id=row["bot_id"],
504-
bot_user_id=row["bot_user_id"],
505-
bot_scopes=row["bot_scopes"],
506-
bot_refresh_token=row["bot_refresh_token"],
507-
bot_token_expires_at=row["bot_token_expires_at"],
508-
is_enterprise_install=row["is_enterprise_install"],
509-
installed_at=row["installed_at"],
510-
)
505+
return SQLAlchemyInstallationStore.build_bot_entity(row)
511506
return None
512507

513508
async def async_find_installation(
@@ -541,33 +536,7 @@ async def async_find_installation(
541536
async with self.engine.connect() as conn:
542537
result: object = await conn.execute(query)
543538
for row in result.mappings(): # type: ignore[attr-defined]
544-
installation = Installation(
545-
app_id=row["app_id"],
546-
enterprise_id=row["enterprise_id"],
547-
enterprise_name=row["enterprise_name"],
548-
enterprise_url=row["enterprise_url"],
549-
team_id=row["team_id"],
550-
team_name=row["team_name"],
551-
bot_token=row["bot_token"],
552-
bot_id=row["bot_id"],
553-
bot_user_id=row["bot_user_id"],
554-
bot_scopes=row["bot_scopes"],
555-
bot_refresh_token=row["bot_refresh_token"],
556-
bot_token_expires_at=row["bot_token_expires_at"],
557-
user_id=row["user_id"],
558-
user_token=row["user_token"],
559-
user_scopes=row["user_scopes"],
560-
user_refresh_token=row["user_refresh_token"],
561-
user_token_expires_at=row["user_token_expires_at"],
562-
# Only the incoming webhook issued in the latest installation is set in this logic
563-
incoming_webhook_url=row["incoming_webhook_url"],
564-
incoming_webhook_channel=row["incoming_webhook_channel"],
565-
incoming_webhook_channel_id=row["incoming_webhook_channel_id"],
566-
incoming_webhook_configuration_url=row["incoming_webhook_configuration_url"],
567-
is_enterprise_install=row["is_enterprise_install"],
568-
token_type=row["token_type"],
569-
installed_at=row["installed_at"],
570-
)
539+
installation = SQLAlchemyInstallationStore.build_installation_entity(row)
571540

572541
has_user_installation = user_id is not None and installation is not None
573542
no_bot_token_installation = installation is not None and installation.bot_token is None

tests/slack_sdk/oauth/state_store/test_async_sqlalchemy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import time
1+
import asyncio
22
import unittest
33

44
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
@@ -29,6 +29,6 @@ async def test_issue_and_consume(self):
2929

3030
async def test_expiration(self):
3131
state = await self.store.async_issue()
32-
time.sleep(3)
32+
await asyncio.sleep(3)
3333
result = await self.store.async_consume(state)
3434
self.assertFalse(result)

0 commit comments

Comments
 (0)