Last active
November 20, 2019 14:31
-
-
Save benoitguigal/b3855051d5d9f0bf725b2f9759180ef7 to your computer and use it in GitHub Desktop.
Exemple de requête GraphQL sur l'API Trackdéchets en Python
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 os | |
import json | |
import requests | |
# url de l'API de recette | |
TD_API_URL = "https://api.trackdechets.fr/" | |
# token passé en variable d'environnement | |
# recette.trackdechets.fr > Mon Compte > Intégration API > Générer une clé | |
TOKEN = os.environ["TOKEN"] | |
# requête GraphQL sous forme de string | |
GET_ME = """ | |
{ | |
me { | |
name | |
} | |
} | |
""" | |
# Ajout du token en Authorization header | |
headers = { | |
"Authorization": "Bearer {token}".format(token=TOKEN), | |
"Content-Type": "application/json" | |
} | |
payload = { | |
"query": GET_ME | |
} | |
# Post request à la racine de l'url | |
response = requests.post(TD_API_URL, data=json.dumps(payload), headers=headers) | |
# Récupération des infos | |
me = response.json()["data"]["me"] | |
print("Hello my name is {name}".format(name=me["name"])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment