Created
October 7, 2019 00:17
-
-
Save joshschmelzle/839b43be8d8c83a0a7fc1923219f35b4 to your computer and use it in GitHub Desktop.
Example GraphQL client query using Python and Requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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