(20250730) - ADDED - Google Places API for and paper trading update api with required fies and changes.

This commit is contained in:
yatmesh
2025-07-30 10:05:46 +05:30
parent 5f493f0ba9
commit 84efe75c06
10 changed files with 1054 additions and 13 deletions
@@ -0,0 +1,366 @@
"""
AUTHOR:
Omkar Khandare
DATE:
Monday, 7th July., 2025.
OBJECTIVE:
To find places details.
REFERENCES:
N/A
DOWNLOADS:
N/A
NOTES:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# For using Quart:
from quart import Blueprint, current_app, g, request
# My utils:
from utils_v2.string import json
from utils_v2.database.async_mongo_v2 import AsyncMongo
from utils_v2.logging.context import AsyncLoggerContext
from utils_v2.api.codes import StatusCodes, HttpCodes
from utils_v2.api.response import ResponseModel
from utils_v2.api.async_quart import (
make_ordered_json,
set_api_version,
read_input,
get_session_info,
log_request_to_mongo,
log_chain_to_mongo,
should_not_be_under_maintenance,
only_whitelisted_ips,
limit_rate,
validate_input,
handle_cancelled_request
)
# google-Places-related utils:
# from utils_v2.goog.controllers.gmail.gmail_message import GmailMessage
from utils_v2.goog.controllers.places.places_client import AsyncPlacesClient
from utils_v2.goog.models.auth_tokens import GoogleAuthTokens
# Common:
from shared import constants
# Data Models:
# from models.api.message.mail.send import MailSendRequestHeaders, MailSendRequestData
from models.api.message.mail.send import MailSendRequestHeaders, MailSendRequestData
from models.message.mail.send import MailSendOneResult
from models.core.user import CoreUserInfoModel
from models.core.auth_token import CoreAuthTokenModel
# To work with datatypes:
from typing import Literal
# For asynchronous activities:
import asyncio
# To work with date and time:
import datetime
# Helpers:
from api.helpers.user import token_check
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# Related to Quart:
google_places_bp = Blueprint("google_places", __name__)
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
@google_places_bp.record_once
def init(blueprint_setup_state):
# This gets called when the blueprint is registered.
# Consider this to be a one-time setup for the whole blueprint:
pass
# ---------------------------------------------------------------------------------------------------------------------
@google_places_bp.route("/<search_type>", methods = ["POST"])
@set_api_version(api_version = "1.0.0")
@read_input(sanitize_headers = False, sanitize_data = False)
@get_session_info(key = "X-Session-Token", session_coro = "get_session")
@log_request_to_mongo(
attr_name = "logs_mongo",
project = constants.PROJECT_NAME,
log_type = constants.MODULE_NAME,
operation = "mailSendApi",
log_input = 1,
log_output = True,
sensitive_keys = ["sessionToken", "X-Session-Token", "tokenKey", "tokenId"]
)
@log_chain_to_mongo(attr_name = "logs_mongo")
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
@validate_input(
header_validator = lambda x: MailSendRequestHeaders(**x).model_dump(),
data_validator = lambda x: MailSendRequestData(**x)
)
@handle_cancelled_request()
async def find_nearby(
inbound_headers: dict | MailSendRequestHeaders = None,
inbound_data: dict | MailSendRequestData = None,
inbound_files: dict = None,
**kwargs
):
"""
Use this endpoint to find place nearby location.
:param inbound_headers: auto-extracted by the decorators.
:param inbound_data: auto-extracted by the decorators.
:param inbound_files: auto-extracted by the decorators.
:param kwargs: Any number of extra inputs supplied by the decorators.
:return: A standard response structure.
"""
# current_app.google_places_client
# # ┏┓ ┓ ┏┓┓ ┓
# # ┣┫┓┏╋┣┓ ┃ ┣┓┏┓┏┃┏
# # ┛┗┗┻┗┛┗ ┗┛┛┗┗ ┗┛┗
#
# # If the session token is invalid/expired:
# if (
# kwargs.get("session_info") is None and
# inbound_headers["Remote-IP"] not in current_app.whitelisted_ips
# ): return constants.API_RESPONSE_UNAUTHORIZED
# ┏┓ ┓ • ┏┓┓ ┓
# ┃┃┓┏┏┏┓┏┓┏┓┏┣┓┓┏┓ ┃ ┣┓┏┓┏┃┏
# ┗┛┗┻┛┛┗┗ ┛ ┛┛┗┗┣┛ ┗┛┛┗┗ ┗┛┗
# ┛
# Get the token based on the key:
auth_token = await current_app.places_controller.get_token_from_key(
mongo_data_conn = current_app.data_mongo,
token_key = inbound_data.tokenKey,
must_be_active = True
)
if not auth_token: return constants.API_RESPONSE_NO_AUTH_TOKEN
# Get the user's info:
if inbound_headers["Remote-IP"] not in current_app.whitelisted_ips:
user_info = CoreUserInfoModel(**kwargs["session_info"])
else: user_info = auth_token.user
# We check if the token that was used to fetch the mail is owned by this user:
if not await token_check.is_authorized(
mongo_data_conn = current_app.data_mongo,
user_info = user_info,
token_ids = [auth_token.authTokenId]
): return constants.API_RESPONSE_UNAUTHORIZED
# ┏┓ ┓ ┳┳┓ •┓
# ┗┓┏┓┏┓┏┫ ┃┃┃┏┓┓┃
# ┗┛┗ ┛┗┗┻ ┛ ┗┗┻┗┗
# # Figure out the client connector:
# match auth_token.client:
# case "gmail":
# client_controller = current_app.places_controller
# client_connector = current_app.google_places_client
# case _:
# client_controller = None
# client_connector = None
# mail_message = None
# send_result.message = f"Invalid/unimplemented client '{auth_token.client}'"
client_controller = current_app.places_controller
client_connector = current_app.google_places_client
# If the controller and connector were matched:
if client_controller is not None and client_connector is not None:
send_result = await client_controller.nearby_radius_search(
sql_conn = current_app.sql_writer,
mongo_data_conn = current_app.data_mongo,
mail_client = client_connector,
mail_message = None,
auth_token = auth_token,
client_thread_id = inbound_data.clientThreadId,
user_info = user_info,
llm = current_app.llm,
session_token = inbound_headers["X-Session-Token"]
)
# ┳┓
# ┣┫┏┓┏┏┓┏┓┏┓┏┏┓
# ┛┗┗ ┛┣┛┗┛┛┗┛┗
# ┛
# Done here:
return ResponseModel(
status_code = StatusCodes.OK if send_result.success else StatusCodes.FAILED,
http_code = HttpCodes.SUCCESS if send_result.success else HttpCodes.INTERNAL_SERVER_ERROR,
message = send_result.message
)
# ---------------------------------------------------------------------------------------------------------------------
@google_places_bp.route("/textsearch", methods = ["POST"])
@set_api_version(api_version = "1.0.0")
@read_input(sanitize_headers = False, sanitize_data = False)
@get_session_info(key = "X-Session-Token", session_coro = "get_session")
@log_request_to_mongo(
attr_name = "logs_mongo",
project = constants.PROJECT_NAME,
log_type = constants.MODULE_NAME,
operation = "mailSendApi",
log_input = 1,
log_output = True,
sensitive_keys = ["sessionToken", "X-Session-Token", "tokenKey", "tokenId"]
)
@log_chain_to_mongo(attr_name = "logs_mongo")
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
@validate_input(
header_validator = lambda x: MailSendRequestHeaders(**x).model_dump(),
data_validator = lambda x: MailSendRequestData(**x)
)
@handle_cancelled_request()
async def text_search(
inbound_headers: dict = None,
inbound_data: dict = None,
inbound_files: dict = None,
**kwargs
):
"""
Use this endpoint to find place nearby location.
:param inbound_headers: auto-extracted by the decorators.
:param inbound_data: auto-extracted by the decorators.
:param inbound_files: auto-extracted by the decorators.
:param kwargs: Any number of extra inputs supplied by the decorators.
:return: A standard response structure.
"""
# current_app.google_places_client
# # ┏┓ ┓ ┏┓┓ ┓
# # ┣┫┓┏╋┣┓ ┃ ┣┓┏┓┏┃┏
# # ┛┗┗┻┗┛┗ ┗┛┛┗┗ ┗┛┗
#
# # If the session token is invalid/expired:
# if (
# kwargs.get("session_info") is None and
# inbound_headers["Remote-IP"] not in current_app.whitelisted_ips
# ): return constants.API_RESPONSE_UNAUTHORIZED
# ┏┓ ┓ • ┏┓┓ ┓
# ┃┃┓┏┏┏┓┏┓┏┓┏┣┓┓┏┓ ┃ ┣┓┏┓┏┃┏
# ┗┛┗┻┛┛┗┗ ┛ ┛┛┗┗┣┛ ┗┛┛┗┗ ┗┛┗
# ┛
# Get the token based on the key:
auth_token = await current_app.places_controller.get_token_from_key(
mongo_data_conn = current_app.data_mongo,
token_key = inbound_data["tokenKey"],
must_be_active = True
)
print("AUTH:", auth_token)
if not auth_token: return constants.API_RESPONSE_NO_AUTH_TOKEN
# Get the user's info:
if inbound_headers["Remote-IP"] not in current_app.whitelisted_ips:
user_info = CoreUserInfoModel(**kwargs["session_info"])
else: user_info = auth_token.user
# We check if the token that was used to fetch the mail is owned by this user:
if not await token_check.is_authorized(
mongo_data_conn = current_app.data_mongo,
user_info = user_info,
token_ids = [auth_token.authTokenId]
): return constants.API_RESPONSE_UNAUTHORIZED
# ┏┓ ┓ ┳┳┓ •┓
# ┗┓┏┓┏┓┏┫ ┃┃┃┏┓┓┃
# ┗┛┗ ┛┗┗┻ ┛ ┗┗┻┗┗
client_controller = current_app.places_controller
client_connector = current_app.google_places_client
# If the controller and connector were matched:
if client_controller is not None and client_connector is not None:
send_result = await client_controller.text_query_search(
tokens=auth_token,
text_query=inbound_data["textQuery"],
max_count=inbound_data["maxCount"]
)
# ┳┓
# ┣┫┏┓┏┏┓┏┓┏┓┏┏┓
# ┛┗┗ ┛┣┛┗┛┛┗┛┗
# ┛
# Done here:
return ResponseModel(
status_code = StatusCodes.OK if send_result.success else StatusCodes.FAILED,
http_code = HttpCodes.SUCCESS if send_result.success else HttpCodes.INTERNAL_SERVER_ERROR,
message = send_result.message
)
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
pass
+2 -6
View File
@@ -2,11 +2,11 @@
AUTHOR:
Khushal P Soonderji
Omkar Khandare
DATE:
Thursday, 16th Jan., 2025.
Friday, 4th July., 2025.
OBJECTIVE:
@@ -20,10 +20,6 @@
N/A
NOTES:
N/A
"""
+2 -2
View File
@@ -2,11 +2,11 @@
AUTHOR:
Khushal P Soonderji
Omkar Khandare
DATE:
Thursday, 16th jan., 2025.
Friday, 4th July., 2025.
OBJECTIVE: