Created
November 20, 2020 15:21
-
-
Save revolunet/7ffb46587b843a3c2ae227b5debe5e56 to your computer and use it in GitHub Desktop.
Python fetch dossiers demarches simplifiées
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
# | |
# Fetch les 100 premiers dossiers d'une démarche DS | |
# | |
# usage: DS_TOKEN=xyz python3 ds-fetch.py | |
# | |
import requests | |
import os | |
import json | |
DS_TOKEN = os.environ.get('DS_TOKEN') | |
API_URL = "https://www.demarches-simplifiees.fr/api/v2/graphql" | |
# requete GraphQL, a affiner sur https://www.demarches-simplifiees.fr/graphql | |
query = """ | |
query getDemarches { | |
demarche(number: 12345) { | |
dateCreation | |
title | |
dossiers( first:100) { | |
edges { | |
node { | |
state | |
avis { | |
id | |
instructeur { | |
} | |
expert { | |
} | |
reponse | |
} | |
dateTraitement | |
dateDerniereModification | |
usager { | |
} | |
messages { | |
id | |
createdAt | |
body | |
} | |
datePassageEnInstruction | |
datePassageEnConstruction | |
number | |
motivation | |
champs { | |
id | |
label | |
stringValue | |
} | |
} | |
} | |
} | |
} | |
} | |
""" | |
def run_query(query): | |
headers = {"Authorization": "Bearer " + DS_TOKEN} | |
request = requests.post(API_URL, json={'query': query}, headers=headers, verify=False) # warn: SSL verification disabled | |
if request.status_code == 200: | |
return request.json() | |
else: | |
raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query)) | |
if __name__=="__main__": | |
result = run_query(query) | |
print(json.dumps(result, indent=2)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment