Python Example

A small python program to show how to fetch an access token and retrieve data

import requests
import json

username="XXXXXXX"
password="YYYYYYY"
compnum="01777777"

data = '{"query": "mutation signIn {signIn(useManagementflow: true, credentials: {userName: \\"%s\\", password: \\"%s\\"}) {accessToken}}"}' %(username,password)

signinHeader = {
'Content-Type': 'application/json',
'Accept': '*/*'
}

a = requests.post('https://azp-primary-api.azurewebsites.net/graphql/', headers=signinHeader, data = data)
if a.status_code != 200:
   print("Error on signin: ",a.status_code)
exit(1)

res = json.loads(a.text)
access_token = res["data"]["signIn"]["accessToken"]

reqheader = {
   'Accept': '*',
   'Content-Type': 'application/json',
   'Authorization': 'Bearer ' + access_token
}

postbody = '{"query": "query example{companies(where: {company_number: {eq: \\"%s\\"}}) {items {company_number company_name generated_rfa_rating}}}"}' %(compnum)

a = requests.post('https://azp-primary-api.azurewebsites.net/GraphQL/', headers=reqheader, data = postbody)
if a.status_code != 200:
   print("Error on query: ",a.status_code,a.text)
exit(1)

res = json.loads(a.text)
data = res["data"]
companies = data["companies"]
items = companies["items"]
for i in items:
   cname = i["company_name"]
   cnum = i["company_number"]
   rating = i["generated_rfa_rating"]
   print("cname",cname)
   print("cnum",cnum)
   print("rating",rating)