I don't know where I am going wrong. This endpoint is working fine on Thunder client but not in python even though the body that is passed is also same and the headers as well.
To be sure that the body is correct I printed the data in the terminal and pasted it into the body of the thunder client.
I also double checked the token that I am passing in SC_CSRF_TOKEN is correct or not.
To get the SC_CSRF_TOKEN login to https://www.smallcase.com/ and then go to the inspect element and check the requested headers of the routes
def get_sc_stock_data(stock):
print(f"Fetching stock data of {stock}")
url=f"https://api.smallcase.com/market/search?text={stock}"
responce=requests.get(url)
responce=responce.json()
res=responce['data']['searchResults']
if(len(res)<=0):
print("stock data not found in smallcase")
return
elif(len(res)==1):
return responce['data']['searchResults'][0]
else:
for i in res:
if(i['stock']['info']['ticker']==stock):
return i
def get_stock_close_prices(stocks):
print("Generating the close prices of basket's stocks")
res = {'count': 0, 'total': 0}
for stock in stocks:
stock=stock.upper()
try:
st = yf.Ticker(stock+'.NS')
price = st.history(period='1d').Close.iloc[0]
res[stock] = price
res['count'] += 1
res['total'] += price
except IndexError:
print(f"No historical data available for {stock}")
res[stock] = None
return res
def convert_to_sc_format(stocks, name, desc):
print("Generating smallcase basket")
prices = get_stock_close_prices(stocks)
total = prices['total']
constituents = []
tickers = []
for stock in stocks:
stock = stock.upper()
data = get_sc_stock_data(stock)
if not data:
continue
ticker = data['stock']['info']['ticker']
temp = {
"sid": data['sid'],
"shares": 1,
"locked": False,
"weight": prices[ticker] / total,
"sidInfo": {
"name": data['stock']['info']['name'],
"sector": data['stock']['info']['sector'],
"ticker": ticker
}
}
tickers.append(ticker)
constituents.append(temp)
res = {
"did": None,
"scid": None,
"source": 'CREATED',
"constituents": constituents,
"segments": [
{
"label": "New Segment",
"constituents": tickers
}
],
"compositionScheme": "WEIGHTS",
"info": {
"name": name,
"shortDescription": desc,
"tier": None
},
"stats": {
"initialValue": total
}
}
res=json.dumps(res,indent=2)
return res
def post_basket_to_sm(stocks,name,desc):
data=convert_to_sc_format(stocks,name,desc)
print(data)
url='https://api.smallcase.com/user/sc/drafts/save'
headers = {
'X-Csrf-Token': SC_CSRF_TOKEN,
}
res = requests.post(url, headers=headers, json=data)
return res.json()
stocks=['dlf','ongc']
data1=post_basket_to_sm(stocks,'t2','t2')
print(data1)
I tried to use the json.dumps() as without that it was showing:
{'success': False, 'errors': \['Invalid token'\], 'data': None}
I also tried to export this data into a new json file and then import it back to the function
Expected output
{
"success": true,
"errors": null,
"data": {
"did": ".......",
"scid": "......."
}
}
Output
{'data': 'Please mail us at [email protected]. Mistake is on our side!'}