Initial commit
This commit is contained in:
52
api_client.py
Normal file
52
api_client.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import requests
|
||||
from qgis.core import QgsMessageLog, Qgis
|
||||
|
||||
|
||||
class ApiClient:
|
||||
"""Fetches location records from the external REST API."""
|
||||
|
||||
def __init__(self, base_url: str):
|
||||
self.base_url = base_url.rstrip("/")
|
||||
|
||||
def fetch_locations(self) -> list[dict]:
|
||||
"""GET /api/locations and return a list of location dicts.
|
||||
|
||||
Each dict contains: id (int), name (str), latitude (float), longitude (float).
|
||||
|
||||
Raises:
|
||||
requests.RequestException: on any network or HTTP error.
|
||||
ValueError: if the response body is not valid JSON or missing expected keys.
|
||||
"""
|
||||
url = f"{self.base_url}/api/locations"
|
||||
try:
|
||||
response = requests.get(url, timeout=10)
|
||||
response.raise_for_status()
|
||||
except requests.HTTPError as exc:
|
||||
QgsMessageLog.logMessage(
|
||||
f"HTTP error fetching locations from {url}: {exc}",
|
||||
"BAPEBridge",
|
||||
Qgis.Warning,
|
||||
)
|
||||
raise
|
||||
except requests.RequestException as exc:
|
||||
QgsMessageLog.logMessage(
|
||||
f"Network error fetching locations from {url}: {exc}",
|
||||
"BAPEBridge",
|
||||
Qgis.Warning,
|
||||
)
|
||||
raise
|
||||
|
||||
try:
|
||||
data = response.json()
|
||||
except ValueError as exc:
|
||||
QgsMessageLog.logMessage(
|
||||
f"Invalid JSON in locations response: {exc}",
|
||||
"BAPEBridge",
|
||||
Qgis.Warning,
|
||||
)
|
||||
raise
|
||||
|
||||
if not isinstance(data, list):
|
||||
raise ValueError(f"Expected a JSON array, got {type(data).__name__}")
|
||||
|
||||
return data
|
||||
Reference in New Issue
Block a user