Co-authored-by: Clément Drouin <clement.drouin@mistral.ai> Co-authored-by: Corentin André <corentin.andre@mistral.ai> Co-authored-by: Guillaume LE GOFF <guillaume.lgf@gmail.com> Co-authored-by: Kim-Adeline Miguel <51720070+kimadeline@users.noreply.github.com> Co-authored-by: Maxime Dolores <maxime.dolores@ext.mistral.ai> Co-authored-by: Nelson PROIA <144663685+Nelson-PROIA@users.noreply.github.com> Co-authored-by: Peter Evers <pevers90@gmail.com> Co-authored-by: Pierre Rossinès <pierre.rossines@mistral.ai> Co-authored-by: Quentin <quentin.torroba@mistral.ai> Co-authored-by: Vincent G <10739306+VinceOPS@users.noreply.github.com> Co-authored-by: MichisGitIsKing <MichisGitIsKing@users.noreply.github.com> Co-authored-by: Mistral Vibe <vibe@mistral.ai>
45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Mapping
|
|
from typing import cast
|
|
|
|
import httpx
|
|
|
|
from vibe.cli.plan_offer.ports.whoami_gateway import (
|
|
WhoAmIGatewayError,
|
|
WhoAmIGatewayUnauthorized,
|
|
WhoAmIResponse,
|
|
)
|
|
from vibe.core.utils.http import build_ssl_context
|
|
|
|
WHOAMI_PATH = "/api/vibe/whoami"
|
|
|
|
|
|
class HttpWhoAmIGateway:
|
|
def __init__(self, base_url: str) -> None:
|
|
self._base_url = base_url.rstrip("/")
|
|
|
|
async def whoami(self, api_key: str) -> WhoAmIResponse:
|
|
url = f"{self._base_url}{WHOAMI_PATH}"
|
|
headers = {"Authorization": f"Bearer {api_key}"}
|
|
try:
|
|
async with httpx.AsyncClient(verify=build_ssl_context()) as client:
|
|
response = await client.get(url, headers=headers)
|
|
except httpx.RequestError as exc:
|
|
raise WhoAmIGatewayError() from exc
|
|
|
|
if response.status_code in {httpx.codes.UNAUTHORIZED, httpx.codes.FORBIDDEN}:
|
|
raise WhoAmIGatewayUnauthorized()
|
|
if not response.is_success:
|
|
raise WhoAmIGatewayError(f"Unexpected status {response.status_code}")
|
|
|
|
payload = _safe_json(response) or {}
|
|
return WhoAmIResponse.from_payload(payload)
|
|
|
|
|
|
def _safe_json(response: httpx.Response) -> Mapping[str, object] | None:
|
|
try:
|
|
data = response.json()
|
|
except ValueError:
|
|
return None
|
|
return cast(Mapping[str, object], data) if isinstance(data, dict) else None
|