# Generated by smithplates HTTP codegen. Do not edit.

from __future__ import annotations

from urllib.parse import quote

import httpx
from pydantic import Field  # noqa: F401

from generated.petstore.api.get_order_output import GetOrderOutput
from generated.petstore.api.order_not_found import OrderNotFound
from generated.petstore.api.petstore.client.client_response import parse_client_response
from generated.petstore.api.petstore.client.operation_bindings import OPERATION_HTTP_BINDINGS
from generated.petstore.api.place_order_input import PlaceOrderInput
from generated.petstore.api.place_order_output import PlaceOrderOutput
from generated.petstore.api.validation_error import ValidationError


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


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

    async def get_order(
        self,
        order_id: str,
    ) -> GetOrderOutput | OrderNotFound:
        headers: dict[str, str] | None = None
        _encoded_order_id = _encode_path_label(order_id)
        request_url = f"{self._base_url}/orders/{_encoded_order_id}"
        response = await self._client.request(
            "GET",
            request_url,
            headers=headers,
        )
        return parse_client_response(
            response,
            OPERATION_HTTP_BINDINGS["get_order"],
        )

    async def place_order(
        self,
        place_order_input: PlaceOrderInput,
    ) -> PlaceOrderOutput | ValidationError:
        headers: dict[str, str] | None = None

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