(20250104) Breeze authorization cycle ready for testing on live server.
This commit is contained in:
@@ -46,6 +46,7 @@ from controllers_v2.finstitutions.trading.base import TradingController
|
||||
|
||||
# Models:
|
||||
from models.core.auth_token import CoreAuthTokenModel
|
||||
from utils_v2.trading.icici_breeze.models.auth_tokens import ICICIBreezeAuthTokens
|
||||
from models.api.finstitutions.trading.symbols.list import (
|
||||
TradingSymbolListRequestData,
|
||||
TradingSymbolListBrokerResponse,
|
||||
@@ -194,7 +195,83 @@ class ICICIBreezeTradingController(TradingController):
|
||||
:return: A structured response to capture the process of callback handling.
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
# Start by assuming failure:
|
||||
response = TradingOAuthCallbackResponse()
|
||||
icici_auth_token = None
|
||||
|
||||
# Check if the callback URL was configured properly:
|
||||
if not client_user_id:
|
||||
response.message = "Your callback URL hasn't been configured properly."
|
||||
return response
|
||||
|
||||
# Get the token from the database:
|
||||
condition = mongo_data_conn.dict_to_dot_notation({"auth": {"userId": client_user_id}})
|
||||
auth_token = await self.get_token_from_filter(
|
||||
mongo_data_conn = mongo_data_conn,
|
||||
filter_json = condition
|
||||
)
|
||||
|
||||
# If not such auth token exists:
|
||||
if not auth_token:
|
||||
response.message = (
|
||||
f"We couldn't find such an integration in our system. "
|
||||
"Please add this integration first and then try again."
|
||||
)
|
||||
return response
|
||||
|
||||
# Get the access token and user information from ICICI Breeze:
|
||||
breeze_session_token = inbound_data.get("apisession")
|
||||
try:
|
||||
breeze = BreezeConnect(api_key = auth_token.auth["apiKey"])
|
||||
breeze.generate_session(
|
||||
api_secret = auth_token.auth["apiSecret"],
|
||||
session_token = breeze_session_token
|
||||
)
|
||||
session_data = breeze.get_customer_details(api_session = breeze_session_token)
|
||||
session_data = session_data.get("Success")
|
||||
icici_auth_token = ICICIBreezeAuthTokens(**session_data)
|
||||
except Exception as exception:
|
||||
response.message = f"Client exception: {exception}"
|
||||
response.exception = exception
|
||||
return response
|
||||
|
||||
# Ensure that the client user id of the incoming callback and the one given in Zerodha's session data match:
|
||||
if icici_auth_token.userId != client_user_id:
|
||||
response.message = (
|
||||
f"We were expecting authorization for the account '{client_user_id}', "
|
||||
f"but ICICI says the authorization was granted for the account '{icici_auth_token.userId}'. "
|
||||
"This could be because of a misconfigured callback URL."
|
||||
)
|
||||
return response
|
||||
|
||||
# Prepare the inputs to save to the database:
|
||||
auth_url = await self.get_authorization_url(api_key = auth_token.auth["apiKey"])
|
||||
icici_auth_token.sessionToken = breeze_session_token
|
||||
auth_token.token = icici_auth_token.model_dump()
|
||||
|
||||
# Save the additional auth info to the database:
|
||||
success = await self.set_token(
|
||||
sql_conn = sql_conn,
|
||||
mongo_data_conn = mongo_data_conn,
|
||||
token_key = auth_token.key,
|
||||
auth_token = auth_token,
|
||||
token_notes = {
|
||||
"apiKey": auth_token.auth["apiKey"],
|
||||
"authUrl": auth_url
|
||||
},
|
||||
display_name = icici_auth_token.userId,
|
||||
display_picture = icici_auth_token.displayPictureUrl
|
||||
)
|
||||
|
||||
# If saving the token fails:
|
||||
if not success:
|
||||
response.message = "Something went wrong towards the end of the authorization cycle."
|
||||
return response
|
||||
|
||||
# Done here:
|
||||
response.success = True
|
||||
response.message = "Authorization cycle successfully completed."
|
||||
return response
|
||||
|
||||
# ┏┳┓ ┓• ┏┓ ┓ ┓
|
||||
# ┃ ┏┓┏┓┏┫┓┏┓┏┓ ┗┓┓┏┏┳┓┣┓┏┓┃┏
|
||||
|
||||
Reference in New Issue
Block a user