# Generated by smithplates HTTP codegen. Do not edit.

from __future__ import annotations

from urllib.parse import quote

import httpx
from generated.example.widget_create_request import WidgetCreateRequest
from generated.example.widget_summary import WidgetSummary
from generated.widget_client.widget_api.client.client_response import parse_client_response
from generated.widget_client.widget_api.client.operation_bindings import OPERATION_HTTP_BINDINGS
from pydantic import Field  # noqa: F401


def _encode_path_label(value: object, *, greedy: bool = False) -> str:
    return quote(str(value), safe="/" if greedy else "")


class WidgetsApiClient:
    def __init__(self, client: httpx.Client, base_url: str) -> None:
        self._client = client
        self._base_url = base_url.rstrip("/")

    def create_widget(
        self,
        body: WidgetCreateRequest,
    ) -> WidgetSummary:
        headers: dict[str, str] | None = None

        request_url = f"{self._base_url}/widgets"
        response = self._client.request(
            "POST",
            request_url,
            headers=headers,
            json=body.model_dump(mode="json", exclude_none=True),
        )
        return parse_client_response(
            response,
            OPERATION_HTTP_BINDINGS["create_widget"],
        )

    def get_widget(
        self,
        widget_id: str,
    ) -> WidgetSummary:
        headers: dict[str, str] | None = None
        _encoded_widget_id = _encode_path_label(widget_id)
        request_url = f"{self._base_url}/widgets/{_encoded_widget_id}"
        response = self._client.request(
            "GET",
            request_url,
            headers=headers,
        )
        return parse_client_response(
            response,
            OPERATION_HTTP_BINDINGS["get_widget"],
        )
