just need advice, correction or even direction where should I go further with the project.
I am building a script which will make for loop API calls for creating products. I am pulling data for API call from edited csv which looks something like this:
title, product_id
"Name 1", 12345
"Name 2", 98765
"Name 3", 34566
"Nime 4", 78930
"Name 5", 99850
So, what I have created so far is for loop which makes API calls and at the end it should save responses and create new csv file with product_ids and responses for every one of them. It is very important for me to save it so i can investigate what happened afterwards.
The script I have now is this one:
from urllib import response
import requests
import json
import pandas as pd
import csv
from edit_csv_script import edited_csv
api_token = api_token
api_base_url = "https://api.mystore.net"
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"X-Recharge-Access-Token": api_token
}
df = pd.read_csv('test.csv')
for i in range(len(df)):
endpoint = f"{api_base_url}/products"
data = {
"product_id": df['product_id'][i],
"title": df['title'][i],
"discount_type": "percentage",
"discount_amount": 5,
"interval": "week",
}
response = requests.post(endpoint, json.dumps(data), headers=headers)
for i in range(len(df)):
response_api = response.json()
bad_responses = response.status_code !=200 or response.status_code !=201
if bad_responses:
df_bad_responses = pd.DataFrame({'product_id': df["product_id"][i],'response': response_api, 'status_code': response.status_code, 'message': response.text})
df_bad_responses.to_csv('bulk_process_bad_responses.csv')
df_ok_responses = pd.DataFrame({'product_id': df["product_id"][i], 'response': response_api, 'status_code': response.status_code})
df_ok_responses.to_csv('bulk_process_ok_responses.csv')
So the error now I am getting is:
Traceback (most recent call last):
File "Desktop/testing_scripts/bulk_products_create.py", line 36, in <module>
response = requests.post(endpoint, json.dumps(data), headers=headers)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type int64 is not JSON serializable
Maybe I should not use Pandas library for this one, not sure what do you think. Also, would like to underline that I have checked this discussion:Python - TypeError: Object of type 'int64' is not JSON serializable
but not one of the answers helped me.
Thank in advance!