Expand my Community achievements bar.

Export data with python from Magento BI

Avatar

Level 1

Is it possible to extract data from Magento BI with a Python script?

2 Replies

Avatar

Community Advisor

Hi @Mohamed_AmineGu 

 

It looks like Adobe Commerce has the API to export reporting data - https://developer.adobe.com/commerce/services/reporting/export-api/ You can invoke these API in your scripts.

 

Thanks

Narendra

Avatar

Level 1

Hi @narendragandhi, thank you for your reply. I have followed the export API documentation but am still not able to retrieve the contents of a tables. this is how I am doing it: 

def get_request(endpoint):

proxies = {
'https': 'http://localhost:3128',
'http': 'http://localhost:3128',
}
params = {
"format": '["json", "zip", "csv"]', # specify the output format as JSON or CSV
#"filter": "price > 100", # specify a filter condition for the data
#"sort": "created_at DESC" # specify a sorting option for the data
}
url = f"{base_url}{endpoint}"
headers = {"X-RJM-API-Key": api_key}
response = requests.get(url, headers=headers, params=params, proxies=proxies)
print(response)
return response.json()

# Function to perform a POST request
def post_request(endpoint):
proxies = {
'https': 'http://localhost:3128',
'http': 'http://localhost:3128',
}
params = {
"format": '["json", "zip", "csv"]', # specify the output format as JSON or CSV
#"filter": "price > 100", # specify a filter condition for the data
#"sort": "created_at DESC" # specify a sorting option for the data
}
url = f"{base_url}{endpoint}"
headers = {"X-RJM-API-Key": api_key}
response = requests.post(url, headers=headers, params=params,proxies=proxies)
print(response)
return response.json()

data_result = get_request(f"/export/631087/info")
print("data_result:", data_result)

# Download the data from the data URL
data = get_request("https://api.rjmetrics.com/0.1/export/631087")

or

data = get_request(f"/client/{client_id}/table/{table_id}/export")

 Sadly i get always this response: <Response [404]> and error:  

Traceback (most recent call last):
File "C:\Users\gum1ul\Desktop\Workspace\python_code\venv\Lib\site-packages\requests\models.py", line 971, in json
return complexjson.loads(self.text, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\gum1ul\AppData\Local\Programs\Python\Python312\Lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\gum1ul\AppData\Local\Programs\Python\Python312\Lib\json\decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 5 (char 4)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\gum1ul\Desktop\Workspace\python_code\magento\export_data_MAgentoBI.py", line 104, in <module>
data = get_request(f"/client/{client_id}/table/{table_id}/export")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\gum1ul\Desktop\Workspace\python_code\magento\export_data_MAgentoBI.py", line 32, in get_request
return response.json()
^^^^^^^^^^^^^^^
File "C:\Users\gum1ul\Desktop\Workspace\python_code\venv\Lib\site-packages\requests\models.py", line 975, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Extra data: line 1 column 5 (char 4) .

 

do you have an idea how to solve that?