Exemples de code
Ces exemples montrent comment appeler la RealityConnect API depuis Python avec le flux Client Credentials. Chaque extrait est autonome : copiez-le dans un fichier .py, renseignez vos identifiants et les ID de ressources, puis exécutez-le avec python votre_script.py.
Configuration du script
Section intitulée « Configuration du script »Chaque extrait ci-dessous part de ce bloc. Il s’authentifie avec Client Credentials, résout l’URL API régionale et définit des helpers HTTP légers.
import base64import jsonimport mathimport os
import requests
CLIENT_ID = "your-client-id"CLIENT_SECRET = "your-client-secret"CLOUD_API_BASE = "https://cloud-api.prevu3d.com"
credentials = base64.b64encode(f"{CLIENT_ID}:{CLIENT_SECRET}".encode()).decode()token_response = requests.post( f"{CLOUD_API_BASE}/oauth/token", data={"grant_type": "client_credentials"}, headers={"Authorization": f"Basic {credentials}"},)token_response.raise_for_status()access_token = token_response.json()["access_token"]
api_info_response = requests.get( f"{CLOUD_API_BASE}/oauth/api-info", headers={"Authorization": f"Bearer {access_token}"},)api_info_response.raise_for_status()api_info = api_info_response.json()
API_URL = api_info["apiUrl"].rstrip("/")ORGANIZATION_ID = api_info["organization"]["id"]HEADERS = {"Authorization": f"Bearer {access_token}"}
def api_get(path: str, params: dict | None = None) -> dict: response = requests.get(f"{API_URL}{path}", headers=HEADERS, params=params) response.raise_for_status() return response.json()
def api_post(path: str, body: dict) -> dict | None: response = requests.post(f"{API_URL}{path}", json=body, headers=HEADERS) response.raise_for_status() if response.status_code == 204: return None return response.json()
def api_patch(path: str, body: dict) -> dict: response = requests.patch(f"{API_URL}{path}", json=body, headers=HEADERS) response.raise_for_status() return response.json()
def api_delete(path: str) -> None: response = requests.delete(f"{API_URL}{path}", headers=HEADERS) response.raise_for_status()