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

from __future__ import annotations

from typing import Type, Union, Optional, cast
from datetime import datetime

import httpx

from ..._types import Body, Omit, Query, Headers, NotGiven, SequenceNotStr, omit, not_given
from ..._utils import path_template, maybe_transform, strip_not_given, 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 ..._wrappers import ResultWrapper
from ..._base_client import make_request_options
from ...types.stream import copy_create_params
from ...types.stream.video import Video
from ...types.stream.allowed_origins import AllowedOrigins

__all__ = ["CopyResource", "AsyncCopyResource"]


class CopyResource(SyncAPIResource):
    @cached_property
    def with_raw_response(self) -> CopyResourceWithRawResponse:
        """
        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/cloudflare/cloudflare-python#accessing-raw-response-data-eg-headers
        """
        return CopyResourceWithRawResponse(self)

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

        For more information, see https://www.github.com/cloudflare/cloudflare-python#with_streaming_response
        """
        return CopyResourceWithStreamingResponse(self)

    def create(
        self,
        *,
        account_id: str,
        allowed_origins: SequenceNotStr[AllowedOrigins] | Omit = omit,
        creator: str | Omit = omit,
        input: str | Omit = omit,
        meta: object | Omit = omit,
        name: str | Omit = omit,
        require_signed_urls: bool | Omit = omit,
        scheduled_deletion: Union[str, datetime] | Omit = omit,
        thumbnail_timestamp_pct: float | Omit = omit,
        url: str | Omit = omit,
        watermark: copy_create_params.Watermark | Omit = omit,
        upload_creator: str | Omit = omit,
        # 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,
    ) -> Optional[Video]:
        """
        Uploads a video to Stream from a provided URL.

        Args:
          account_id: The account identifier tag.

          allowed_origins: Lists the origins allowed to display the video. Enter allowed origin domains in
              an array and use `*` for wildcard subdomains. Empty arrays allow the video to be
              viewed on any origin.

          creator: A user-defined identifier for the media creator.

          input: A video's URL. The server must be publicly routable and support `HTTP HEAD`
              requests and `HTTP GET` range requests. The server should respond to `HTTP HEAD`
              requests with a `content-range` header that includes the size of the file. This
              is the preferred field over `url`.

          meta: A user modifiable key-value store used to reference other systems of record for
              managing videos.

          name: A video's name. Used for legacy compatibility.

          require_signed_urls: Indicates whether the video can be a accessed using the UID. When set to `true`,
              a signed token must be generated with a signing key to view the video.

          scheduled_deletion: Indicates the date and time at which the video will be deleted. Omit the field
              to indicate no change, or include with a `null` value to remove an existing
              scheduled deletion. If specified, must be at least 30 days from upload time.

          thumbnail_timestamp_pct: The timestamp for a thumbnail image calculated as a percentage value of the
              video's duration. To convert from a second-wise timestamp to a percentage,
              divide the desired timestamp by the total duration of the video. If this value
              is not set, the default thumbnail image is taken from 0s of the video.

          url: A video's URL. The server must be publicly routable and support `HTTP HEAD`
              requests and `HTTP GET` range requests. The server should respond to `HTTP HEAD`
              requests with a `content-range` header that includes the size of the file. This
              field is deprecated in favor of `input`.

          upload_creator: A user-defined identifier for the media creator.

          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 account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        extra_headers = {**strip_not_given({"Upload-Creator": upload_creator}), **(extra_headers or {})}
        return self._post(
            path_template("/accounts/{account_id}/stream/copy", account_id=account_id),
            body=maybe_transform(
                {
                    "allowed_origins": allowed_origins,
                    "creator": creator,
                    "input": input,
                    "meta": meta,
                    "name": name,
                    "require_signed_urls": require_signed_urls,
                    "scheduled_deletion": scheduled_deletion,
                    "thumbnail_timestamp_pct": thumbnail_timestamp_pct,
                    "url": url,
                    "watermark": watermark,
                },
                copy_create_params.CopyCreateParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[Optional[Video]]._unwrapper,
            ),
            cast_to=cast(Type[Optional[Video]], ResultWrapper[Video]),
        )


class AsyncCopyResource(AsyncAPIResource):
    @cached_property
    def with_raw_response(self) -> AsyncCopyResourceWithRawResponse:
        """
        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/cloudflare/cloudflare-python#accessing-raw-response-data-eg-headers
        """
        return AsyncCopyResourceWithRawResponse(self)

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

        For more information, see https://www.github.com/cloudflare/cloudflare-python#with_streaming_response
        """
        return AsyncCopyResourceWithStreamingResponse(self)

    async def create(
        self,
        *,
        account_id: str,
        allowed_origins: SequenceNotStr[AllowedOrigins] | Omit = omit,
        creator: str | Omit = omit,
        input: str | Omit = omit,
        meta: object | Omit = omit,
        name: str | Omit = omit,
        require_signed_urls: bool | Omit = omit,
        scheduled_deletion: Union[str, datetime] | Omit = omit,
        thumbnail_timestamp_pct: float | Omit = omit,
        url: str | Omit = omit,
        watermark: copy_create_params.Watermark | Omit = omit,
        upload_creator: str | Omit = omit,
        # 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,
    ) -> Optional[Video]:
        """
        Uploads a video to Stream from a provided URL.

        Args:
          account_id: The account identifier tag.

          allowed_origins: Lists the origins allowed to display the video. Enter allowed origin domains in
              an array and use `*` for wildcard subdomains. Empty arrays allow the video to be
              viewed on any origin.

          creator: A user-defined identifier for the media creator.

          input: A video's URL. The server must be publicly routable and support `HTTP HEAD`
              requests and `HTTP GET` range requests. The server should respond to `HTTP HEAD`
              requests with a `content-range` header that includes the size of the file. This
              is the preferred field over `url`.

          meta: A user modifiable key-value store used to reference other systems of record for
              managing videos.

          name: A video's name. Used for legacy compatibility.

          require_signed_urls: Indicates whether the video can be a accessed using the UID. When set to `true`,
              a signed token must be generated with a signing key to view the video.

          scheduled_deletion: Indicates the date and time at which the video will be deleted. Omit the field
              to indicate no change, or include with a `null` value to remove an existing
              scheduled deletion. If specified, must be at least 30 days from upload time.

          thumbnail_timestamp_pct: The timestamp for a thumbnail image calculated as a percentage value of the
              video's duration. To convert from a second-wise timestamp to a percentage,
              divide the desired timestamp by the total duration of the video. If this value
              is not set, the default thumbnail image is taken from 0s of the video.

          url: A video's URL. The server must be publicly routable and support `HTTP HEAD`
              requests and `HTTP GET` range requests. The server should respond to `HTTP HEAD`
              requests with a `content-range` header that includes the size of the file. This
              field is deprecated in favor of `input`.

          upload_creator: A user-defined identifier for the media creator.

          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 account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        extra_headers = {**strip_not_given({"Upload-Creator": upload_creator}), **(extra_headers or {})}
        return await self._post(
            path_template("/accounts/{account_id}/stream/copy", account_id=account_id),
            body=await async_maybe_transform(
                {
                    "allowed_origins": allowed_origins,
                    "creator": creator,
                    "input": input,
                    "meta": meta,
                    "name": name,
                    "require_signed_urls": require_signed_urls,
                    "scheduled_deletion": scheduled_deletion,
                    "thumbnail_timestamp_pct": thumbnail_timestamp_pct,
                    "url": url,
                    "watermark": watermark,
                },
                copy_create_params.CopyCreateParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[Optional[Video]]._unwrapper,
            ),
            cast_to=cast(Type[Optional[Video]], ResultWrapper[Video]),
        )


class CopyResourceWithRawResponse:
    def __init__(self, copy: CopyResource) -> None:
        self._copy = copy

        self.create = to_raw_response_wrapper(
            copy.create,
        )


class AsyncCopyResourceWithRawResponse:
    def __init__(self, copy: AsyncCopyResource) -> None:
        self._copy = copy

        self.create = async_to_raw_response_wrapper(
            copy.create,
        )


class CopyResourceWithStreamingResponse:
    def __init__(self, copy: CopyResource) -> None:
        self._copy = copy

        self.create = to_streamed_response_wrapper(
            copy.create,
        )


class AsyncCopyResourceWithStreamingResponse:
    def __init__(self, copy: AsyncCopyResource) -> None:
        self._copy = copy

        self.create = async_to_streamed_response_wrapper(
            copy.create,
        )
