コンテンツにスキップ

コードスニペット

これらの例は、Client Credentialsフローを使ってPythonからRealityConnect APIを呼び出す方法を示しています。各スニペットは自己完結型です。.pyファイルにコピーし、認証情報とリソースIDを設定してから python your_script.py で実行してください。


以下の各スニペットは、このブロックから始まります。Client Credentialsで認証し、リージョンのAPI URLを解決し、薄いHTTPヘルパーを定義します。

import base64
import json
import math
import 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()