(20241223) Zerodha auth now returns views.

This commit is contained in:
2024-12-23 16:27:52 +05:30
parent 3296fec639
commit 58e91fb15f
16 changed files with 112 additions and 294 deletions
@@ -57,7 +57,8 @@ from utils_v2.api.async_quart import (
only_whitelisted_ips,
limit_rate,
validate_input,
handle_cancelled_request
handle_cancelled_request,
handle_failed_request
)
# GMail-related utils:
@@ -112,6 +113,22 @@ def init(blueprint_setup_state):
# ---------------------------------------------------------------------------------------------------------------------
async def handle_auth_exception():
print("Hi! Cleaning up...")
return await render_template(
"/finstitutions/trading/oauth/oauth_failure_v2.html",
client = g.client_label,
failure_hint = (
f"Something went wrong (E). "
f"Please use log-id <b>'{g.log_id}'</b> to check with the support team."
)
)
# ---------------------------------------------------------------------------------------------------------------------
@trading_oauth_callback_bp.route("/callback/<trading_client>", methods = ["POST", "GET"])
@set_api_version(api_version = "1.0.0")
@read_input(sanitize_headers = False, sanitize_data = False)
@@ -127,6 +144,7 @@ def init(blueprint_setup_state):
@log_chain_to_mongo(attr_name = "logs_mongo")
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
@handle_cancelled_request()
@handle_failed_request(cleanup_coro = handle_auth_exception)
async def trading_oauth_callback(
trading_client: str = None,
inbound_headers: dict = None,
@@ -145,6 +163,13 @@ async def trading_oauth_callback(
:param kwargs: Any number of extra inputs supplied by the decorators.
:return: A standard response structure.
"""
# ┓┏ ┓┓ ┓┏ • ┓ ┓
# ┣┫┏┓┏┓┏┫┃┏┓ ┃┃┏┓┏┓┓┏┓┣┓┃┏┓┏
# ┛┗┗┻┛┗┗┻┗┗ ┗┛┗┻┛ ┗┗┻┗┛┗┗ ┛
# Store needed values in 'g':
g.log_id = kwargs.get("log_id")
g.client_label = "Zerodha (Kite)"
# Start by assuming failure:
success = None
@@ -165,17 +190,43 @@ async def trading_oauth_callback(
# ┛┗┗ ┛┣┛┗┛┛┗┛┗
# ┛
# # No valid client:
# if success is None: return ResponseModel(
# status_code = StatusCodes.FAILED,
# http_code = HttpCodes.BAD_REQUEST,
# message = f"Invalid/unimplemented client '{trading_client}'."
# )
#
# # When the client was valid:
# return ResponseModel(
# status_code = StatusCodes.OK if success else StatusCodes.FAILED,
# http_code = HttpCodes.SUCCESS if success else HttpCodes.INTERNAL_SERVER_ERROR,
# )
# No valid client:
if success is None: return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.BAD_REQUEST,
message = f"Invalid/unimplemented client '{trading_client}'."
if success is None: return await render_template(
"/finstitutions/trading/oauth/oauth_failure_v2.html",
client = g.client_label,
failure_hint = (
f"Invalid client '{g.client_label}' selected. "
f"Please use log-id '{g.log_id}' to check with the support team."
)
)
# When the client was valid:
return ResponseModel(
status_code = StatusCodes.OK if success else StatusCodes.FAILED,
http_code = HttpCodes.SUCCESS if success else HttpCodes.INTERNAL_SERVER_ERROR,
# Successful auth:
if success: return await render_template(
"/finstitutions/trading/oauth/oauth_success_v2.html",
client = g.client_label
)
# Failed auth:
if success is None: return await render_template(
"/finstitutions/trading/oauth/oauth_failure_v2.html",
client = g.client_label,
failure_hint = (
f"Something went wrong (NE). "
f"Please use log-id '{g.log_id}' to check with the support team."
)
)
+6 -6
View File
@@ -134,7 +134,7 @@ async def handle_gmail_callback() -> render_template:
# In case the user cancelled halfway through (on Google's screen):
if g.inbound_data.get("error") == "access_denied": return await render_template(
"/mail/oauth/oauth_cancelled_v2.html",
"/message/mail/oauth/oauth_cancelled_v2.html",
mail_client = g.mail_client.title()
)
@@ -167,7 +167,7 @@ async def handle_gmail_callback() -> render_template:
(not auth_token) or
auth_token.clientUserId["email"] != str(google_tokens.email)
): return await render_template(
"/mail/oauth/oauth_failure_v2.html",
"/message/mail/oauth/oauth_failure_v2.html",
mail_client = g.mail_client.title(),
failure_hint = (
f"We were expecting authorization from '{auth_token.clientUserId['email']}', "
@@ -224,13 +224,13 @@ async def handle_gmail_callback() -> render_template:
# Return an HTML response for success:
if tokens_saved: return await render_template(
"/mail/oauth/oauth_success_v2.html",
"/message/mail/oauth/oauth_success_v2.html",
mail_client = g.mail_client.title()
)
# Return an HTML response for failure:
else: return await render_template(
"/mail/oauth/oauth_failure_v2.html",
"/message/mail/oauth/oauth_failure_v2.html",
mail_client = g.mail_client.title(),
failure_hint = f"Unknown error. Please use log-id '{g.log_id}' to check with the support team."
)
@@ -292,7 +292,7 @@ async def mail_auth_callback(
# If something goes wrong:
except Exception as exception: return await render_template(
"/mail/oauth/oauth_failure_v2.html",
"/message/mail/oauth/oauth_failure_v2.html",
mail_client = mail_client.title(),
failure_hint = (
f"An internal server error occurred. "
@@ -305,7 +305,7 @@ async def mail_auth_callback(
# ┛┗┗┻┛┗┗┻┗┗ ┻┛┗┗┛┗┻┗┗┗┻ ┗┛┗┗┗ ┛┗┗
return await render_template(
"/mail/oauth/oauth_failure_v2.html",
"/message/mail/oauth/oauth_failure_v2.html",
mail_client = mail_client.title(),
failure_hint = (
f"Invalid client '{mail_client}' selected. "
+1 -1
View File
@@ -155,7 +155,7 @@ APP_VERSION = constants.APP_VERSION
# The Quart app:
app = Quart(__name__, template_folder = r"../views")
app = Quart(__name__, template_folder = r"../views_v2")
app = cors(app)
# Mail Blueprints: