1

I am trying to request a Graphql API through python. I have written the script which is trying to pull audit log from Github for each organisation.

This is the Python script I have written.

Query = """
query {
  organization(login: '{}') {
    auditLog(first: 100, '{}') {
      edges {
        node {
          ... on RepositoryAuditEntryData {
            repository {
              name
            }
          }
          ... on OrganizationAuditEntryData {
            organizationResourcePath
            organizationName
            organizationUrl
          }

          ... on TeamAuditEntryData {
            teamName
          }

          ... on TopicAuditEntryData {
            topicName
          }

          ... on OauthApplicationAuditEntryData {
            oauthApplicationName
          }
          
          ... on EnterpriseAuditEntryData {
            enterpriseResourcePath
            enterpriseUrl
            enterpriseSlug
          }

          ... on AuditEntry {
            actorResourcePath
            action
            actorIp
            actorLogin
            operationType
            createdAt
            actorLocation {
              countryCode
              country
              regionCode
              region
              city
            }
            #User 'Action' was performed on
            userLogin
            userResourcePath
            userUrl
          }
        }
        cursor
      }
      pageInfo {
        endCursor
        hasNextPage
        hasPreviousPage
      }
    }
  }
}
"""
  
l = []
l.append("CoreDevOpsTools") 
l.append("JIRA-Cloud")

res = []

for i in range(len(l)):   
    org = str(l[i])
    after = ''

    while True:
        result = requests.post('https://api.github.com/graphql',
                                json={'query': Query.format(org,after)},
                                headers=headers)

        json_data = json.loads(result.text)

        if 'errors' in json_data:
            print(json_data['errors'])
            break

        res_list = json_data['data']['organization']['auditLog']

        for items in res_list['edges']:
            res.append(items)

        if not res_list['pageInfo']['hasNextPage']:
            break

        after = 'after: "%s"' % res_list['edges'][-1]['cursor']
        time.sleep(1)

  File "../AuditLog.py", line 98, in <module>
    json={'query': Query.format(org,after)},
KeyError: '\n  organization(login'

This is the structure of query in Insomnia/Postman.

query {
  organization(login: "CoreDevOpsTools") {
    auditLog(first: 100, after: "XYZ") {
      edges {
        node {
          ... on RepositoryAuditEntryData {
            repository {
              name
            }
          }
          ... on OrganizationAuditEntryData {
            organizationResourcePath
            organizationName
            organizationUrl
          }

          ... on TeamAuditEntryData {
            teamName
          }

          ... on TopicAuditEntryData {
            topicName
          }

          ... on OauthApplicationAuditEntryData {
            oauthApplicationName
          }
          
          ... on EnterpriseAuditEntryData {
            enterpriseResourcePath
            enterpriseUrl
            enterpriseSlug
          }

          ... on AuditEntry {
            actorResourcePath
            action
            actorIp
            actorLogin
            operationType
            createdAt
            actorLocation {
              countryCode
              country
              regionCode
              region
              city
            }
            #User 'Action' was performed on
            userLogin
            userResourcePath
            userUrl
          }
        }
        cursor
      }
      pageInfo {
        endCursor
        hasNextPage
        hasPreviousPage
      }
    }
  }
}

This is error I am getting, I am not able to figure out what is wrong. I looked at other same type of questions here but that didn't work either.

1 Answer 1

2

The issue with your code is that the string you're trying to format itself has curly brackets in places where you don't want to replace things. e.g the first line "query {"

You can fix this by doubling the curly brackets. So "{" becomes "{{" etc. More on this here: stackoverflow.com

Sign up to request clarification or add additional context in comments.

2 Comments

``` query{{ organization(login: '{}') {{ }} }} ``` As far as I understood, this is how I have to write, right?
@KishanKumarGupta Yes! Alternatively if you think it doesn't look to great you could try this answer as well:link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.