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.