# Generated by smithplates HTTP codegen. Do not edit.

from __future__ import annotations

from datetime import datetime, timezone
from email.utils import format_datetime
from typing import cast
from urllib.parse import quote, urlencode

import httpx2
from generated.example.auth_output import AuthOutput
from generated.example.query_auth_api.client.client_response import parse_client_response
from generated.example.query_auth_api.client.operation_bindings import (
    OPERATION_AUTH_BINDINGS,
    OPERATION_HTTP_BINDINGS,
    AuthProvider,
    apply_operation_auth,
)
from pydantic import Field  # noqa: F401


def _serialize_query_value(value: object, *, timestamp_format: str = "date-time") -> str:
    if isinstance(value, bool):
        return str(value).lower()
    if isinstance(value, datetime):
        if timestamp_format == "epoch-seconds":
            return str(value.timestamp())
        if timestamp_format == "http-date":
            if value.tzinfo is None:
                value = value.replace(tzinfo=timezone.utc)
            return format_datetime(value.astimezone(timezone.utc), usegmt=True)
        return value.isoformat()
    return str(value)


class QueryAuthApiClient:
    def __init__(self, client: httpx2.Client, base_url: str, auth_provider: AuthProvider | None = None) -> None:
        self._client = client
        self._base_url = base_url.rstrip("/")
        self._auth_provider = auth_provider

    def query_api_key(
        self,
        filter: str | None,
    ) -> AuthOutput:
        headers: dict[str, str] = {}

        query_params: list[tuple[str, str]] = []
        if filter is not None:
            query_params.append(("filter", _serialize_query_value(filter)))
        apply_operation_auth(
            OPERATION_AUTH_BINDINGS["query_api_key"],
            self._auth_provider,
            headers,
            query_params,
        )
        query_string = urlencode(query_params, quote_via=quote)
        request_url = f"{self._base_url}/query-key"
        if query_string:
            request_url = f"{request_url}?{query_string}"
        response = self._client.request(
            "GET",
            request_url,
            headers=headers,
        )
        return cast(
            AuthOutput,
            parse_client_response(
                response,
                OPERATION_HTTP_BINDINGS["query_api_key"],
            ),
        )
