[Python] Pulling hourly traffic data from Akamai using API

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to pull a week's worth hourly data from Akamai using API. However, the normalize json isn't really working as a result df is empty plus I am afraid that it is not getting the hourly timestamp column. Below is the code I am using. Would be great if someone can help.

    import requests
    import json
    import pandas as pd
    from akamai.edgegrid import EdgeGridAuth

    # Akamai API credentials
    client_token = "axxxxxxxxxxxxxxxxxxxxxxx"
    client_secret = "xxxxxxxxxxxxxxxxxxxxxxx"
    access_token = "xxxxxxxxxxxxxxxxxxxxxx"
    base_url="https://akab-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.net"

    # Initialize session with EdgeGrid authentication
    session = requests.Session()
    session.auth = EdgeGridAuth(client_token=client_token, client_secret=client_secret,


    access_token=access_token)

    # Define the API endpoint and parameters
    endpoint = '/reporting-api/v2/reports/delivery/traffic/current/data'
    url = f'{base_url}{endpoint}'

    # Define the request payload
    payload = {
    "dimensions": ["time1hour"],
    "metrics": ["edgeHitsSum", "edgeBytesSum"],
    "timeRange": "LAST_1_WEEK",
    "sortBys": [{"name": "time1hour", "sortOrder": "ASCENDING"}]
    }

    # Send the POST request
    response = session.post(url, json=payload)

    # Check for successful response
    if response.status_code == 200:
    data = response.json()

    # Convert the data to a DataFrame
    df = pd.json_normalize(data['data'])

    # Save the DataFrame to a CSV file
    df.to_csv('akamai_data.csv', index=False)

    print("Data has been saved to akamai_data.csv")
    else:
    print(f'Error: {response.status_code}')
    print(response.text)

    Continue reading...

Compartilhe esta Página