# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

from typing import List, Union, Iterable
from datetime import datetime
from typing_extensions import Literal

import httpx

from ..._types import NOT_GIVEN, Body, Query, Headers, NotGiven
from ..._utils import maybe_transform, async_maybe_transform
from ..._compat import cached_property
from ..._resource import SyncAPIResource, AsyncAPIResource
from ..._response import (
    to_raw_response_wrapper,
    to_streamed_response_wrapper,
    async_to_raw_response_wrapper,
    async_to_streamed_response_wrapper,
)
from ..._base_client import make_request_options
from ...types.history import flow_update_params
from ...types.history.flow_history_item import FlowHistoryItem

__all__ = ["FlowResource", "AsyncFlowResource"]


class FlowResource(SyncAPIResource):
    @cached_property
    def with_raw_response(self) -> FlowResourceWithRawResponse:
        """
        This property can be used as a prefix for any HTTP method call to return
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/quarklabsai/quarkupy#accessing-raw-response-data-eg-headers
        """
        return FlowResourceWithRawResponse(self)

    @cached_property
    def with_streaming_response(self) -> FlowResourceWithStreamingResponse:
        """
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/quarklabsai/quarkupy#with_streaming_response
        """
        return FlowResourceWithStreamingResponse(self)

    def retrieve(
        self,
        id: str,
        *,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
    ) -> FlowHistoryItem:
        """
        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not id:
            raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
        extra_headers = {"Accept": "application/json; charset=utf-8", **(extra_headers or {})}
        return self._get(
            f"/history/flow/{id}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=FlowHistoryItem,
        )

    def update(
        self,
        *,
        created_at: Union[str, datetime],
        edges: Iterable[flow_update_params.Edge],
        flow_history_id: str,
        identity_id: str,
        input: object,
        metrics: object,
        nodes: List[str],
        registry_qrn: str,
        status: Literal["New", "Scheduled", "Running", "Completed", "Failed"],
        updated_at: Union[str, datetime],
        output: object | NotGiven = NOT_GIVEN,
        registry_id: str | NotGiven = NOT_GIVEN,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
    ) -> FlowHistoryItem:
        """
        Args:
          created_at: The timestamp when the Lattice was created.

          edges: A list of relationships (edges) between the nodes in the Lattice.

          flow_history_id: The unique identifier for the Lattice.

          identity_id: Identity of the Lattice Runner

          input: Input data provided to the Lattice, stored as a JSON value.

          metrics: Performance-related data or metrics associated with the Lattice.

          nodes: A list of unique identifiers for the nodes (Quarks) in the Lattice.

          registry_qrn: User-facing fully qualified identifier for the registry where the Flow is
              defined.

          status: Represents the status/stage of a Quark instance

          output: Optional output data generated by the Lattice, stored as a JSON value.

          registry_id: The identifier for the registry associated with the Lattice.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        extra_headers = {"Accept": "application/json; charset=utf-8", **(extra_headers or {})}
        return self._put(
            "/history/flow",
            body=maybe_transform(
                {
                    "created_at": created_at,
                    "edges": edges,
                    "flow_history_id": flow_history_id,
                    "identity_id": identity_id,
                    "input": input,
                    "metrics": metrics,
                    "nodes": nodes,
                    "registry_qrn": registry_qrn,
                    "status": status,
                    "updated_at": updated_at,
                    "output": output,
                    "registry_id": registry_id,
                },
                flow_update_params.FlowUpdateParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=FlowHistoryItem,
        )


class AsyncFlowResource(AsyncAPIResource):
    @cached_property
    def with_raw_response(self) -> AsyncFlowResourceWithRawResponse:
        """
        This property can be used as a prefix for any HTTP method call to return
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/quarklabsai/quarkupy#accessing-raw-response-data-eg-headers
        """
        return AsyncFlowResourceWithRawResponse(self)

    @cached_property
    def with_streaming_response(self) -> AsyncFlowResourceWithStreamingResponse:
        """
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/quarklabsai/quarkupy#with_streaming_response
        """
        return AsyncFlowResourceWithStreamingResponse(self)

    async def retrieve(
        self,
        id: str,
        *,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
    ) -> FlowHistoryItem:
        """
        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not id:
            raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
        extra_headers = {"Accept": "application/json; charset=utf-8", **(extra_headers or {})}
        return await self._get(
            f"/history/flow/{id}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=FlowHistoryItem,
        )

    async def update(
        self,
        *,
        created_at: Union[str, datetime],
        edges: Iterable[flow_update_params.Edge],
        flow_history_id: str,
        identity_id: str,
        input: object,
        metrics: object,
        nodes: List[str],
        registry_qrn: str,
        status: Literal["New", "Scheduled", "Running", "Completed", "Failed"],
        updated_at: Union[str, datetime],
        output: object | NotGiven = NOT_GIVEN,
        registry_id: str | NotGiven = NOT_GIVEN,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
    ) -> FlowHistoryItem:
        """
        Args:
          created_at: The timestamp when the Lattice was created.

          edges: A list of relationships (edges) between the nodes in the Lattice.

          flow_history_id: The unique identifier for the Lattice.

          identity_id: Identity of the Lattice Runner

          input: Input data provided to the Lattice, stored as a JSON value.

          metrics: Performance-related data or metrics associated with the Lattice.

          nodes: A list of unique identifiers for the nodes (Quarks) in the Lattice.

          registry_qrn: User-facing fully qualified identifier for the registry where the Flow is
              defined.

          status: Represents the status/stage of a Quark instance

          output: Optional output data generated by the Lattice, stored as a JSON value.

          registry_id: The identifier for the registry associated with the Lattice.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        extra_headers = {"Accept": "application/json; charset=utf-8", **(extra_headers or {})}
        return await self._put(
            "/history/flow",
            body=await async_maybe_transform(
                {
                    "created_at": created_at,
                    "edges": edges,
                    "flow_history_id": flow_history_id,
                    "identity_id": identity_id,
                    "input": input,
                    "metrics": metrics,
                    "nodes": nodes,
                    "registry_qrn": registry_qrn,
                    "status": status,
                    "updated_at": updated_at,
                    "output": output,
                    "registry_id": registry_id,
                },
                flow_update_params.FlowUpdateParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=FlowHistoryItem,
        )


class FlowResourceWithRawResponse:
    def __init__(self, flow: FlowResource) -> None:
        self._flow = flow

        self.retrieve = to_raw_response_wrapper(
            flow.retrieve,
        )
        self.update = to_raw_response_wrapper(
            flow.update,
        )


class AsyncFlowResourceWithRawResponse:
    def __init__(self, flow: AsyncFlowResource) -> None:
        self._flow = flow

        self.retrieve = async_to_raw_response_wrapper(
            flow.retrieve,
        )
        self.update = async_to_raw_response_wrapper(
            flow.update,
        )


class FlowResourceWithStreamingResponse:
    def __init__(self, flow: FlowResource) -> None:
        self._flow = flow

        self.retrieve = to_streamed_response_wrapper(
            flow.retrieve,
        )
        self.update = to_streamed_response_wrapper(
            flow.update,
        )


class AsyncFlowResourceWithStreamingResponse:
    def __init__(self, flow: AsyncFlowResource) -> None:
        self._flow = flow

        self.retrieve = async_to_streamed_response_wrapper(
            flow.retrieve,
        )
        self.update = async_to_streamed_response_wrapper(
            flow.update,
        )
