Rest Endpoint - SDK - Is there any way we can invoke grapqhl rest endpoint via sdk

Related products: API and SDKs

Hello,

I want to leverage the refresh token functionality of  sdk so is there any way i can get my graphql query executed using the python sdk ? I am invoking below endpoint as of today.
https://{cognite_cluster}.cognitedata.com/api/v1/projects/{project_name}/userapis/spaces/{space}/datamodels/{model}/versions/{string_version}/graphql

If not available can a request be taken to provide that in python sdk!

@Carin Meems Can this be converted to a Product Idea?


While this is not officially supported in the Cognite Python SDK today, there is a method to perform post requests in the SDK that allows you to do exactly what you want. Here’s a quick example on how to make a GraphQL request today using the Python SDK.

client = CogniteClient()
project = client.config.project

external_id = "LER001-TI518"
now = datetime.utcnow()
end = timestamp_to_ms(now)
start = timestamp_to_ms(now - timedelta(hours=1))

query = {
"query": f"""
query {{
listShower {{
items {{
name
coldWaterTemperature {{
name
externalId
unit
dataPoints(start: {start}, end: {end}, limit: 100000) {{
timestamp
value
}}
}}
}}
}}
}}
"""
}

response = client.post(url=f"/api/v1/projects/{project}/userapis/spaces/showerMixer/datamodels/ShowerMixer/versions/1/graphql", json=query)

If everything is correct, you can inspect the response and get:

<Response [200]>

If the query was successful, you can get the query response as below:

response.json()
>> {'data': {'listShower': {'items': [{'name': 'Shower01',
>> 'coldWaterTemperature': {'name': '001-TI518',
>> 'externalId': 'LER001-TI518',
>> 'unit': 'degC',
>> 'dataPoints': [{'timestamp': '2023-10-10T07:01:47.387Z',
>> 'value': 27.46875},
>> {'timestamp': '2023-10-10T07:01:49.387Z', 'value': 27.65625},
>> {'timestamp': '2023-10-10T07:07:24.885Z', 'value': 27.65625},
>> {'timestamp': '2023-10-10T07:07:26.886Z', 'value': 27.65625},
>> {'timestamp': '2023-10-10T07:17:26.386Z', 'value': 27.65625},
>> {'timestamp': '2023-10-10T07:17:28.386Z', 'value': 27.9375},
>> {'timestamp': '2023-10-10T07:22:59.386Z', 'value': 27.9375},
>> {'timestamp': '2023-10-10T07:23:01.386Z', 'value': 27.9375},
>> {'timestamp': '2023-10-10T07:32:59.886Z', 'value': 27.9375},
>> {'timestamp': '2023-10-10T07:33:01.886Z', 'value': 28.0},
>> {'timestamp': '2023-10-10T07:38:36.385Z', 'value': 28.0},
>> {'timestamp': '2023-10-10T07:38:38.386Z', 'value': 28.0},
>> {'timestamp': '2023-10-10T07:48:36.886Z', 'value': 28.0},
>> {'timestamp': '2023-10-10T07:48:38.886Z', 'value': 28.125},
>> {'timestamp': '2023-10-10T07:54:15.886Z', 'value': 28.125},
>> {'timestamp': '2023-10-10T07:54:17.886Z', 'value': 28.125}]}}]}}}

 


NewGathering Interest