Test and refactors.
This commit is contained in:
parent
f684b2a043
commit
8534c727c4
4 changed files with 70 additions and 12 deletions
|
|
@ -1,4 +1,3 @@
|
|||
import json
|
||||
from typing import List
|
||||
|
||||
from prefect.storage import S3
|
||||
|
|
@ -11,6 +10,7 @@ from lolafect.defaults import (
|
|||
DEFAULT_KUBERNETES_LABELS,
|
||||
DEFAULT_FLOWS_PATH_IN_BUCKET,
|
||||
)
|
||||
from lolafect.utils import S3FileReader
|
||||
|
||||
|
||||
class LolaConfig:
|
||||
|
|
@ -62,23 +62,21 @@ class LolaConfig:
|
|||
DEFAULT_KUBERNETES_IMAGE if kubernetes_image is None else kubernetes_image
|
||||
)
|
||||
|
||||
def fetch_slack_webhooks(self, s3_client=None) -> None:
|
||||
self._s3_reader = S3FileReader(s3_client=boto3.client("s3"))
|
||||
|
||||
def fetch_slack_webhooks(self, s3_reader=None) -> None:
|
||||
"""
|
||||
Read the slack webhooks file from S3 and store the webhooks in memory.
|
||||
|
||||
:param s3_client: a client to fetch files from S3.
|
||||
:param s3_reader: a client to fetch files from S3.
|
||||
:return: None
|
||||
"""
|
||||
|
||||
if s3_client is None:
|
||||
s3_client = boto3.client("s3")
|
||||
if s3_reader is None:
|
||||
s3_reader = self._s3_reader
|
||||
|
||||
self.SLACK_WEBHOOKS = json.loads(
|
||||
s3_client.get_object(
|
||||
Bucket=self.S3_BUCKET_NAME, Key=self.SLACK_WEBHOOKS_FILE
|
||||
)["Body"]
|
||||
.read()
|
||||
.decode("utf-8")
|
||||
self.SLACK_WEBHOOKS = s3_reader.read_json_from_s3_file(
|
||||
bucket=self.S3_BUCKET_NAME, key=self.SLACK_WEBHOOKS_FILE
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
24
lolafect/utils.py
Normal file
24
lolafect/utils.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import json
|
||||
|
||||
|
||||
class S3FileReader:
|
||||
"""
|
||||
An S3 client along with a few reading utils.
|
||||
"""
|
||||
|
||||
def __init__(self, s3_client):
|
||||
self.s3_client = s3_client
|
||||
|
||||
def read_json_from_s3_file(self, bucket: str, key: str) -> dict:
|
||||
"""
|
||||
Read a JSON file from an S3 location and return contents as a dict.
|
||||
|
||||
:param bucket: the name of the bucket where the file is stored.
|
||||
:param key: the path to the file within the bucket.
|
||||
:return: the file contents.
|
||||
"""
|
||||
return json.loads(
|
||||
self.s3_client.get_object(Bucket=bucket, Key=key)["Body"]
|
||||
.read()
|
||||
.decode("utf-8")
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue