# This file was auto-generated by Fern from our API Definition.

import typing
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.http_response import AsyncHttpResponse, HttpResponse
from ..core.jsonable_encoder import jsonable_encoder
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.collect_status_response import CollectStatusResponse
from ..types.format import Format
from ..types.http_validation_error import HttpValidationError
from .types.fetch_trace_by_id_request_trace_id import FetchTraceByIdRequestTraceId
from .types.fetch_trace_by_id_response import FetchTraceByIdResponse
from .types.query_analytics_response import QueryAnalyticsResponse
from .types.query_traces_response import QueryTracesResponse


class RawObservabilityClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def otlp_v_1_traces(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[CollectStatusResponse]:
        """
        Receive traces via OTLP.

        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[CollectStatusResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "otlp/v1/traces",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CollectStatusResponse,
                    parse_obj_as(
                        type_=CollectStatusResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(
                status_code=_response.status_code,
                headers=dict(_response.headers),
                body=_response.text,
            )
        raise ApiError(
            status_code=_response.status_code,
            headers=dict(_response.headers),
            body=_response_json,
        )

    def otlp_status(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[CollectStatusResponse]:
        """
        Status of OTLP endpoint.

        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[CollectStatusResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "observability/v1/otlp/traces",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CollectStatusResponse,
                    parse_obj_as(
                        type_=CollectStatusResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(
                status_code=_response.status_code,
                headers=dict(_response.headers),
                body=_response.text,
            )
        raise ApiError(
            status_code=_response.status_code,
            headers=dict(_response.headers),
            body=_response_json,
        )

    def otlp_receiver(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[CollectStatusResponse]:
        """
        Receive traces via OTLP.

        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[CollectStatusResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "observability/v1/otlp/traces",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CollectStatusResponse,
                    parse_obj_as(
                        type_=CollectStatusResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(
                status_code=_response.status_code,
                headers=dict(_response.headers),
                body=_response.text,
            )
        raise ApiError(
            status_code=_response.status_code,
            headers=dict(_response.headers),
            body=_response_json,
        )

    def query_traces(
        self,
        *,
        format: typing.Optional[Format] = None,
        focus: typing.Optional[str] = None,
        oldest: typing.Optional[str] = None,
        newest: typing.Optional[str] = None,
        filtering: typing.Optional[str] = None,
        page: typing.Optional[int] = None,
        size: typing.Optional[int] = None,
        next: typing.Optional[str] = None,
        stop: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[QueryTracesResponse]:
        """
        Query traces, with optional grouping, windowing, filtering, and pagination.

        Parameters
        ----------
        format : typing.Optional[Format]

        focus : typing.Optional[str]

        oldest : typing.Optional[str]

        newest : typing.Optional[str]

        filtering : typing.Optional[str]

        page : typing.Optional[int]

        size : typing.Optional[int]

        next : typing.Optional[str]

        stop : typing.Optional[str]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[QueryTracesResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "observability/v1/traces",
            method="GET",
            params={
                "format": format,
                "focus": focus,
                "oldest": oldest,
                "newest": newest,
                "filtering": filtering,
                "page": page,
                "size": size,
                "next": next,
                "stop": stop,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    QueryTracesResponse,
                    parse_obj_as(
                        type_=QueryTracesResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(
                status_code=_response.status_code,
                headers=dict(_response.headers),
                body=_response.text,
            )
        raise ApiError(
            status_code=_response.status_code,
            headers=dict(_response.headers),
            body=_response_json,
        )

    def delete_traces(
        self,
        *,
        node_id: typing.Optional[str] = None,
        node_ids: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[CollectStatusResponse]:
        """
        Delete trace.

        Parameters
        ----------
        node_id : typing.Optional[str]

        node_ids : typing.Optional[typing.Union[str, typing.Sequence[str]]]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[CollectStatusResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "observability/v1/traces",
            method="DELETE",
            params={
                "node_id": node_id,
                "node_ids": node_ids,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CollectStatusResponse,
                    parse_obj_as(
                        type_=CollectStatusResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(
                status_code=_response.status_code,
                headers=dict(_response.headers),
                body=_response.text,
            )
        raise ApiError(
            status_code=_response.status_code,
            headers=dict(_response.headers),
            body=_response_json,
        )

    def query_analytics(
        self,
        *,
        format: typing.Optional[Format] = None,
        focus: typing.Optional[str] = None,
        oldest: typing.Optional[str] = None,
        newest: typing.Optional[str] = None,
        window: typing.Optional[int] = None,
        filtering: typing.Optional[str] = None,
        time_range: typing.Optional[str] = None,
        app_id: typing.Optional[str] = None,
        environment: typing.Optional[str] = None,
        variant: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[QueryAnalyticsResponse]:
        """
        Parameters
        ----------
        format : typing.Optional[Format]

        focus : typing.Optional[str]

        oldest : typing.Optional[str]

        newest : typing.Optional[str]

        window : typing.Optional[int]

        filtering : typing.Optional[str]

        time_range : typing.Optional[str]

        app_id : typing.Optional[str]

        environment : typing.Optional[str]

        variant : typing.Optional[str]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[QueryAnalyticsResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "observability/v1/analytics",
            method="GET",
            params={
                "format": format,
                "focus": focus,
                "oldest": oldest,
                "newest": newest,
                "window": window,
                "filtering": filtering,
                "timeRange": time_range,
                "app_id": app_id,
                "environment": environment,
                "variant": variant,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    QueryAnalyticsResponse,
                    parse_obj_as(
                        type_=QueryAnalyticsResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(
                status_code=_response.status_code,
                headers=dict(_response.headers),
                body=_response.text,
            )
        raise ApiError(
            status_code=_response.status_code,
            headers=dict(_response.headers),
            body=_response_json,
        )

    def fetch_trace_by_id(
        self,
        trace_id: FetchTraceByIdRequestTraceId,
        *,
        format: typing.Optional[Format] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[FetchTraceByIdResponse]:
        """
        Fetch trace by ID.

        Parameters
        ----------
        trace_id : FetchTraceByIdRequestTraceId

        format : typing.Optional[Format]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[FetchTraceByIdResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"observability/v1/traces/{jsonable_encoder(trace_id)}",
            method="GET",
            params={
                "format": format,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    FetchTraceByIdResponse,
                    parse_obj_as(
                        type_=FetchTraceByIdResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(
                status_code=_response.status_code,
                headers=dict(_response.headers),
                body=_response.text,
            )
        raise ApiError(
            status_code=_response.status_code,
            headers=dict(_response.headers),
            body=_response_json,
        )


class AsyncRawObservabilityClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def otlp_v_1_traces(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[CollectStatusResponse]:
        """
        Receive traces via OTLP.

        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[CollectStatusResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "otlp/v1/traces",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CollectStatusResponse,
                    parse_obj_as(
                        type_=CollectStatusResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(
                status_code=_response.status_code,
                headers=dict(_response.headers),
                body=_response.text,
            )
        raise ApiError(
            status_code=_response.status_code,
            headers=dict(_response.headers),
            body=_response_json,
        )

    async def otlp_status(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[CollectStatusResponse]:
        """
        Status of OTLP endpoint.

        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[CollectStatusResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "observability/v1/otlp/traces",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CollectStatusResponse,
                    parse_obj_as(
                        type_=CollectStatusResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(
                status_code=_response.status_code,
                headers=dict(_response.headers),
                body=_response.text,
            )
        raise ApiError(
            status_code=_response.status_code,
            headers=dict(_response.headers),
            body=_response_json,
        )

    async def otlp_receiver(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[CollectStatusResponse]:
        """
        Receive traces via OTLP.

        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[CollectStatusResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "observability/v1/otlp/traces",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CollectStatusResponse,
                    parse_obj_as(
                        type_=CollectStatusResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(
                status_code=_response.status_code,
                headers=dict(_response.headers),
                body=_response.text,
            )
        raise ApiError(
            status_code=_response.status_code,
            headers=dict(_response.headers),
            body=_response_json,
        )

    async def query_traces(
        self,
        *,
        format: typing.Optional[Format] = None,
        focus: typing.Optional[str] = None,
        oldest: typing.Optional[str] = None,
        newest: typing.Optional[str] = None,
        filtering: typing.Optional[str] = None,
        page: typing.Optional[int] = None,
        size: typing.Optional[int] = None,
        next: typing.Optional[str] = None,
        stop: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[QueryTracesResponse]:
        """
        Query traces, with optional grouping, windowing, filtering, and pagination.

        Parameters
        ----------
        format : typing.Optional[Format]

        focus : typing.Optional[str]

        oldest : typing.Optional[str]

        newest : typing.Optional[str]

        filtering : typing.Optional[str]

        page : typing.Optional[int]

        size : typing.Optional[int]

        next : typing.Optional[str]

        stop : typing.Optional[str]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[QueryTracesResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "observability/v1/traces",
            method="GET",
            params={
                "format": format,
                "focus": focus,
                "oldest": oldest,
                "newest": newest,
                "filtering": filtering,
                "page": page,
                "size": size,
                "next": next,
                "stop": stop,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    QueryTracesResponse,
                    parse_obj_as(
                        type_=QueryTracesResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(
                status_code=_response.status_code,
                headers=dict(_response.headers),
                body=_response.text,
            )
        raise ApiError(
            status_code=_response.status_code,
            headers=dict(_response.headers),
            body=_response_json,
        )

    async def delete_traces(
        self,
        *,
        node_id: typing.Optional[str] = None,
        node_ids: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[CollectStatusResponse]:
        """
        Delete trace.

        Parameters
        ----------
        node_id : typing.Optional[str]

        node_ids : typing.Optional[typing.Union[str, typing.Sequence[str]]]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[CollectStatusResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "observability/v1/traces",
            method="DELETE",
            params={
                "node_id": node_id,
                "node_ids": node_ids,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CollectStatusResponse,
                    parse_obj_as(
                        type_=CollectStatusResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(
                status_code=_response.status_code,
                headers=dict(_response.headers),
                body=_response.text,
            )
        raise ApiError(
            status_code=_response.status_code,
            headers=dict(_response.headers),
            body=_response_json,
        )

    async def query_analytics(
        self,
        *,
        format: typing.Optional[Format] = None,
        focus: typing.Optional[str] = None,
        oldest: typing.Optional[str] = None,
        newest: typing.Optional[str] = None,
        window: typing.Optional[int] = None,
        filtering: typing.Optional[str] = None,
        time_range: typing.Optional[str] = None,
        app_id: typing.Optional[str] = None,
        environment: typing.Optional[str] = None,
        variant: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[QueryAnalyticsResponse]:
        """
        Parameters
        ----------
        format : typing.Optional[Format]

        focus : typing.Optional[str]

        oldest : typing.Optional[str]

        newest : typing.Optional[str]

        window : typing.Optional[int]

        filtering : typing.Optional[str]

        time_range : typing.Optional[str]

        app_id : typing.Optional[str]

        environment : typing.Optional[str]

        variant : typing.Optional[str]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[QueryAnalyticsResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "observability/v1/analytics",
            method="GET",
            params={
                "format": format,
                "focus": focus,
                "oldest": oldest,
                "newest": newest,
                "window": window,
                "filtering": filtering,
                "timeRange": time_range,
                "app_id": app_id,
                "environment": environment,
                "variant": variant,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    QueryAnalyticsResponse,
                    parse_obj_as(
                        type_=QueryAnalyticsResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(
                status_code=_response.status_code,
                headers=dict(_response.headers),
                body=_response.text,
            )
        raise ApiError(
            status_code=_response.status_code,
            headers=dict(_response.headers),
            body=_response_json,
        )

    async def fetch_trace_by_id(
        self,
        trace_id: FetchTraceByIdRequestTraceId,
        *,
        format: typing.Optional[Format] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[FetchTraceByIdResponse]:
        """
        Fetch trace by ID.

        Parameters
        ----------
        trace_id : FetchTraceByIdRequestTraceId

        format : typing.Optional[Format]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[FetchTraceByIdResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"observability/v1/traces/{jsonable_encoder(trace_id)}",
            method="GET",
            params={
                "format": format,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    FetchTraceByIdResponse,
                    parse_obj_as(
                        type_=FetchTraceByIdResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(
                status_code=_response.status_code,
                headers=dict(_response.headers),
                body=_response.text,
            )
        raise ApiError(
            status_code=_response.status_code,
            headers=dict(_response.headers),
            body=_response_json,
        )
