From d1c0ce24f4896cb73e56eb2bdbe607907617fe7e Mon Sep 17 00:00:00 2001 From: Khushal P Soonderji Date: Sat, 14 Jun 2025 11:38:42 +0530 Subject: [PATCH] (20250614) Ready for deployment with the UI. --- api_v2/blueprints/cred_and_data/blueprint.py | 21 ++++++++++++++++---- run.sh | 2 +- tests/mongo_test.py | 2 +- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/api_v2/blueprints/cred_and_data/blueprint.py b/api_v2/blueprints/cred_and_data/blueprint.py index 7496b69..13a6c6d 100644 --- a/api_v2/blueprints/cred_and_data/blueprint.py +++ b/api_v2/blueprints/cred_and_data/blueprint.py @@ -47,6 +47,7 @@ from quart import Blueprint, current_app, make_response # My utils: from utils_v2.string import json +from utils_v2.date_time import date_time from utils_v2.api.codes import StatusCodes, HttpCodes from utils_v2.api.response import ResponseModel from utils_v2.api.async_quart import ( @@ -157,6 +158,9 @@ async def set_data( :return: A standard response structure. """ + # Get the current date-time: + now = date_time.get_current_utc_date_time() + # If an invalid choice was made: if json_type not in JSON_TYPE_INFO.keys(): return ResponseModel( @@ -171,14 +175,18 @@ async def set_data( document = { "scriptId": inbound_headers["X-Script-Id"], "desc": description, - "content": inbound_data + "content": inbound_data, + "lastUpdateTs": now } # Make an attempt to set the credentials: - success = await current_app.mongo.replace_one( + success = await current_app.mongo.update_one( collection = JSON_TYPE_INFO[json_type]["collection"], filter = {"scriptId": inbound_headers["X-Script-Id"]}, - replacement = document, + update = { + "$set": document, + "$setOnInsert": {"firstSetTs": now}, + }, upsert = True, raise_exception = True ) @@ -227,6 +235,9 @@ async def get_data( :return: A standard response structure. """ + # Get the current date-time: + now = date_time.get_current_utc_date_time() + # If an invalid choice was made: if json_type not in JSON_TYPE_INFO.keys(): return ResponseModel( @@ -236,9 +247,11 @@ async def get_data( ) # Make an attempt to retrieve the credentials: - data_json = await current_app.mongo.find_one( + data_json = await current_app.mongo.find_one_and_update( collection = JSON_TYPE_INFO[json_type]["collection"], filter = {"scriptId": inbound_headers["X-Script-Id"]}, + update = {"$set": {"lastFetchTs": now}}, + return_updated = True, projection = {"_id": False, "scriptId": False}, raise_exception = True ) diff --git a/run.sh b/run.sh index 3688c40..9bb6dad 100644 --- a/run.sh +++ b/run.sh @@ -2,7 +2,7 @@ # Use this to run the microservice without any docker setup. source .venv/bin/activate -python3 "$(pwd)/api/main.py" --host "0.0.0.0" --port 5102 --script-id "yZqq2NVo" --debug & +python3 "$(pwd)/api_v2/main.py" --host "0.0.0.0" --port 5102 --script-id "yZqq2NVo" --debug & deactivate # All done: diff --git a/tests/mongo_test.py b/tests/mongo_test.py index ac97659..19eddb7 100644 --- a/tests/mongo_test.py +++ b/tests/mongo_test.py @@ -93,7 +93,7 @@ if __name__ == "__main__": # Create an instance of the database connector: my_db = AsyncMongo( - connection_string = constants.MONGO_DATA_CONNECTION_STRING, + connection_string = input("Connection String: "), database_name = constants.MONGO_DATA_DATABASE_NAME, max_connections = 10, debug = True