Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds sqlite option, sanitizes inputs #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@app.route(f'/{endpoint}', methods=["POST"])
def run():
try:
sb.SpotBot(request, tables.HamAlertTable(), discord_http.DiscordHttp()).process()
sb.SpotBot(request, tables.create_table_client(), discord_http.DiscordHttp()).process()
except Exception as _excpt:
logging.error(f"Exception occurred: {_excpt}")
return make_response(f"Exception occurred: {_excpt}", 500)
Expand Down
60 changes: 50 additions & 10 deletions tables.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import logging
import os
import sqlite3
from azure.data.tables import TableServiceClient
from azure.data.tables import UpdateMode

class HamAlertTable:
def __init__(self):
connection_string = os.getenv('AzureWebJobsStorage')
table_name = os.getenv('TABLE_NAME')
table_service_client = TableServiceClient.from_connection_string(conn_str=connection_string)
self.table_client = table_service_client.get_table_client(table_name=table_name)
class BaseAlertTable:
def query_for_entity(self, callsign):
raise NotImplementedError

def upsert_entity(self, callsign, messageId):
raise NotImplementedError

def initialize_table(self):
class HamAlertAzureTable(BaseAlertTable):
def __init__(self):
connection_string = os.getenv('AzureWebJobsStorage')
table_name = os.getenv('TABLE_NAME')
table_service_client = TableServiceClient.from_connection_string(conn_str=connection_string)
self.table_client = table_service_client.get_table_client(table_name=table_name)

def query_for_entity(self, callsign):
entities = [ent for ent in self.table_client.query_entities(f"PartitionKey eq '{callsign}' and RowKey eq '{callsign}'")]
safe_callsign = callsign.replace("'", "''")
entities = [ent for ent in self.table_client.query_entities(f"PartitionKey eq '{safe_callsign}' and RowKey eq '{safe_callsign}'")]
if len(entities) > 0:
logging.info(f"Entity already exists for {callsign}")
logging.info(f"Entity already exists for {safe_callsign}")
return entities[0] if len(entities) > 0 else None

def upsert_entity(self, callsign, messageId):
Expand All @@ -28,4 +31,41 @@ def upsert_entity(self, callsign, messageId):
u'RowKey': callsign,
u'MessageId': messageId
}
self.table_client.upsert_entity(mode=UpdateMode.REPLACE, entity=entity)
self.table_client.upsert_entity(mode=UpdateMode.REPLACE, entity=entity)

class HamAlertSqliteTable(BaseAlertTable):
def __init__(self):
db_path = os.getenv('SQLITE_DB_PATH', 'ham_alerts.db')
self.conn = sqlite3.connect(db_path)
self._create_table()

def _create_table(self):
cursor = self.conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS ham_alerts (
callsign TEXT PRIMARY KEY,
message_id TEXT
)
''')
self.conn.commit()

def query_for_entity(self, callsign):
cursor = self.conn.cursor()
cursor.execute('SELECT callsign, message_id FROM ham_alerts WHERE callsign = ?', (callsign,))
result = cursor.fetchone()
if result:
logging.info(f"Entity already exists for {callsign}")
return {'PartitionKey': result[0], 'RowKey': result[0], 'MessageId': result[1]}
return None

def upsert_entity(self, callsign, messageId):
cursor = self.conn.cursor()
cursor.execute('INSERT OR REPLACE INTO ham_alerts (callsign, message_id) VALUES (?, ?)',
(callsign, messageId))
self.conn.commit()

def create_table_client():
storage_type = os.getenv('STORAGE_TYPE', 'sqlite').lower()
if storage_type == 'azure':
return HamAlertAzureTable()
return HamAlertSqliteTable()
Loading