Automatically change data in query API with python using requests

Viewed 105

I'm using an API to get some date (see code below) and everything works fine so far.

import requests
import pandas as pd

headers = {'content-type': 'application/json'}
query = """
query Dataset 
{
    elspotprices
    (
        where:
        {
            HourUTC: {_gt: \"2021-09-30\"}
            PriceArea: {_eq: \"DK2\"}
        }
        order_by: 
        {
            HourUTC: asc
        } 
        offset:0
    )
    {   
        SpotPriceDKK
    }
}
"""
request = requests.post('https://data-api.energidataservice.dk/v1/graphql', json={"query":query}, headers=headers)
data = request.json()['data']['elspotprices']
spotprices_df = pd.DataFrame(data)

However, I would like to define my variable date that represents any day and automatically change also the query string (an example of code is shown below which doesn't work) where the HourUTC is specified.

import requests
import pandas as pd

date = "2021-09-30\"

headers = {'content-type': 'application/json'}
query = """
query Dataset 
{
    elspotprices
    (
        where:
        {
            HourUTC: {_gt: \date}
            PriceArea: {_eq: \"DK2\"}
        }
        order_by: 
        {
            HourUTC: asc
        } 
        offset:0
    )
    {   
        SpotPriceDKK
    }
}
"""
request = requests.post('https://data-api.energidataservice.dk/v1/graphql', json={"query":query}, headers=headers)
data = request.json()['data']['elspotprices']
spotprices_df = pd.DataFrame(data)

Unfortunately, I didn't find the right way to do it yet but I'm pretty sure that there should be a way to do it.

0 Answers
Related