From 09afc61099289d31e54f3e76eff83a71b37860f6 Mon Sep 17 00:00:00 2001 From: miztch Date: Sun, 8 Oct 2023 03:30:02 +0900 Subject: [PATCH 1/3] style: use black formatter --- .vscode/settings.json | 6 ++++++ api/app.py | 37 +++++++++++++++++++++---------------- api/chalicelib/database.py | 26 ++++++++++++-------------- 3 files changed, 39 insertions(+), 30 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a0e76ff --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "[python]": { + "editor.defaultFormatter": "ms-python.black-formatter", + "editor.formatOnSave": true + } +} \ No newline at end of file diff --git a/api/app.py b/api/app.py index 2f09b61..cba69ef 100644 --- a/api/app.py +++ b/api/app.py @@ -3,48 +3,53 @@ from chalice import Chalice from chalicelib import database -app = Chalice(app_name='dima') +app = Chalice(app_name="dima") -@app.route('/') +@app.route("/") def index(): - ''' + """ return fixed response. - ''' - return {'status': 200, 'message': 'Remember, bullets hurt.'} + """ + return {"status": 200, "message": "Remember, bullets hurt."} -@app.route('/matches') +@app.route("/matches") def matches(): - ''' + """ return upcoming matches list. - ''' + """ request = app.current_request - app.log.debug('Request: {}'.format(request.to_dict())) + app.log.debug("Request: {}".format(request.to_dict())) # default date for search: today - date = datetime.strftime(datetime.now(), '%Y-%m-%d') + date = datetime.strftime(datetime.now(), "%Y-%m-%d") if request.query_params: - param = request.query_params.get('date') + param = request.query_params.get("date") # validate date format of query parameter (YYYY-mm-dd) # if invalid, parameter date -> today. - pattern = '^(?!([02468][1235679]|[13579][01345789])00-02-29)(([0-9]{4}-(01|03|05|07|08|10|12)-(0[1-9]|[12][0-9]|3[01]))|([0-9]{4}-(04|06|09|11)-(0[1-9]|[12][0-9]|30))|([0-9]{4}-02-(0[1-9]|1[0-9]|2[0-8]))|([0-9]{2}([02468][048]|[13579][26])-02-29))$' + pattern = "^(?!([02468][1235679]|[13579][01345789])00-02-29)(([0-9]{4}-(01|03|05|07|08|10|12)-(0[1-9]|[12][0-9]|3[01]))|([0-9]{4}-(04|06|09|11)-(0[1-9]|[12][0-9]|30))|([0-9]{4}-02-(0[1-9]|1[0-9]|2[0-8]))|([0-9]{2}([02468][048]|[13579][26])-02-29))$" try: if re.fullmatch(pattern, param) is not None: app.log.debug( - 'Date format in query parameter is valid. input: {}'.format(param)) + "Date format in query parameter is valid. input: {}".format(param) + ) date = param else: app.log.debug( - 'Date format in query parameter is invalid or empty. input: {}'.format(param)) + "Date format in query parameter is invalid or empty. input: {}".format( + param + ) + ) except TypeError: app.log.debug( - 'query parameter with invalid format given. input: {}'.format(param)) + "query parameter with invalid format given. input: {}".format(param) + ) matches = database.query(date) - app.log.debug('Return {} items.'.format(len(matches))) + app.log.debug("Return {} items.".format(len(matches))) return matches diff --git a/api/chalicelib/database.py b/api/chalicelib/database.py index 3d974f5..cbf45b8 100644 --- a/api/chalicelib/database.py +++ b/api/chalicelib/database.py @@ -8,28 +8,26 @@ def _get_table(): - dynamodb = boto3.resource('dynamodb') - table = dynamodb.Table(os.environ['TABLE_NAME']) + dynamodb = boto3.resource("dynamodb") + table = dynamodb.Table(os.environ["TABLE_NAME"]) return table def query(date): - ''' + """ scan DynamoDB table by following condition. - 'startTime' begins with $date - ''' + """ table = _get_table() - logger.info('Query table. date: {}'.format(date)) - response = table.scan( - FilterExpression=Attr('startTime').begins_with(date) - ) + logger.info("Query table. date: {}".format(date)) + response = table.scan(FilterExpression=Attr("startTime").begins_with(date)) - if response['Items']: - for item in response['Items']: - item['id'] = int(item['id']) - item['bestOf'] = int(item['bestOf']) + if response["Items"]: + for item in response["Items"]: + item["id"] = int(item["id"]) + item["bestOf"] = int(item["bestOf"]) - logger.info('Found {} items.'.format(len(response['Items']))) - return response['Items'] + logger.info("Found {} items.".format(len(response["Items"]))) + return response["Items"] From a4ba79ee3afa86a9d54cda5302c3daa131ea3309 Mon Sep 17 00:00:00 2001 From: miztch Date: Sun, 8 Oct 2023 04:12:37 +0900 Subject: [PATCH 2/3] refactor: separate logic for date validation --- api/app.py | 28 +++++----------------------- api/chalicelib/date_format.py | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 23 deletions(-) create mode 100644 api/chalicelib/date_format.py diff --git a/api/app.py b/api/app.py index cba69ef..6ad0213 100644 --- a/api/app.py +++ b/api/app.py @@ -1,7 +1,5 @@ -import re -from datetime import datetime from chalice import Chalice -from chalicelib import database +from chalicelib import database, date_format app = Chalice(app_name="dima") @@ -23,31 +21,15 @@ def matches(): app.log.debug("Request: {}".format(request.to_dict())) # default date for search: today - date = datetime.strftime(datetime.now(), "%Y-%m-%d") + date = date_format.get_default() if request.query_params: param = request.query_params.get("date") # validate date format of query parameter (YYYY-mm-dd) - # if invalid, parameter date -> today. - pattern = "^(?!([02468][1235679]|[13579][01345789])00-02-29)(([0-9]{4}-(01|03|05|07|08|10|12)-(0[1-9]|[12][0-9]|3[01]))|([0-9]{4}-(04|06|09|11)-(0[1-9]|[12][0-9]|30))|([0-9]{4}-02-(0[1-9]|1[0-9]|2[0-8]))|([0-9]{2}([02468][048]|[13579][26])-02-29))$" - - try: - if re.fullmatch(pattern, param) is not None: - app.log.debug( - "Date format in query parameter is valid. input: {}".format(param) - ) - date = param - else: - app.log.debug( - "Date format in query parameter is invalid or empty. input: {}".format( - param - ) - ) - except TypeError: - app.log.debug( - "query parameter with invalid format given. input: {}".format(param) - ) + # if format is valid, overwrite date with parameter + if param and date_format.validate(param): + date = param matches = database.query(date) app.log.debug("Return {} items.".format(len(matches))) diff --git a/api/chalicelib/date_format.py b/api/chalicelib/date_format.py new file mode 100644 index 0000000..ca2142a --- /dev/null +++ b/api/chalicelib/date_format.py @@ -0,0 +1,27 @@ +import logging +import re +from datetime import datetime + +logger = logging.getLogger() +logger.setLevel(logging.INFO) + + +def validate(date): + """ + Validate date format (YYYY-mm-dd). + """ + pattern = "^(?!([02468][1235679]|[13579][01345789])00-02-29)(([0-9]{4}-(01|03|05|07|08|10|12)-(0[1-9]|[12][0-9]|3[01]))|([0-9]{4}-(04|06|09|11)-(0[1-9]|[12][0-9]|30))|([0-9]{4}-02-(0[1-9]|1[0-9]|2[0-8]))|([0-9]{2}([02468][048]|[13579][26])-02-29))$" + + if re.fullmatch(pattern, date) is not None: + logger.debug("Date format is valid. input: {}".format(date)) + return True + else: + logger.debug("Date format is invalid or empty. input: {}".format(date)) + return False + + +def get_default(): + """ + Get the default date for search: today. + """ + return datetime.strftime(datetime.now(), "%Y-%m-%d") From c0de522df71352b22ff1d520b4747e313cb9e3d7 Mon Sep 17 00:00:00 2001 From: miztch Date: Sun, 8 Oct 2023 04:21:37 +0900 Subject: [PATCH 3/3] refactor: add logging module --- api/app.py | 8 +++++--- api/chalicelib/database.py | 6 +++--- api/chalicelib/date_format.py | 6 +++--- api/chalicelib/log.py | 11 +++++++++++ 4 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 api/chalicelib/log.py diff --git a/api/app.py b/api/app.py index 6ad0213..b68b367 100644 --- a/api/app.py +++ b/api/app.py @@ -1,5 +1,7 @@ from chalice import Chalice -from chalicelib import database, date_format +from chalicelib import database, date_format, log + +logger = log.getLogger() app = Chalice(app_name="dima") @@ -18,7 +20,7 @@ def matches(): return upcoming matches list. """ request = app.current_request - app.log.debug("Request: {}".format(request.to_dict())) + logger.debug("Request: {}".format(request.to_dict())) # default date for search: today date = date_format.get_default() @@ -32,6 +34,6 @@ def matches(): date = param matches = database.query(date) - app.log.debug("Return {} items.".format(len(matches))) + logger.debug("Return {} items.".format(len(matches))) return matches diff --git a/api/chalicelib/database.py b/api/chalicelib/database.py index cbf45b8..1fb676b 100644 --- a/api/chalicelib/database.py +++ b/api/chalicelib/database.py @@ -1,10 +1,10 @@ -import logging import os + import boto3 from boto3.dynamodb.conditions import Key, Attr +from chalicelib import log -logger = logging.getLogger() -logger.setLevel(logging.INFO) +logger = log.getLogger() def _get_table(): diff --git a/api/chalicelib/date_format.py b/api/chalicelib/date_format.py index ca2142a..f1a64a0 100644 --- a/api/chalicelib/date_format.py +++ b/api/chalicelib/date_format.py @@ -1,9 +1,9 @@ -import logging import re from datetime import datetime -logger = logging.getLogger() -logger.setLevel(logging.INFO) +from chalicelib import log + +logger = log.getLogger() def validate(date): diff --git a/api/chalicelib/log.py b/api/chalicelib/log.py new file mode 100644 index 0000000..fde5e53 --- /dev/null +++ b/api/chalicelib/log.py @@ -0,0 +1,11 @@ +import logging + + +def getLogger(): + """ + init logger. + """ + logger = logging.getLogger() + logger.setLevel(logging.INFO) + + return logger