Step 1: Render filters and create dictionaries for filtering
# This code filters for product types and material types import requestsbase_api_url ='https://app.2050-materials.com/'get_filters_url =f'{base_api_url}developer/api/get_product_filters'headers ={'Authorization':f'Bearer YOUR_API_TOKEN','Content-Type':'application/json',}try: response = requests.get(get_filters_url, headers=headers) response.raise_for_status() filters = response.json()except requests.RequestException as e:raiseException(f"Failed call to get_products API: {e}")product_types ={}for i in filters['product_type']['filter_options']: product_types[i['name']]= i['id']material_types ={}for i in filters['material_types']['filter_options']: material_types[i['name']]= i['id']certificate_type ={}for i in filters['certificate_type']['filter_options']: certificate_type[i['name']]= i['id']
Limited data: /get_products_open_api
# This code gets building boards (product_type.id = 5), with an EPD (certificate_type.id=1) and groups then by company_name
import requestsurl = "https://app.2050-materials.com/developer/api/get_products_open_api?group_by=company_name&certificate_type=1&product_type=5"
#The above code could also replace the filters in this way:#certificate_type_selected = certificate_type['EPD']#product_type_selected = product_type['Building boards (Multiple functions)']#url = "https://app.2050-materials.com/developer/api/get_products_open_api?group_by=company_name&certificate_type=&product_type=5"
payload ={}headers ={'Authorization':'Bearer YOUR_API_TOKEN }response = requests.request("GET", url, headers=headers, data=payload)print(response.text)
Expanded data: /get_products
# This code gets building boards (product_type.id = 5), with an EPD (certificate_type.id=1) and groups then by company_name
# Additionally, this filters to products from Canada (country=Canada) only.import requestsurl = "https://app.2050-materials.com/developer/api/get_products?group_by=company_name&certificate_type=1&product_type=5&country=Canada"
payload ={}headers ={'Authorization':'Bearer YOUR_API_TOKEN }response = requests.request("GET", url, headers=headers, data=payload)print(response.text)
Generic Data: /get_generic_materials
# This code gets building boards from the generic data (product_type.id = 5)import requestsurl ="https://app.2050-materials.com/developer/api/get_generic_materials?product_types=5"payload ={}files={}headers ={'Authorization':'Bearer YOUR_API_TOKEN'}response = requests.request("GET", url, headers=headers, data=payload, files=files)print(response.text)
Retrieve Warming Potential and EC for a High-Rise Residential building /get_co2_warming_potential
Step 2: Get Filters and Create Dictionaries for Product Types, Material Types, and Certificate Types
codebase_api_url ='https://app.2050-materials.com/'get_filters_url =f'{base_api_url}developer/api/get_product_filters'# Update headers with the obtained bearer tokenheaders ={'Authorization':f'Bearer {bearer_token}','Content-Type':'application/json',}try:# Request to get product filters filters_response = requests.get(get_filters_url, headers=headers) filters_response.raise_for_status() filters = filters_response.json()# Parsing and storing filters in dictionaries product_types ={i['name']: i['id']for i in filters['product_type']['filter_options']} material_types ={i['name']: i['id']for i in filters['material_types']['filter_options']} certificate_type ={i['name']: i['id']for i in filters['certificate_type']['filter_options']}except requests.RequestException as e:raiseException(f"Failed to retrieve product filters: {e}")
Step 3: Use Filters to Call get_products API and Create DataFrame
codeimport pandas as pd# Define your filters herecertificate_type_selected = certificate_type['EPD']product_type_selected = product_type['Building boards (Multiple functions)']selected_country ='Canada'# Constructing the URL with query parametersproducts_url = f"{base_api_url}developer/api/get_products?group_by=company_name&certificate_type={selected_certificate_type}&product_type={selected_product_type}&country={selected_country}"
try:# Request to get products data products_response = requests.get(products_url, headers=headers) products_response.raise_for_status() products_data = products_response.json()# Creating a DataFrame from the response products_df = pd.DataFrame(products_data)except requests.RequestException as e:raiseException(f"Failed to retrieve products data: {e}")# Optional: Display the DataFrameprint(products_df.head())
Notes:
Error Handling: The code includes try-except blocks to handle potential request errors.
Bearer Token: Ensure to replace {{Developer Token}} with your actual developer token.
Filters: The code dynamically creates dictionaries for different filter types based on the response from the get_product_filters endpoint.
DataFrame Creation: The final step converts the product data into a pandas DataFrame for easy manipulation and analysis.