(20250625) - Implemented the ecommerce shopify auth integration.

with multiple files added new API for get token details
This commit is contained in:
yatmesh
2025-06-25 14:06:02 +05:30
parent 388b9bdd1b
commit c022844824
14 changed files with 1029 additions and 7 deletions
+58
View File
@@ -408,6 +408,64 @@ class CoreAuthTokenController(CoreBaseModel):
# Done here:
return success
# CREATED BY OMKAR -------------------------------------------------------------------------------------------------
# 25 - 06 - 2025
# SET TOKEN WITH RETURN TOKEN --------------------------------------------------------------------------------------
async def set_token_direct_with_return_id(
self,
sql_conn: AsyncMySQL,
mongo_data_conn: AsyncMongo,
auth_token: CoreAuthTokenModel,
token_notes: dict,
display_name: str = None,
display_picture: str = None,
session_token: str = None
) -> (bool, str) :
"""
Some authorizations don't need two steps, but our core system works on the 2-step approach that was developed to
work with Google's GMail OAuth2.0 mechanism.
:param sql_conn: The database connection (MariaDB) to use to perform the action.
:param mongo_data_conn: The database connection (MongoDB) to use to perform the action.
:param auth_token: The actual auth/token data to be saved to the database.
:param token_notes: Any notes to feed into MariaDB with the token identifier.
:param display_name: The name of the user to user as their display name.
:param display_picture: The URL at which you will find a display picture of the user.
:param session_token: The session token of the user who requested this service.
:return: True if saved, False if failed.
"""
# Start by assuming failure:
success = False
# Get a token id (and receive its key):
token_key = await self.generate_token_key(
sql_conn = sql_conn,
mongo_data_conn = mongo_data_conn,
auth_token = auth_token,
token_notes = token_notes,
display_name = display_name,
display_picture = display_picture,
session_token = session_token
)
# Immediately save the details against that token id:
success = await self.set_token(
sql_conn = sql_conn,
mongo_data_conn = mongo_data_conn,
token_key = token_key,
auth_token = auth_token,
token_notes = token_notes,
display_name = display_name,
display_picture = display_picture,
session_token = session_token
)
# Done here:
return success, token_key
async def modify_status_by_token_key(
self,
sql_conn: AsyncMySQL,
+206
View File
@@ -0,0 +1,206 @@
"""
AUTHOR:
Omkar Khandare
DATE:
Tuesday, 24th Jun., 2025.
OBJECTIVE:
To handle ecommerce authentication.
REFERENCES:
N/A
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# My async utils:
from utils_v2.string import json
from utils_v2.date_time import date_time
from utils_v2.database.async_mysql_v2 import AsyncMySQL
from utils_v2.database.async_mongo_v2 import AsyncMongo
from utils_v2.cache.async_redis_cache_v2 import AsyncRedisCache
# Controllers:
from controllers_v2.core.software import CoreSoftwareController
# To make very controlled API calls:
from utils_v2.rest.controllers.async_base import AsyncREST
from utils_v2.rest.models.api_call import ApiResponse
# Models:
from models.software.ecommerce.auth import (
ShopifyAuth,
ShopifyAuthResponse
)
from models.core.user import CoreUserInfoModel
# To make HTTP requests:
import httpx
# to work with MongoDB:
from bson.objectid import ObjectId
# To make abstract classes:
from abc import ABC, abstractmethod
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** CLASSES ***
# ***** ****
# *****************************************************************************************************************
class EcommerceController(CoreSoftwareController, ABC):
# ┏┓┓ ┓┏
# ┃ ┃┏┓┏┏ ┃┃┏┓┏┓┏
# ┗┛┗┗┻┛┛ ┗┛┗┻┛ ┛
SERVICE_TYPE = "ecommerce"
# ┏┓
# ┃ ┏┓┏┓┏╋┏┓┓┏┏╋┏┓┏┓
# ┗┛┗┛┛┗┛┗┛ ┗┻┗┗┗┛┛
def __init__(
self,
cache: AsyncRedisCache = None,
http_client: httpx.AsyncClient = None,
alert_url: str = None,
base_filter: dict = None,
debug: bool = True,
debug_prefix: str = "Shopify (C) | ",
debug_only_errors: bool = True
):
"""
This is the foundational controller for generate an authentication key for ecommerce integration,
To Validate user from ecommerce webhooks
:param cache: The object to use for caching results from database calls.
:param http_client: The HTTP client to use to make REST-ful API calls.
:param base_filter: The basic filter that will be applied to all fetching/updating queries. WARNING: THE BASE
FILTER WILL ALWAYS BE APPLIED AUTOMATICALLY. SET THIS UP WISELY.
:param debug: Whether, or not, you would like to print debugging messages:
:param debug_prefix: The prefix to print with the debugging messages.
:param debug_only_errors: Whether you would like to print only error messages or all messages.
:return: None.
"""
# Prepare the combined base filter:
shopify_filter = {}
for k, v in (base_filter or {}).items(): shopify_filter[k] = v
shopify_filter["serviceType"] = self.SERVICE_TYPE
# Invoke the parent's constructor:
CoreSoftwareController.__init__(
self,
cache = cache,
alert_url = alert_url,
http_client = http_client,
base_filter = shopify_filter,
debug = debug,
debug_prefix = debug_prefix,
debug_only_errors = debug_only_errors
)
# Init a variable in a parent:
self._service_type = self.SERVICE_TYPE
# For controlled REST-ful calls:
self._rest = AsyncREST(
http_client = http_client,
debug = debug,
debug_prefix = debug_prefix,
debug_only_errors = debug_only_errors
)
# ┏┓ ┓
# ┣┫┓┏╋┣┓
# ┛┗┗┻┗┛┗
@abstractmethod
async def save_auth(
self,
sql_conn: AsyncMySQL,
mongo_data_conn: AsyncMongo,
auth: ShopifyAuth,
user: CoreUserInfoModel,
session_token: str
) -> ShopifyAuthResponse:
"""
Checks if a particular set of incoming credentials give access to a valid server and then stores the
credentials.
:param sql_conn: The database connection to use to perform this task.
:param mongo_data_conn: The database connection to use to perform this task.
:param auth: The set of credentials as received from the UI/API.
:return: A structured response to indicate what happened during authorization.
"""
pass
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
pass
@@ -0,0 +1,210 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Wednesday, 15th Jan., 2025.
OBJECTIVE:
To handle all WhatsApp-related behaviour for Nimbus IT's service from one place.
REFERENCES:
N/A
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
from abc import ABC
sys.path.append(".")
sys.path.append("..")
# My async utils:
from utils_v2.date_time import date_time
from utils_v2.database.async_mysql_v2 import AsyncMySQL
from utils_v2.database.async_mongo_v2 import AsyncMongo
from utils_v2.cache.async_redis_cache_v2 import AsyncRedisCache
from utils_v2.logging.context import AsyncLoggerContext
# Controllers:
from controllers_v2.software.ecommerce.base import EcommerceController
# Models:
from models.core.auth_token import CoreAuthTokenModel
from models.core.user import CoreUserInfoModel
from models.core.message import CoreMessageModel
from models.software.ecommerce.auth import (
ShopifyAuth,
ShopifyAuthResponse
)
# Chat clients:
from utils_v2.whatsapp.nimbus.controllers.async_nimbus_whatsapp import AsyncNimbusWhatsapp
# To work with datatypes:
from typing import List, Any
# To make HTTP requests:
import httpx
# For asynchronous activities:
import asyncio
# Common:
from shared import constants
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** CLASSES ***
# ***** ****
# *****************************************************************************************************************
class ShopifyAppController(EcommerceController):
# ┏┓┓ ┓┏
# ┃ ┃┏┓┏┏ ┃┃┏┓┏┓┏
# ┗┛┗┗┻┛┛ ┗┛┗┻┛ ┛
CLIENT_NAME = "shopify"
# ┏┓
# ┃ ┏┓┏┓┏╋┏┓┓┏┏╋┏┓┏┓
# ┗┛┗┛┛┗┛┗┛ ┗┻┗┗┗┛┛
def __init__(
self,
cache: AsyncRedisCache = None,
http_client: httpx.AsyncClient = None,
alert_url: str = None,
debug: bool = True,
debug_prefix: str = "Shopify (C) | ",
debug_only_errors: bool = True
):
"""
This is the controller for Nimbus IT's WhatsApp service.
:param cache: The object to use for caching results from database calls.
:param http_client: The HTTP client
:param debug: Whether, or not, you would like to print debugging messages:
:param debug_prefix: The prefix to print with the debugging messages.
:param debug_only_errors: Whether you would like to print only error messages or all messages.
:return: None.
"""
# Invoke the parent's constructor:
super().__init__(
cache = cache,
alert_url = alert_url,
http_client = http_client,
base_filter = {"client": self.CLIENT_NAME},
debug = debug,
debug_prefix = debug_prefix,
debug_only_errors = debug_only_errors
)
# Init a variable in a parent:
self._client = self.CLIENT_NAME
async def save_auth(
self,
sql_conn: AsyncMySQL,
mongo_data_conn: AsyncMongo,
auth: ShopifyAuth,
user: CoreUserInfoModel,
session_token: str
) -> ShopifyAuthResponse:
success, object_id = await self.set_token_direct_with_return_id(
sql_conn=sql_conn,
mongo_data_conn=mongo_data_conn,
auth_token=CoreAuthTokenModel(
serviceType=self.SERVICE_TYPE,
client=self.CLIENT_NAME,
authType="auth",
auth=auth.model_dump(),
user=user,
clientUserId={
"storeName": auth.storeName,
"storeUrl": auth.storeUrl
},
status="active",
syncFreq=60
),
token_notes={
"storeName": auth.storeName,
"storeUrl": auth.storeUrl
},
display_name=auth.storeName,
display_picture=None,
session_token=session_token
)
# Done here:
return ShopifyAuthResponse(
success=success,
token_id=str(object_id),
message="Shopify Account Added successfully." if success else "Shopify Account Added failed."
)
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
pass