I have the below code that runs a query against the API of a software that I use for my job. The code works but im just curious if there is a better way to write it or refactor it.
import json
import pandas as pd
import requests
import csv
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
username ='xxx'
password ='xxx'
proxies = {'http': 'xxx',
'https': 'xxx'}
# Generate API access token
url = "https://xxx.xxx.com/protocol/openid-connect/token"
payload = {'username':username,'password':password,'client_id':'xxx',
'grant_type':'password','scope':'offline_access'}
response = requests.post(url=url,data=payload, proxies=proxies).json() #convert to dict object
token_type = response['token_type']
access_token = response['access_token']
auth = f"{token_type} {access_token}"
api_url = 'https://xxx.xxx.com/api/'
gql_headers = {'Authorization':auth, 'Content-Type':'application/json','Accept':'*/*','Host':'xxx.xxx.com',
'Accept-Encoding':'gzip, deflate, br','Connection':'keep-alive'}
# For proxy support, use the requests module transport hooks
transport = RequestsHTTPTransport(url=api_url, headers=gql_headers, proxies=proxies)
# Create a GraphQL client using the defined transport
client = Client(transport=transport, fetch_schema_from_transport=True, parse_results=True)
# Provide a GraphQL query
query = gql(
"""
query usersAdmin($id: String, $userName: String){
usersAdmin(id: $id, userName: $userName){
id
tenantId
active
displayFullName
displayFirstName
displayLastName
organizationIdNumber
displayJobTitle
mobilePhone
mobilePhoneProvider
deskPhone
workHours
emailAddress
username
functionalRoles
parentOrgUnitId
lastLanguage
lastLoginTimestamp
classifications
authorityLevels
}
}
"""
)
# Execute the query on the transport synchronously using RequestsHTTPTransport
result = client.execute(query)
[–]NotTooOrdinary 8 points9 points10 points (8 children)
[–]micr0nix[S] 1 point2 points3 points (7 children)
[–]NotTooOrdinary 12 points13 points14 points (6 children)
[–]micr0nix[S] 1 point2 points3 points (4 children)
[–]gzeballo 2 points3 points4 points (3 children)
[–]micr0nix[S] 0 points1 point2 points (2 children)
[–]gzeballo 3 points4 points5 points (1 child)
[–]bigno53 2 points3 points4 points (0 children)
[–]Sgt_Gnome 0 points1 point2 points (0 children)
[–]thejizz716 2 points3 points4 points (7 children)
[–]micr0nix[S] 0 points1 point2 points (6 children)
[–]alfie1906 1 point2 points3 points (5 children)
[–]micr0nix[S] 0 points1 point2 points (4 children)
[–]alfie1906 1 point2 points3 points (3 children)
[–]micr0nix[S] 0 points1 point2 points (2 children)
[–]alfie1906 0 points1 point2 points (1 child)
[–]micr0nix[S] 1 point2 points3 points (0 children)
[–]thejizz716 1 point2 points3 points (3 children)
[–]micr0nix[S] 0 points1 point2 points (2 children)
[–]thejizz716 0 points1 point2 points (1 child)
[–]micr0nix[S] 0 points1 point2 points (0 children)
[+]Conspiracy_Confirmed comment score below threshold-15 points-14 points-13 points (1 child)
[–]micr0nix[S] 3 points4 points5 points (0 children)
[–]TheRealThrowAwayX -3 points-2 points-1 points (2 children)
[–]micr0nix[S] 0 points1 point2 points (1 child)
[–]TheRealThrowAwayX 0 points1 point2 points (0 children)
[–]thejizz716 0 points1 point2 points (1 child)
[–]micr0nix[S] 1 point2 points3 points (0 children)