3c4cac1019
git-subtree-dir: utils_v2 git-subtree-split: af73d53e43729f79a736602775d61ef5b1b0d9cf
249 lines
8.1 KiB
Python
249 lines
8.1 KiB
Python
"""
|
|
|
|
AUTHOR:
|
|
|
|
Khushal P Soonderji
|
|
|
|
DATE:
|
|
|
|
Saturday, 21st Dec., 2024.
|
|
|
|
OBJECTIVE:
|
|
|
|
To provide a data model for describing the tokens to be used for Google's APIs.
|
|
|
|
REFERENCES:
|
|
|
|
N/A
|
|
|
|
DOWNLOADS:
|
|
|
|
N/A
|
|
|
|
"""
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** IMPORT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# To make sibling directories accessible for imports:
|
|
import sys
|
|
sys.path.append(".")
|
|
sys.path.append("..")
|
|
|
|
# For making data behaviour_models:
|
|
from pydantic import BaseModel, Field, field_validator, model_validator, AwareDatetime
|
|
from typing import Optional, Literal, Union, Dict, List, Any
|
|
|
|
# Related to Google:
|
|
from google.auth.transport.requests import Request
|
|
from google.oauth2.credentials import Credentials
|
|
|
|
# My utils:
|
|
from utils_v2.string import json
|
|
from utils_v2.string import regex
|
|
from utils_v2.date_time import date_time
|
|
|
|
# To work with date and time:
|
|
import datetime
|
|
import dateparser
|
|
|
|
# To make API calls:
|
|
import httpx
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MACROS / ONE-TIME INIT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** VARIABLES ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** FUNCTIONS ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
class ZerodhaKiteAuthTokens(BaseModel):
|
|
|
|
userType: str | None = Field(
|
|
description = "the type of the user",
|
|
default = None,
|
|
alias = "user_type",
|
|
examples = ["individual/ind_with_nom"]
|
|
)
|
|
|
|
userId: str = Field(
|
|
description = "the id of the user as assigned by zerodha",
|
|
alias = "user_id",
|
|
examples = ["ABC123"]
|
|
)
|
|
|
|
email: str | None = Field(
|
|
description = "the email id of the user",
|
|
default = None,
|
|
alias = "email"
|
|
)
|
|
|
|
name: str = Field(
|
|
description = "the name of the user",
|
|
alias = "user_name"
|
|
)
|
|
|
|
displayName: str | None = Field(
|
|
description = "the short display name of the user",
|
|
default = None,
|
|
alias = "user_shortname"
|
|
)
|
|
|
|
displayPictureUrl: str | None = Field(
|
|
description = "the display picture of the user",
|
|
default = None,
|
|
alias = "avatar_url"
|
|
)
|
|
|
|
exchanges: List[str] = Field(
|
|
description = "the list of exchanges this user can trade on",
|
|
alias = "exchanges"
|
|
)
|
|
|
|
products: List[str] = Field(
|
|
description = "the list of products (offered by the broker) this user can avail",
|
|
alias = "products"
|
|
)
|
|
|
|
orderTypes: List[str] = Field(
|
|
description = "the list of order types this user can place",
|
|
alias = "order_types"
|
|
)
|
|
|
|
accessToken: str = Field(
|
|
description = "the main access token to be used in actual requests",
|
|
alias = "access_token"
|
|
)
|
|
|
|
refreshToken: str | None = Field(
|
|
description = "token to be used to refresh the access token; may not be provided",
|
|
default = None,
|
|
alias = "refresh_token"
|
|
)
|
|
|
|
publicToken: str | None = Field(
|
|
description = "undocumented on their official documentation",
|
|
default = None,
|
|
alias = "public_token"
|
|
)
|
|
|
|
loginTs: AwareDatetime = Field(
|
|
description = "the time (utc) at which this user logged in",
|
|
alias = "login_time"
|
|
)
|
|
|
|
# ┏┓ ┏•
|
|
# ┃ ┏┓┏┓╋┓┏┓
|
|
# ┗┛┗┛┛┗┛┗┗┫
|
|
# ┛
|
|
|
|
class Config:
|
|
extra = "ignore"
|
|
populate_by_name = True
|
|
|
|
# ┓┏ ┓• ┓ •
|
|
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
|
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
|
|
|
@field_validator("loginTs", mode = "before")
|
|
def parse_dates(cls, value):
|
|
if not isinstance(value, datetime.datetime):
|
|
parsed = date_time.parse_date_time(
|
|
value,
|
|
date_formats = ["%Y-%m-%d %H:%M:%S"],
|
|
timezone = date_time.TIMEZONE_IST
|
|
)
|
|
value = parsed if isinstance(parsed, datetime.datetime) else dateparser.parse(value)
|
|
if isinstance(value, datetime.datetime): value = date_time.to_timezone(value, date_time.TIMEZONE_UTC)
|
|
return value
|
|
|
|
# ┏┓ •
|
|
# ┃┃┏┓┏┓┏┓┏┓┏┓╋┓┏┓┏
|
|
# ┣┛┛ ┗┛┣┛┗ ┛ ┗┗┗ ┛
|
|
# ┛
|
|
|
|
pass
|
|
|
|
# ┏┓ ┏┓
|
|
# ┃ ┓┏┏╋┏┓┏┳┓ ┣ ┓┏┏┓┏┏
|
|
# ┗┛┗┻┛┗┗┛┛┗┗ ┻ ┗┻┛┗┗┛
|
|
|
|
pass
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MAIN PROGRAM ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sample_token = {
|
|
"user_type": "individual/ind_with_nom",
|
|
"email": "khushalpradipsoonderji@gmail.com",
|
|
"user_name": "Khushal Pradip Soonderji",
|
|
"user_shortname": "Khushal",
|
|
"broker": "ZERODHA",
|
|
"exchanges": [
|
|
"NSE",
|
|
"BSE",
|
|
"NFO",
|
|
"MF"
|
|
],
|
|
"products": [
|
|
"CNC",
|
|
"NRML",
|
|
"MIS",
|
|
"BO",
|
|
"CO"
|
|
],
|
|
"order_types": [
|
|
"MARKET",
|
|
"LIMIT",
|
|
"SL",
|
|
"SL-M"
|
|
],
|
|
"avatar_url": None,
|
|
"user_id": "ABC123",
|
|
"api_key": "same-as-api-key-input-to-this-func",
|
|
"access_token": "use-this-for-subsequent-activities-like-data-feeds",
|
|
"public_token": "???",
|
|
"refresh_token": "",
|
|
"enctoken": "???",
|
|
"login_time": "2024-12-20 13:23:20",
|
|
"meta": {
|
|
"demat_consent": "consent"
|
|
}
|
|
}
|
|
|
|
model = ZerodhaKiteAuthTokens(**sample_token)
|
|
print(json.to_string(model.model_dump(), default = str))
|