코드 스니펫
이 예제들은 Client Credentials 플로를 사용하여 Python에서 RealityConnect API를 호출하는 방법을 보여줍니다. 각 스니펫은 독립적입니다: .py 파일에 복사하고, 자격 증명과 리소스 ID를 설정한 다음, python your_script.py로 실행하세요.
스크립트 설정
섹션 제목: “스크립트 설정”아래의 모든 스니펫은 이 블록에서 시작합니다. 클라이언트 자격 증명으로 인증하고, 지역 API URL을 해석하고, 얇은 HTTP 헬퍼를 정의합니다.
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()