Add DynamoDb support
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -130,4 +130,6 @@ resouces/facts.txt
|
|||||||
|
|
||||||
secrets.local
|
secrets.local
|
||||||
|
|
||||||
resources/facts.txt
|
facts.txt
|
||||||
|
|
||||||
|
.idea/terminal.xml
|
||||||
|
|||||||
5
.idea/terminal.xml
generated
5
.idea/terminal.xml
generated
@@ -4,6 +4,11 @@
|
|||||||
<envs>
|
<envs>
|
||||||
<env key="AWS_TOKEN_ID" value="AKIAIIITA3BAG6LBBZ7A" />
|
<env key="AWS_TOKEN_ID" value="AKIAIIITA3BAG6LBBZ7A" />
|
||||||
<env key="AWS_TOKEN" value="OkJJ/LubZea6KbicszllpUKf6QvawCzOGxMJAaR5" />
|
<env key="AWS_TOKEN" value="OkJJ/LubZea6KbicszllpUKf6QvawCzOGxMJAaR5" />
|
||||||
|
<env key="BOT_NAME" value="Rando Facts Test" />
|
||||||
|
<env key="GROUPME_KEY" value="5ad6d5506c5e0138d774127c6b6f91ba" />
|
||||||
|
<env key="AWS_ACCESS_KEY_ID" value="AKIAIIITA3BAG6LBBZ7A" />
|
||||||
|
<env key="AWS_SECRET_KEY" value="OkJJ/LubZea6KbicszllpUKf6QvawCzOGxMJAaR5" />
|
||||||
|
<env key="FACTS_PATH" value="/var/task/facts.txt" />
|
||||||
</envs>
|
</envs>
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
1
__init__.py
Normal file
1
__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
import service
|
||||||
@@ -3,7 +3,7 @@ region: us-east-2
|
|||||||
function_name: avan_facts_bot
|
function_name: avan_facts_bot
|
||||||
handler: service.handler
|
handler: service.handler
|
||||||
description: Random Avan facts via groupme
|
description: Random Avan facts via groupme
|
||||||
runtime: python3.8
|
runtime: python3.7
|
||||||
#role: lambda_basic_execution
|
#role: lambda_basic_execution
|
||||||
|
|
||||||
# S3 upload requires appropriate role with s3:PutObject permission
|
# S3 upload requires appropriate role with s3:PutObject permission
|
||||||
@@ -25,7 +25,9 @@ aws_secret_access_key: ${AWS_TOKEN}
|
|||||||
# Experimental Environment variables
|
# Experimental Environment variables
|
||||||
environment_variables:
|
environment_variables:
|
||||||
BOT_NAME: ${BOT_NAME}
|
BOT_NAME: ${BOT_NAME}
|
||||||
|
DB_TABLE_NAME: ${DB_TABLE_NAME}
|
||||||
GROUPME_KEY: ${GROUPME_KEY}
|
GROUPME_KEY: ${GROUPME_KEY}
|
||||||
|
FACTS_PATH: ${FACTS_PATH}
|
||||||
|
|
||||||
# If `tags` is uncommented then tags will be set at creation or update
|
# If `tags` is uncommented then tags will be set at creation or update
|
||||||
# time. During an update all other tags will be removed except the tags
|
# time. During an update all other tags will be removed except the tags
|
||||||
|
|||||||
7
constants.py
Normal file
7
constants.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
BOT_NAME = os.environ.get("BOT_NAME")
|
||||||
|
DB_TABLE_NAME = os.environ.get("DB_TABLE_NAME")
|
||||||
|
FACTS_PATH = os.environ.get("FACTS_PATH")
|
||||||
|
GROUPME = os.environ.get("GROUPME_KEY")
|
||||||
|
|
||||||
34
dynamo_db.py
Normal file
34
dynamo_db.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
import boto3
|
||||||
|
|
||||||
|
import constants
|
||||||
|
from db_service import DbService
|
||||||
|
import common
|
||||||
|
|
||||||
|
|
||||||
|
class DynamoDb(DbService):
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.FACTS_LIST = []
|
||||||
|
self.dynamodb = boto3.resource('dynamodb')
|
||||||
|
self.facts_table = self.dynamodb.Table(constants.DB_TABLE_NAME)
|
||||||
|
print(self.facts_table.creation_date_time)
|
||||||
|
|
||||||
|
def load_facts_from_db(self):
|
||||||
|
# global facts_table
|
||||||
|
self.FACTS_LIST.clear()
|
||||||
|
resp = self.facts_table.scan()
|
||||||
|
self.FACTS_LIST = [i["fact"] for i in resp["Items"]]
|
||||||
|
assert self.FACTS_LIST
|
||||||
|
|
||||||
|
def get_fact(self):
|
||||||
|
if self.FACTS_LIST is None or not self.FACTS_LIST:
|
||||||
|
self.load_facts_from_db()
|
||||||
|
return random.choice(self.FACTS_LIST)
|
||||||
|
|
||||||
|
def get_multiple_facts(self, count=2):
|
||||||
|
if self.FACTS_LIST is None or not self.FACTS_LIST:
|
||||||
|
self.load_facts_from_db()
|
||||||
|
return random.sample(self.FACTS_LIST, k=count)
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
from app import fact_service, groupme
|
import fact_service
|
||||||
from util import constants
|
import groupme
|
||||||
|
import constants
|
||||||
|
|
||||||
|
|
||||||
def post_fact_to_group():
|
def post_fact_to_group():
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
from model.text_db import TextDb
|
from dynamo_db import DynamoDb
|
||||||
|
from text_db import TextDb
|
||||||
|
|
||||||
|
|
||||||
def get_random_fact():
|
def get_random_fact():
|
||||||
ret_fact = None
|
ret_fact = None
|
||||||
db_service = TextDb()
|
# db_service = TextDb()
|
||||||
|
db_service = DynamoDb()
|
||||||
ret_fact = db_service.get_fact()
|
ret_fact = db_service.get_fact()
|
||||||
return ret_fact
|
return ret_fact
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
from groupy import Client
|
from groupy import Client
|
||||||
|
|
||||||
from util import constants
|
import constants
|
||||||
|
|
||||||
GROUPME_CLIENT: Client = None
|
GROUPME_CLIENT: Client = None
|
||||||
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
from model import db_service
|
|
||||||
|
|
||||||
from model import text_db
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
boto3
|
||||||
GroupyAPI
|
GroupyAPI
|
||||||
pytest
|
pytest
|
||||||
python-lambda
|
python-lambda
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from app import fact_bot
|
import fact_bot
|
||||||
|
|
||||||
|
|
||||||
def handler(event, context):
|
def handler(event, context):
|
||||||
|
|||||||
15
test_dynamo_db.py
Normal file
15
test_dynamo_db.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
from dynamo_db import DynamoDb
|
||||||
|
|
||||||
|
|
||||||
|
class TestDynamoDb:
|
||||||
|
def test_get_fact(self):
|
||||||
|
text_db_service = DynamoDb()
|
||||||
|
random_fact = text_db_service.get_fact()
|
||||||
|
assert random_fact is not None
|
||||||
|
assert random_fact != ""
|
||||||
|
|
||||||
|
def test_get_multiple_facts(self):
|
||||||
|
text_db_service = DynamoDb()
|
||||||
|
random_facts_list = text_db_service.get_multiple_facts()
|
||||||
|
assert random_facts_list is not None
|
||||||
|
assert len(random_facts_list) > 0
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
from model.text_db import TextDb
|
from text_db import TextDb
|
||||||
|
|
||||||
|
|
||||||
class TestTestDb:
|
class TestTextDb:
|
||||||
def test_get_fact(self):
|
def test_get_fact(self):
|
||||||
text_db_service = TextDb()
|
text_db_service = TextDb()
|
||||||
random_fact = text_db_service.get_fact()
|
random_fact = text_db_service.get_fact()
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
from model.db_service import DbService
|
import constants
|
||||||
from util import common
|
from db_service import DbService
|
||||||
|
import common
|
||||||
|
|
||||||
|
|
||||||
class TextDb(DbService):
|
class TextDb(DbService):
|
||||||
@@ -11,7 +12,7 @@ class TextDb(DbService):
|
|||||||
self.FACTS_LIST = None
|
self.FACTS_LIST = None
|
||||||
|
|
||||||
def load_facts_from_file(self):
|
def load_facts_from_file(self):
|
||||||
facts_file = open(f"{common.get_project_root()}/resources/facts.txt", mode="r+")
|
facts_file = open(f"{constants.FACTS_PATH}", mode="r")
|
||||||
self.FACTS_LIST = facts_file.readlines()
|
self.FACTS_LIST = facts_file.readlines()
|
||||||
assert self.FACTS_LIST
|
assert self.FACTS_LIST
|
||||||
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
import os
|
|
||||||
|
|
||||||
BOT_NAME = os.environ.get("BOT_NAME")
|
|
||||||
GROUPME = os.environ.get("GROUPME_KEY")
|
|
||||||
Reference in New Issue
Block a user