0

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!

Vlajko
  • 15
  • 5
  • Hi, welcome to SO. Your question doesn't specify a problem. Please see the guidelines https://stackoverflow.com/help/how-to-ask for good questions. For your problem. Try to run the code, see if you face a problem. If you do face a problem, come back on SO and ask people how to overcome the issue you face. – Stereo Jul 19 '22 at 11:52
  • @Stereo Thanks! Apologies for this beginner's mistake. Unfortunately, I cannot resolve is that it is very hard to call the API until I get permissions to test the script. The problem at this point is saving this responses - status_codes and error messages inside of another outcome csv. If that does not make it clear, will try to test it somehow here and if I get any mistake, I will get back here. Hah. Thanks once again! – Vlajko Jul 19 '22 at 12:20
  • @Stereo I have updated my question and code. Hope it helps. – Vlajko Jul 26 '22 at 16:12
  • Can you post an example JSON response that you would get with curl or postman? – Stereo Jul 27 '22 at 13:26

0 Answers0