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

from __future__ import annotations

import httpx

from ...._types import Body, Query, Headers, NotGiven, not_given
from ...._compat import cached_property
from ...._resource import SyncAPIResource, AsyncAPIResource
from ...._response import (
    BinaryAPIResponse,
    AsyncBinaryAPIResponse,
    StreamedBinaryAPIResponse,
    AsyncStreamedBinaryAPIResponse,
    to_raw_response_wrapper,
    to_streamed_response_wrapper,
    async_to_raw_response_wrapper,
    to_custom_raw_response_wrapper,
    async_to_streamed_response_wrapper,
    to_custom_streamed_response_wrapper,
    async_to_custom_raw_response_wrapper,
    async_to_custom_streamed_response_wrapper,
)
from ...._base_client import make_request_options
from ....types.edgar.filing.document_retrieve_response import DocumentRetrieveResponse

__all__ = ["DocumentResource", "AsyncDocumentResource"]


class DocumentResource(SyncAPIResource):
    """Direct access to SEC EDGAR filings and documents by accession number."""

    @cached_property
    def with_raw_response(self) -> DocumentResourceWithRawResponse:
        """
        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/village-dev/village-python#accessing-raw-response-data-eg-headers
        """
        return DocumentResourceWithRawResponse(self)

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

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

    def retrieve(
        self,
        sequence: int,
        *,
        accession_number: 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,
    ) -> DocumentRetrieveResponse:
        """
        Retrieve a specific document from an SEC filing by its accession number and
        sequence number. Returns the document metadata (ToC) along with the parent
        filing's metadata, documents, and entities.

        Args:
          accession_number: The SEC accession number of the filing (e.g. '0000320193-23-000077').

          sequence: The sequence number of the document within the filing.

          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 accession_number:
            raise ValueError(f"Expected a non-empty value for `accession_number` but received {accession_number!r}")
        return self._get(
            f"/edgar/filing/{accession_number}/document/{sequence}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=DocumentRetrieveResponse,
        )

    def download_pdf(
        self,
        sequence: int,
        *,
        accession_number: 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,
    ) -> BinaryAPIResponse:
        """
        Download the rendered PDF version of an SEC filing document.

        The response is served with indefinite cache headers since rendered PDFs are
        immutable artifacts.

        Args:
          accession_number: The SEC accession number of the filing (e.g. '0000320193-23-000077').

          sequence: The sequence number of the document within the filing.

          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 accession_number:
            raise ValueError(f"Expected a non-empty value for `accession_number` but received {accession_number!r}")
        extra_headers = {"Accept": "application/pdf", **(extra_headers or {})}
        return self._get(
            f"/edgar/filing/{accession_number}/document/{sequence}/pdf",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=BinaryAPIResponse,
        )

    def get_text(
        self,
        sequence: int,
        *,
        accession_number: 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,
    ) -> str:
        """
        Get the extracted plaintext of an SEC filing document.

        Returns the document's text content reconstructed from parsed chunks, served
        with indefinite cache headers since extracted text is an immutable artifact.

        Args:
          accession_number: The SEC accession number of the filing (e.g. '0000320193-23-000077').

          sequence: The sequence number of the document within the filing.

          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 accession_number:
            raise ValueError(f"Expected a non-empty value for `accession_number` but received {accession_number!r}")
        extra_headers = {"Accept": "text/plain", **(extra_headers or {})}
        return self._get(
            f"/edgar/filing/{accession_number}/document/{sequence}/text",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=str,
        )


class AsyncDocumentResource(AsyncAPIResource):
    """Direct access to SEC EDGAR filings and documents by accession number."""

    @cached_property
    def with_raw_response(self) -> AsyncDocumentResourceWithRawResponse:
        """
        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/village-dev/village-python#accessing-raw-response-data-eg-headers
        """
        return AsyncDocumentResourceWithRawResponse(self)

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

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

    async def retrieve(
        self,
        sequence: int,
        *,
        accession_number: 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,
    ) -> DocumentRetrieveResponse:
        """
        Retrieve a specific document from an SEC filing by its accession number and
        sequence number. Returns the document metadata (ToC) along with the parent
        filing's metadata, documents, and entities.

        Args:
          accession_number: The SEC accession number of the filing (e.g. '0000320193-23-000077').

          sequence: The sequence number of the document within the filing.

          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 accession_number:
            raise ValueError(f"Expected a non-empty value for `accession_number` but received {accession_number!r}")
        return await self._get(
            f"/edgar/filing/{accession_number}/document/{sequence}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=DocumentRetrieveResponse,
        )

    async def download_pdf(
        self,
        sequence: int,
        *,
        accession_number: 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,
    ) -> AsyncBinaryAPIResponse:
        """
        Download the rendered PDF version of an SEC filing document.

        The response is served with indefinite cache headers since rendered PDFs are
        immutable artifacts.

        Args:
          accession_number: The SEC accession number of the filing (e.g. '0000320193-23-000077').

          sequence: The sequence number of the document within the filing.

          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 accession_number:
            raise ValueError(f"Expected a non-empty value for `accession_number` but received {accession_number!r}")
        extra_headers = {"Accept": "application/pdf", **(extra_headers or {})}
        return await self._get(
            f"/edgar/filing/{accession_number}/document/{sequence}/pdf",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=AsyncBinaryAPIResponse,
        )

    async def get_text(
        self,
        sequence: int,
        *,
        accession_number: 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,
    ) -> str:
        """
        Get the extracted plaintext of an SEC filing document.

        Returns the document's text content reconstructed from parsed chunks, served
        with indefinite cache headers since extracted text is an immutable artifact.

        Args:
          accession_number: The SEC accession number of the filing (e.g. '0000320193-23-000077').

          sequence: The sequence number of the document within the filing.

          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 accession_number:
            raise ValueError(f"Expected a non-empty value for `accession_number` but received {accession_number!r}")
        extra_headers = {"Accept": "text/plain", **(extra_headers or {})}
        return await self._get(
            f"/edgar/filing/{accession_number}/document/{sequence}/text",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=str,
        )


class DocumentResourceWithRawResponse:
    def __init__(self, document: DocumentResource) -> None:
        self._document = document

        self.retrieve = to_raw_response_wrapper(
            document.retrieve,
        )
        self.download_pdf = to_custom_raw_response_wrapper(
            document.download_pdf,
            BinaryAPIResponse,
        )
        self.get_text = to_raw_response_wrapper(
            document.get_text,
        )


class AsyncDocumentResourceWithRawResponse:
    def __init__(self, document: AsyncDocumentResource) -> None:
        self._document = document

        self.retrieve = async_to_raw_response_wrapper(
            document.retrieve,
        )
        self.download_pdf = async_to_custom_raw_response_wrapper(
            document.download_pdf,
            AsyncBinaryAPIResponse,
        )
        self.get_text = async_to_raw_response_wrapper(
            document.get_text,
        )


class DocumentResourceWithStreamingResponse:
    def __init__(self, document: DocumentResource) -> None:
        self._document = document

        self.retrieve = to_streamed_response_wrapper(
            document.retrieve,
        )
        self.download_pdf = to_custom_streamed_response_wrapper(
            document.download_pdf,
            StreamedBinaryAPIResponse,
        )
        self.get_text = to_streamed_response_wrapper(
            document.get_text,
        )


class AsyncDocumentResourceWithStreamingResponse:
    def __init__(self, document: AsyncDocumentResource) -> None:
        self._document = document

        self.retrieve = async_to_streamed_response_wrapper(
            document.retrieve,
        )
        self.download_pdf = async_to_custom_streamed_response_wrapper(
            document.download_pdf,
            AsyncStreamedBinaryAPIResponse,
        )
        self.get_text = async_to_streamed_response_wrapper(
            document.get_text,
        )
