Skip to content

Commit

Permalink
Add save_bot methods
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Jul 15, 2021
1 parent 96ba35f commit bc364f2
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 93 deletions.
55 changes: 35 additions & 20 deletions slack_sdk/oauth/installation_store/amazon_s3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,84 +38,99 @@ def logger(self) -> Logger:
async def async_save(self, installation: Installation):
return self.save(installation)

async def async_save_bot(self, bot: Bot):
return self.save_bot(bot)

def save(self, installation: Installation):
none = "none"
e_id = installation.enterprise_id or none
t_id = installation.team_id or none
workspace_path = f"{self.client_id}/{e_id}-{t_id}"

self.save_bot(installation.to_bot())

if self.historical_data_enabled:
history_version: str = str(installation.installed_at)
entity: str = json.dumps(installation.to_bot().__dict__)

# per workspace
entity: str = json.dumps(installation.__dict__)
response = self.s3_client.put_object(
Bucket=self.bucket_name,
Body=entity,
Key=f"{workspace_path}/bot-latest",
Key=f"{workspace_path}/installer-latest",
)
self.logger.debug(f"S3 put_object response: {response}")
response = self.s3_client.put_object(
Bucket=self.bucket_name,
Body=entity,
Key=f"{workspace_path}/bot-{history_version}",
Key=f"{workspace_path}/installer-{history_version}",
)
self.logger.debug(f"S3 put_object response: {response}")

# per workspace
# per workspace per user
u_id = installation.user_id or none
entity: str = json.dumps(installation.__dict__)
response = self.s3_client.put_object(
Bucket=self.bucket_name,
Body=entity,
Key=f"{workspace_path}/installer-latest",
Key=f"{workspace_path}/installer-{u_id}-latest",
)
self.logger.debug(f"S3 put_object response: {response}")
response = self.s3_client.put_object(
Bucket=self.bucket_name,
Body=entity,
Key=f"{workspace_path}/installer-{history_version}",
Key=f"{workspace_path}/installer-{u_id}-{history_version}",
)
self.logger.debug(f"S3 put_object response: {response}")

# per workspace per user
u_id = installation.user_id or none
else:
# per workspace
entity: str = json.dumps(installation.__dict__)
response = self.s3_client.put_object(
Bucket=self.bucket_name,
Body=entity,
Key=f"{workspace_path}/installer-{u_id}-latest",
Key=f"{workspace_path}/installer-latest",
)
self.logger.debug(f"S3 put_object response: {response}")

# per workspace per user
u_id = installation.user_id or none
entity: str = json.dumps(installation.__dict__)
response = self.s3_client.put_object(
Bucket=self.bucket_name,
Body=entity,
Key=f"{workspace_path}/installer-{u_id}-{history_version}",
Key=f"{workspace_path}/installer-{u_id}-latest",
)
self.logger.debug(f"S3 put_object response: {response}")

else:
entity: str = json.dumps(installation.to_bot().__dict__)
def save_bot(self, bot: Bot):
none = "none"
e_id = bot.enterprise_id or none
t_id = bot.team_id or none
workspace_path = f"{self.client_id}/{e_id}-{t_id}"

if self.historical_data_enabled:
history_version: str = str(bot.installed_at)
entity: str = json.dumps(bot.__dict__)
response = self.s3_client.put_object(
Bucket=self.bucket_name,
Body=entity,
Key=f"{workspace_path}/bot-latest",
)
self.logger.debug(f"S3 put_object response: {response}")

# per workspace
entity: str = json.dumps(installation.__dict__)
response = self.s3_client.put_object(
Bucket=self.bucket_name,
Body=entity,
Key=f"{workspace_path}/installer-latest",
Key=f"{workspace_path}/bot-{history_version}",
)
self.logger.debug(f"S3 put_object response: {response}")

# per workspace per user
u_id = installation.user_id or none
entity: str = json.dumps(installation.__dict__)
else:
entity: str = json.dumps(bot.__dict__)
response = self.s3_client.put_object(
Bucket=self.bucket_name,
Body=entity,
Key=f"{workspace_path}/installer-{u_id}-latest",
Key=f"{workspace_path}/bot-latest",
)
self.logger.debug(f"S3 put_object response: {response}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ async def async_save(self, installation: Installation):
self.cached_installations.pop(key)
return await self.underlying.async_save(installation)

async def async_save_bot(self, bot: Bot):
# Invalidate cache data for update operations
key = f"{bot.enterprise_id or ''}-{bot.team_id or ''}"
if key in self.cached_bots:
self.cached_bots.pop(key)
return await self.underlying.async_save_bot(bot)

async def async_find_bot(
self,
*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def logger(self) -> Logger:
raise NotImplementedError()

async def async_save(self, installation: Installation):
"""Saves a new installation data"""
"""Saves an installation data"""
raise NotImplementedError()

async def async_save_bot(self, bot: Bot):
"""Saves a bot installation data"""
raise NotImplementedError()

async def async_find_bot(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ def save(self, installation: Installation):

return self.underlying.save(installation)

def save_bot(self, bot: Bot):
# Invalidate cache data for update operations
key = f"{bot.enterprise_id or ''}-{bot.team_id or ''}"
if key in self.cached_bots:
self.cached_bots.pop(key)
return self.underlying.save_bot(bot)

def find_bot(
self,
*,
Expand Down
35 changes: 25 additions & 10 deletions slack_sdk/oauth/installation_store/file/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,21 @@ def logger(self) -> Logger:
async def async_save(self, installation: Installation):
return self.save(installation)

async def async_save_bot(self, bot: Bot):
return self.save_bot(bot)

def save(self, installation: Installation):
none = "none"
e_id = installation.enterprise_id or none
t_id = installation.team_id or none
team_installation_dir = f"{self.base_dir}/{e_id}-{t_id}"
self._mkdir(team_installation_dir)

self.save_bot(installation.to_bot())

if self.historical_data_enabled:
history_version: str = str(installation.installed_at)

entity: str = json.dumps(installation.to_bot().__dict__)
with open(f"{team_installation_dir}/bot-latest", "w") as f:
f.write(entity)
with open(f"{team_installation_dir}/bot-{history_version}", "w") as f:
f.write(entity)

# per workspace
entity: str = json.dumps(installation.__dict__)
with open(f"{team_installation_dir}/installer-latest", "w") as f:
Expand All @@ -73,16 +72,32 @@ def save(self, installation: Installation):
f.write(entity)

else:
with open(f"{team_installation_dir}/bot-latest", "w") as f:
entity: str = json.dumps(installation.to_bot().__dict__)
f.write(entity)

u_id = installation.user_id or none
installer_filepath = f"{team_installation_dir}/installer-{u_id}-latest"
with open(installer_filepath, "w") as f:
entity: str = json.dumps(installation.__dict__)
f.write(entity)

def save_bot(self, bot: Bot):
none = "none"
e_id = bot.enterprise_id or none
t_id = bot.team_id or none
team_installation_dir = f"{self.base_dir}/{e_id}-{t_id}"
self._mkdir(team_installation_dir)

if self.historical_data_enabled:
history_version: str = str(bot.installed_at)

entity: str = json.dumps(bot.__dict__)
with open(f"{team_installation_dir}/bot-latest", "w") as f:
f.write(entity)
with open(f"{team_installation_dir}/bot-{history_version}", "w") as f:
f.write(entity)
else:
with open(f"{team_installation_dir}/bot-latest", "w") as f:
entity: str = json.dumps(bot.__dict__)
f.write(entity)

async def async_find_bot(
self,
*,
Expand Down
6 changes: 5 additions & 1 deletion slack_sdk/oauth/installation_store/installation_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ def logger(self) -> Logger:
raise NotImplementedError()

def save(self, installation: Installation):
"""Saves a new installation data"""
"""Saves an installation data"""
raise NotImplementedError()

def save_bot(self, bot: Bot):
"""Saves a bot installation data"""
raise NotImplementedError()

def find_bot(
Expand Down
11 changes: 8 additions & 3 deletions slack_sdk/oauth/installation_store/sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,13 @@ def save(self, installation: Installation):
)
conn.execute(update_statement, i)

# bots
self.save_bot(installation.to_bot())

def save_bot(self, bot: Bot):
with self.engine.begin() as conn:
# bots
b = installation.to_bot().to_dict()
b = bot.to_dict()
b["client_id"] = self.client_id

b_column = self.bots.c
Expand All @@ -178,8 +183,8 @@ def save(self, installation: Installation):
.where(
and_(
b_column.client_id == self.client_id,
b_column.enterprise_id == installation.enterprise_id,
b_column.team_id == installation.team_id,
b_column.enterprise_id == bot.enterprise_id,
b_column.team_id == bot.team_id,
b_column.installed_at == b.get("installed_at"),
)
)
Expand Down
109 changes: 59 additions & 50 deletions slack_sdk/oauth/installation_store/sqlite3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,58 +130,11 @@ def create_tables(self):
async def async_save(self, installation: Installation):
return self.save(installation)

async def async_save_bot(self, bot: Bot):
return self.save_bot(bot)

def save(self, installation: Installation):
with self.connect() as conn:
conn.execute(
"""
insert into slack_bots (
client_id,
app_id,
enterprise_id,
enterprise_name,
team_id,
team_name,
bot_token,
bot_id,
bot_user_id,
bot_scopes,
bot_refresh_token, -- since v3.8
bot_token_expires_at, -- since v3.8
is_enterprise_install
)
values
(
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
);
""",
[
self.client_id,
installation.app_id,
installation.enterprise_id or "",
installation.enterprise_name,
installation.team_id or "",
installation.team_name,
installation.bot_token,
installation.bot_id,
installation.bot_user_id,
",".join(installation.bot_scopes),
installation.bot_refresh_token,
installation.bot_token_expires_at,
installation.is_enterprise_install,
],
)
conn.execute(
"""
insert into slack_installations (
Expand Down Expand Up @@ -272,6 +225,62 @@ def save(self, installation: Installation):
)
conn.commit()

self.save_bot(installation.to_bot())

def save_bot(self, bot: Bot):
with self.connect() as conn:
conn.execute(
"""
insert into slack_bots (
client_id,
app_id,
enterprise_id,
enterprise_name,
team_id,
team_name,
bot_token,
bot_id,
bot_user_id,
bot_scopes,
bot_refresh_token, -- since v3.8
bot_token_expires_at, -- since v3.8
is_enterprise_install
)
values
(
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
);
""",
[
self.client_id,
bot.app_id,
bot.enterprise_id or "",
bot.enterprise_name,
bot.team_id or "",
bot.team_name,
bot.bot_token,
bot.bot_id,
bot.bot_user_id,
",".join(bot.bot_scopes),
bot.bot_refresh_token,
bot.bot_token_expires_at,
bot.is_enterprise_install,
],
)
conn.commit()

async def async_find_bot(
self,
*,
Expand Down
Loading

0 comments on commit bc364f2

Please sign in to comment.