GraphQL with Python

Hi, Might be a simple solution and not sure if this the right place to ask but wanted to check here,
I’ve the below script where in am trying to get various values by passing policy ids from a csv file in a for loop. How to pass in each value from “fileone” to policyId: “” ?
In the below py file, this value (2807853) should be dynamic.

import json
import requests

with open('test.csv','r') as f1:
    fileone = f1.readlines()
    for l in fileone:
            url = 'https://api.newrelic.com/graphql'
            value = '''{
                    actor {
                        account(id: 2099802) {
                        alerts {
                            nrqlConditionsSearch(searchCriteria: {policyId: "2807853"}) {
                            nrqlConditions {
                                entityGuid
                                terms {
                                priority
                                }
                                policyId
                                name
                                id
                                # enabled
                            }
                            totalCount
                            }
                        }
                        }
                    }
                    }
                    '''
            print(value)
            payload = { 'query': value }

            headers = { 'Content-Type': 'application/json', 'API-Key': 'NRAK-XXXX' }

            r = requests.post(url, data=json.dumps(payload), headers=headers)
            print(r.json())

eg. test.csv contents
1398838
2807853
1470997
1453883
2171934

Hi, @suraj.koditala1: This is a general programming question that really has nothing to do with New Relic (“How can I read values from a CSV file and pass them as parameters to a GraphQL endpoint?”). You are more likely to get help in a general programming forum, such as Stack Overflow; this is definitely not something our support engineers can help with. Perhaps a member of the community will help you.

1 Like

You may find these articles helpful:

2 Likes

Thank you for the reference articles @philweber

Hi @suraj.koditala1

Glad that @philweber was able to help here!