Skip to content

Instantly share code, notes, and snippets.

@joshschmelzle
Created October 7, 2019 00:17
Show Gist options
  • Save joshschmelzle/839b43be8d8c83a0a7fc1923219f35b4 to your computer and use it in GitHub Desktop.
Save joshschmelzle/839b43be8d8c83a0a7fc1923219f35b4 to your computer and use it in GitHub Desktop.
Example GraphQL client query using Python and Requests
import requests
url = "https://GRAPHQL-ENDPOINT/graphql"
headers = {"api-token": "TOKEN HERE"}
def run_query(url, query, headers) -> dict:
""" Run query and return result. """
request = requests.post(url=url, json={"query": query}, headers=headers)
if request.status_code == 200:
return request.json()
else:
raise Exception(
f"Query failed and returned code {request.status_code}. {query}"
)
query = """
{
deviceList (search: "00:00:00:33:33:33") {
devices {
ipAddress
macAddr
lastUpdated
locationNames
accessPointHistory {
apMacAddr
lastSeen
}
}
}
}
"""
result = run_query(url, query, headers)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment