mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
MagicPlan HTTP client with auth headers and response parsing 🟩
This commit is contained in:
parent
ef5c44f7ae
commit
bbcde75b8e
2 changed files with 23 additions and 1 deletions
22
backend/magic_plan/magic_plan_client.py
Normal file
22
backend/magic_plan/magic_plan_client.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import requests
|
||||
|
||||
from datatypes.magicplan.api.response import MagicPlan, PlansListResponse
|
||||
|
||||
_BASE_URL = "https://cloud.magicplan.app/api/v2"
|
||||
|
||||
|
||||
class MagicPlanClient:
|
||||
def __init__(self, customer_id: str, api_key: str) -> None:
|
||||
self._api_key = api_key
|
||||
self._session = requests.Session()
|
||||
self._session.headers.update({"customer": customer_id})
|
||||
|
||||
def get_plans(self) -> PlansListResponse:
|
||||
r = self._session.get(f"{_BASE_URL}/plans", params={"key": self._api_key})
|
||||
r.raise_for_status()
|
||||
return PlansListResponse.model_validate(r.json()["data"])
|
||||
|
||||
def get_plan(self, plan_id: str) -> MagicPlan:
|
||||
r = self._session.get(f"{_BASE_URL}/plans/{plan_id}", params={"key": self._api_key})
|
||||
r.raise_for_status()
|
||||
return MagicPlan.model_validate(r.json()["data"])
|
||||
|
|
@ -26,7 +26,7 @@ def _make_client(mock_session: MagicMock) -> MagicPlanClient:
|
|||
|
||||
@pytest.fixture()
|
||||
def mock_session() -> MagicMock:
|
||||
return MagicMock(spec=requests.Session)
|
||||
return MagicMock()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue