Skip to content

Commit bc364f2

Browse files
committed
Add save_bot methods
1 parent 96ba35f commit bc364f2

10 files changed

Lines changed: 161 additions & 93 deletions

File tree

slack_sdk/oauth/installation_store/amazon_s3/__init__.py

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,84 +38,99 @@ def logger(self) -> Logger:
3838
async def async_save(self, installation: Installation):
3939
return self.save(installation)
4040

41+
async def async_save_bot(self, bot: Bot):
42+
return self.save_bot(bot)
43+
4144
def save(self, installation: Installation):
4245
none = "none"
4346
e_id = installation.enterprise_id or none
4447
t_id = installation.team_id or none
4548
workspace_path = f"{self.client_id}/{e_id}-{t_id}"
4649

50+
self.save_bot(installation.to_bot())
51+
4752
if self.historical_data_enabled:
4853
history_version: str = str(installation.installed_at)
49-
entity: str = json.dumps(installation.to_bot().__dict__)
54+
55+
# per workspace
56+
entity: str = json.dumps(installation.__dict__)
5057
response = self.s3_client.put_object(
5158
Bucket=self.bucket_name,
5259
Body=entity,
53-
Key=f"{workspace_path}/bot-latest",
60+
Key=f"{workspace_path}/installer-latest",
5461
)
5562
self.logger.debug(f"S3 put_object response: {response}")
5663
response = self.s3_client.put_object(
5764
Bucket=self.bucket_name,
5865
Body=entity,
59-
Key=f"{workspace_path}/bot-{history_version}",
66+
Key=f"{workspace_path}/installer-{history_version}",
6067
)
6168
self.logger.debug(f"S3 put_object response: {response}")
6269

63-
# per workspace
70+
# per workspace per user
71+
u_id = installation.user_id or none
6472
entity: str = json.dumps(installation.__dict__)
6573
response = self.s3_client.put_object(
6674
Bucket=self.bucket_name,
6775
Body=entity,
68-
Key=f"{workspace_path}/installer-latest",
76+
Key=f"{workspace_path}/installer-{u_id}-latest",
6977
)
7078
self.logger.debug(f"S3 put_object response: {response}")
7179
response = self.s3_client.put_object(
7280
Bucket=self.bucket_name,
7381
Body=entity,
74-
Key=f"{workspace_path}/installer-{history_version}",
82+
Key=f"{workspace_path}/installer-{u_id}-{history_version}",
7583
)
7684
self.logger.debug(f"S3 put_object response: {response}")
7785

78-
# per workspace per user
79-
u_id = installation.user_id or none
86+
else:
87+
# per workspace
8088
entity: str = json.dumps(installation.__dict__)
8189
response = self.s3_client.put_object(
8290
Bucket=self.bucket_name,
8391
Body=entity,
84-
Key=f"{workspace_path}/installer-{u_id}-latest",
92+
Key=f"{workspace_path}/installer-latest",
8593
)
8694
self.logger.debug(f"S3 put_object response: {response}")
95+
96+
# per workspace per user
97+
u_id = installation.user_id or none
98+
entity: str = json.dumps(installation.__dict__)
8799
response = self.s3_client.put_object(
88100
Bucket=self.bucket_name,
89101
Body=entity,
90-
Key=f"{workspace_path}/installer-{u_id}-{history_version}",
102+
Key=f"{workspace_path}/installer-{u_id}-latest",
91103
)
92104
self.logger.debug(f"S3 put_object response: {response}")
93105

94-
else:
95-
entity: str = json.dumps(installation.to_bot().__dict__)
106+
def save_bot(self, bot: Bot):
107+
none = "none"
108+
e_id = bot.enterprise_id or none
109+
t_id = bot.team_id or none
110+
workspace_path = f"{self.client_id}/{e_id}-{t_id}"
111+
112+
if self.historical_data_enabled:
113+
history_version: str = str(bot.installed_at)
114+
entity: str = json.dumps(bot.__dict__)
96115
response = self.s3_client.put_object(
97116
Bucket=self.bucket_name,
98117
Body=entity,
99118
Key=f"{workspace_path}/bot-latest",
100119
)
101120
self.logger.debug(f"S3 put_object response: {response}")
102-
103-
# per workspace
104-
entity: str = json.dumps(installation.__dict__)
105121
response = self.s3_client.put_object(
106122
Bucket=self.bucket_name,
107123
Body=entity,
108-
Key=f"{workspace_path}/installer-latest",
124+
Key=f"{workspace_path}/bot-{history_version}",
109125
)
110126
self.logger.debug(f"S3 put_object response: {response}")
111127

112-
# per workspace per user
113-
u_id = installation.user_id or none
114-
entity: str = json.dumps(installation.__dict__)
128+
else:
129+
entity: str = json.dumps(bot.__dict__)
115130
response = self.s3_client.put_object(
116131
Bucket=self.bucket_name,
117132
Body=entity,
118-
Key=f"{workspace_path}/installer-{u_id}-latest",
133+
Key=f"{workspace_path}/bot-latest",
119134
)
120135
self.logger.debug(f"S3 put_object response: {response}")
121136

slack_sdk/oauth/installation_store/async_cacheable_installation_store.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ async def async_save(self, installation: Installation):
3636
self.cached_installations.pop(key)
3737
return await self.underlying.async_save(installation)
3838

39+
async def async_save_bot(self, bot: Bot):
40+
# Invalidate cache data for update operations
41+
key = f"{bot.enterprise_id or ''}-{bot.team_id or ''}"
42+
if key in self.cached_bots:
43+
self.cached_bots.pop(key)
44+
return await self.underlying.async_save_bot(bot)
45+
3946
async def async_find_bot(
4047
self,
4148
*,

slack_sdk/oauth/installation_store/async_installation_store.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ def logger(self) -> Logger:
3232
raise NotImplementedError()
3333

3434
async def async_save(self, installation: Installation):
35-
"""Saves a new installation data"""
35+
"""Saves an installation data"""
36+
raise NotImplementedError()
37+
38+
async def async_save_bot(self, bot: Bot):
39+
"""Saves a bot installation data"""
3640
raise NotImplementedError()
3741

3842
async def async_find_bot(

slack_sdk/oauth/installation_store/cacheable_installation_store.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ def save(self, installation: Installation):
3535

3636
return self.underlying.save(installation)
3737

38+
def save_bot(self, bot: Bot):
39+
# Invalidate cache data for update operations
40+
key = f"{bot.enterprise_id or ''}-{bot.team_id or ''}"
41+
if key in self.cached_bots:
42+
self.cached_bots.pop(key)
43+
return self.underlying.save_bot(bot)
44+
3845
def find_bot(
3946
self,
4047
*,

slack_sdk/oauth/installation_store/file/__init__.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,21 @@ def logger(self) -> Logger:
3939
async def async_save(self, installation: Installation):
4040
return self.save(installation)
4141

42+
async def async_save_bot(self, bot: Bot):
43+
return self.save_bot(bot)
44+
4245
def save(self, installation: Installation):
4346
none = "none"
4447
e_id = installation.enterprise_id or none
4548
t_id = installation.team_id or none
4649
team_installation_dir = f"{self.base_dir}/{e_id}-{t_id}"
4750
self._mkdir(team_installation_dir)
4851

52+
self.save_bot(installation.to_bot())
53+
4954
if self.historical_data_enabled:
5055
history_version: str = str(installation.installed_at)
5156

52-
entity: str = json.dumps(installation.to_bot().__dict__)
53-
with open(f"{team_installation_dir}/bot-latest", "w") as f:
54-
f.write(entity)
55-
with open(f"{team_installation_dir}/bot-{history_version}", "w") as f:
56-
f.write(entity)
57-
5857
# per workspace
5958
entity: str = json.dumps(installation.__dict__)
6059
with open(f"{team_installation_dir}/installer-latest", "w") as f:
@@ -73,16 +72,32 @@ def save(self, installation: Installation):
7372
f.write(entity)
7473

7574
else:
76-
with open(f"{team_installation_dir}/bot-latest", "w") as f:
77-
entity: str = json.dumps(installation.to_bot().__dict__)
78-
f.write(entity)
79-
8075
u_id = installation.user_id or none
8176
installer_filepath = f"{team_installation_dir}/installer-{u_id}-latest"
8277
with open(installer_filepath, "w") as f:
8378
entity: str = json.dumps(installation.__dict__)
8479
f.write(entity)
8580

81+
def save_bot(self, bot: Bot):
82+
none = "none"
83+
e_id = bot.enterprise_id or none
84+
t_id = bot.team_id or none
85+
team_installation_dir = f"{self.base_dir}/{e_id}-{t_id}"
86+
self._mkdir(team_installation_dir)
87+
88+
if self.historical_data_enabled:
89+
history_version: str = str(bot.installed_at)
90+
91+
entity: str = json.dumps(bot.__dict__)
92+
with open(f"{team_installation_dir}/bot-latest", "w") as f:
93+
f.write(entity)
94+
with open(f"{team_installation_dir}/bot-{history_version}", "w") as f:
95+
f.write(entity)
96+
else:
97+
with open(f"{team_installation_dir}/bot-latest", "w") as f:
98+
entity: str = json.dumps(bot.__dict__)
99+
f.write(entity)
100+
86101
async def async_find_bot(
87102
self,
88103
*,

slack_sdk/oauth/installation_store/installation_store.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ def logger(self) -> Logger:
3636
raise NotImplementedError()
3737

3838
def save(self, installation: Installation):
39-
"""Saves a new installation data"""
39+
"""Saves an installation data"""
40+
raise NotImplementedError()
41+
42+
def save_bot(self, bot: Bot):
43+
"""Saves a bot installation data"""
4044
raise NotImplementedError()
4145

4246
def find_bot(

slack_sdk/oauth/installation_store/sqlalchemy/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,13 @@ def save(self, installation: Installation):
168168
)
169169
conn.execute(update_statement, i)
170170

171+
# bots
172+
self.save_bot(installation.to_bot())
173+
174+
def save_bot(self, bot: Bot):
175+
with self.engine.begin() as conn:
171176
# bots
172-
b = installation.to_bot().to_dict()
177+
b = bot.to_dict()
173178
b["client_id"] = self.client_id
174179

175180
b_column = self.bots.c
@@ -178,8 +183,8 @@ def save(self, installation: Installation):
178183
.where(
179184
and_(
180185
b_column.client_id == self.client_id,
181-
b_column.enterprise_id == installation.enterprise_id,
182-
b_column.team_id == installation.team_id,
186+
b_column.enterprise_id == bot.enterprise_id,
187+
b_column.team_id == bot.team_id,
183188
b_column.installed_at == b.get("installed_at"),
184189
)
185190
)

slack_sdk/oauth/installation_store/sqlite3/__init__.py

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -130,58 +130,11 @@ def create_tables(self):
130130
async def async_save(self, installation: Installation):
131131
return self.save(installation)
132132

133+
async def async_save_bot(self, bot: Bot):
134+
return self.save_bot(bot)
135+
133136
def save(self, installation: Installation):
134137
with self.connect() as conn:
135-
conn.execute(
136-
"""
137-
insert into slack_bots (
138-
client_id,
139-
app_id,
140-
enterprise_id,
141-
enterprise_name,
142-
team_id,
143-
team_name,
144-
bot_token,
145-
bot_id,
146-
bot_user_id,
147-
bot_scopes,
148-
bot_refresh_token, -- since v3.8
149-
bot_token_expires_at, -- since v3.8
150-
is_enterprise_install
151-
)
152-
values
153-
(
154-
?,
155-
?,
156-
?,
157-
?,
158-
?,
159-
?,
160-
?,
161-
?,
162-
?,
163-
?,
164-
?,
165-
?,
166-
?
167-
);
168-
""",
169-
[
170-
self.client_id,
171-
installation.app_id,
172-
installation.enterprise_id or "",
173-
installation.enterprise_name,
174-
installation.team_id or "",
175-
installation.team_name,
176-
installation.bot_token,
177-
installation.bot_id,
178-
installation.bot_user_id,
179-
",".join(installation.bot_scopes),
180-
installation.bot_refresh_token,
181-
installation.bot_token_expires_at,
182-
installation.is_enterprise_install,
183-
],
184-
)
185138
conn.execute(
186139
"""
187140
insert into slack_installations (
@@ -272,6 +225,62 @@ def save(self, installation: Installation):
272225
)
273226
conn.commit()
274227

228+
self.save_bot(installation.to_bot())
229+
230+
def save_bot(self, bot: Bot):
231+
with self.connect() as conn:
232+
conn.execute(
233+
"""
234+
insert into slack_bots (
235+
client_id,
236+
app_id,
237+
enterprise_id,
238+
enterprise_name,
239+
team_id,
240+
team_name,
241+
bot_token,
242+
bot_id,
243+
bot_user_id,
244+
bot_scopes,
245+
bot_refresh_token, -- since v3.8
246+
bot_token_expires_at, -- since v3.8
247+
is_enterprise_install
248+
)
249+
values
250+
(
251+
?,
252+
?,
253+
?,
254+
?,
255+
?,
256+
?,
257+
?,
258+
?,
259+
?,
260+
?,
261+
?,
262+
?,
263+
?
264+
);
265+
""",
266+
[
267+
self.client_id,
268+
bot.app_id,
269+
bot.enterprise_id or "",
270+
bot.enterprise_name,
271+
bot.team_id or "",
272+
bot.team_name,
273+
bot.bot_token,
274+
bot.bot_id,
275+
bot.bot_user_id,
276+
",".join(bot.bot_scopes),
277+
bot.bot_refresh_token,
278+
bot.bot_token_expires_at,
279+
bot.is_enterprise_install,
280+
],
281+
)
282+
conn.commit()
283+
275284
async def async_find_bot(
276285
self,
277286
*,

0 commit comments

Comments
 (0)