Upload new product to the PIM
In this example, we want to import the product 'Wallpaper'.
import requests
import json
def get_product_type_code():
url_get_product_type = 'https://pim-client.wizart.ai/api/v2.1/import/product-types'
headers = {
'Authorization': access_token
}
response = requests.get(url_get_product_type, headers=headers)
if response.status_code == 200:
get_product_types = requests.get(url_get_product_type, headers=headers)
product_types = json.loads(get_product_types.content.decode('utf8').replace("'", '"'))
for each_product_type in product_types['data']:
if each_product_type['attributes']['name'] == 'Wallpaper':
wallpaper_code = each_product_type['attributes']['code']
else:
print(f"Error: {response.status_code} - {response.text}")
return wallpaper_code
product_code = get_product_type_code()
def get_default_csv():
url_default_csv = f'https://pim-client.wizart.ai/api/v2.1/data-mappings/default/{product_code}'
headers = {
'Authorization': access_token
}
response = requests.get(url_default_csv, headers=headers)
if response.status_code == 200:
get_default_csv = requests.get(url_default_csv, headers=headers)
default_csv = get_default_csv.content.decode("utf-8")
csv_columns = default_csv.split(',')
else:
print(f"Error: {response.status_code} - {response.text}")
return csv_columns
empty_csv_file = get_default_csv()
def transform_custom_data_to_pim_csv():
#the process where you transform columns from your data storage into a format required for loading into a PIM system.
#to merge columns, use the obtained empty_csv_file with all the relevant columns required for loading into the PIM system.
return csv_data_for_import
def import_to_pim():
url = f'https://pim-client.wizart.ai/api/v2.1/product-type/{product_code}/import'
headers = {
'Authorization': access_token
}
files = {
'data': open('/path/to/file', 'rb'),
'archive': open('/path/to/file', 'rb')
}
response = requests.post(url, headers=headers, files=files)
import_to_pim()
The
get_product_type_code()
function retrieves the product type code for a specific product type (in this case, 'Wallpaper') from the PIM system. It sends a GET request to the/import/product-types
endpoint, using the providedaccess_token
for authorization. If the request is successful, the product types are obtained from the response, and the code for the 'Wallpaper' product type is extracted.The
get_default_csv()
function retrieves the default CSV mapping for the product type identified byproduct_code
. It sends a GET request to the/data-mappings/default/{product_code}
endpoint, again using theaccess_token
. If the request is successful, the default CSV content is obtained from the response and split into individual columns.The
transform_custom_data_to_pim_csv()
function is a placeholder that represents the process of transforming columns from your data storage into the format required for loading into the PIM system. It mentions merging the columns using theempty_csv_file
obtained from theget_default_csv()
function. The actual implementation of this transformation process is not provided in the code.The
import_to_pim()
function performs the actual import of data to the PIM system. It sends a POST request to the/product-type/{product_code}/import
endpoint, including theaccess_token
in the headers and files to be uploaded (data
andarchive
). The file paths should be updated accordingly.The
import_to_pim()
function is called at the end to initiate the data import process to the PIM system.