Skip to content

Instantly share code, notes, and snippets.

@benoitguigal
Last active November 20, 2019 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benoitguigal/b3855051d5d9f0bf725b2f9759180ef7 to your computer and use it in GitHub Desktop.
Save benoitguigal/b3855051d5d9f0bf725b2f9759180ef7 to your computer and use it in GitHub Desktop.
Exemple de requête GraphQL sur l'API Trackdéchets en Python
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