Python Example of Using API

The following is an example Python program that illustrates the use of API.

Python Example

You can copy the following code to a file, and run it where Python is installed on your machine:

python <filename.py>

You will need to fill in your specific environment information in this Python file, such as Metric Insights URl, and API credentials:

#!/usr/bin/python3
import requests
import json
# Set up the API endpoints and credentials
token_url = "https://your_metric_insights_url/api/get_token/"  # Replace with your Metric Insights token URL
data_url = "https://your_metric_insights_url/api/dataset_data"  # Replace with your Metric Insights dataset data URL
credentials = {
    "application_id": "your_application_id",  # Replace with your application ID
    "application_key": "your_application_key",  # Replace with your application key
    "user": "your_username"  # Replace with your username
}
# Function to get a session token
def get_token():
    headers = {"Accept": "application/json"}
    try:
        # Make a POST request to obtain a session token
        response = requests.post(token_url, headers=headers, json=credentials)
        response.raise_for_status()  # Raise an error for bad status codes
        data = response.json()  # Parse the response as JSON
        return data['token']  # Return the token from the response
    except requests.exceptions.HTTPError as err:
        print(f"HTTP error occurred: {err.response.status_code} {err.response.reason} for url: {err.response.url}")
        print(f"Response content: {err.response.text}")
        return None
    except Exception as err:
        print(f"An error occurred: {err}")
        return None
# Function to fetch dataset data from the API
def fetch_dataset_data(token, dataset_id):
    headers = {"Accept": "application/json", "Token": token}
    params = {"dataset": dataset_id}
    try:
        # Make a GET request to fetch dataset data
        response = requests.get(data_url, headers=headers, params=params)
        response.raise_for_status()  # Raise an error for bad status codes
        data = response.json()  # Parse the response as JSON
        print(f"Request URL: {response.url}")
        print(f"Response JSON: {json.dumps(data, indent=2)}")
        return data
    except requests.exceptions.HTTPError as err:
        print(f"HTTP error occurred: {err.response.status_code} {err.response.reason} for url: {err.response.url}")
        print(f"Response content: {err.response.text}")
        return None
    except Exception as err:
        print(f"An error occurred: {err}")
        return None
# Main execution
token = get_token()
if token:
    dataset_id = "your_dataset_id"  # Replace with your actual dataset ID
    dataset_data = fetch_dataset_data(token, dataset_id)
    if dataset_data and dataset_data["data"]:
        print(json.dumps(dataset_data, indent=2))
    else:
        print("No data found for the given dataset ID. Please check the dataset ID and ensure it contains data.")
else:
    print("Failed to retrieve token. Please check your credentials.")