# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. from __future__ import annotations from typing_extensions import Literal import httpx from ..types import file_list_params, file_retry_params, file_upload_params from .._types import Body, Omit, Query, Headers, NotGiven, FileTypes, SequenceNotStr, omit, not_given from .._utils import path_template, maybe_transform, async_maybe_transform 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 ..pagination import SyncCursorPage, AsyncCursorPage from ..types.file import File from .._base_client import AsyncPaginator, make_request_options from ..types.file_retry_response import FileRetryResponse from ..types.file_delete_response import FileDeleteResponse __all__ = ["FilesResource", "AsyncFilesResource"] class FilesResource(SyncAPIResource): @cached_property def with_raw_response(self) -> FilesResourceWithRawResponse: """ 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/writer/writer-python#accessing-raw-response-data-eg-headers """ return FilesResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> FilesResourceWithStreamingResponse: """ An alternative to `.with_raw_response` that doesn't eagerly read the response body. For more information, see https://www.github.com/writer/writer-python#with_streaming_response """ return FilesResourceWithStreamingResponse(self) def retrieve( self, file_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, ) -> File: """ Retrieve detailed information about a specific file, including its metadata, status, and associated graphs. 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 file_id: raise ValueError(f"Expected a non-empty value for `file_id` but received {file_id!r}") return self._get( path_template("/v1/files/{file_id}", file_id=file_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=File, ) def list( self, *, after: str | Omit = omit, before: str | Omit = omit, file_types: str | Omit = omit, graph_id: str | Omit = omit, limit: int | Omit = omit, order: Literal["asc", "desc"] | Omit = omit, status: Literal["in_progress", "completed", "failed"] | 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, ) -> SyncCursorPage[File]: """ Retrieve a paginated list of files with optional filtering by status, graph association, and file type. Args: after: The ID of the last object in the previous page. This parameter instructs the API to return the next page of results. before: The ID of the first object in the previous page. This parameter instructs the API to return the previous page of results. file_types: The extensions of the files to retrieve. Separate multiple extensions with a comma. For example: `pdf,jpg,docx`. graph_id: The unique identifier of the graph to which the files belong. limit: Specifies the maximum number of objects returned in a page. The default value is 50. The minimum value is 1, and the maximum value is 100. order: Specifies the order of the results. Valid values are asc for ascending and desc for descending. status: Specifies the status of the files to retrieve. Valid values are in_progress, completed or failed. 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 """ return self._get_api_list( "/v1/files", page=SyncCursorPage[File], options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout, query=maybe_transform( { "after": after, "before": before, "file_types": file_types, "graph_id": graph_id, "limit": limit, "order": order, "status": status, }, file_list_params.FileListParams, ), ), model=File, ) def delete( self, file_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, ) -> FileDeleteResponse: """Permanently delete a file from the system. This action cannot be undone. 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 file_id: raise ValueError(f"Expected a non-empty value for `file_id` but received {file_id!r}") return self._delete( path_template("/v1/files/{file_id}", file_id=file_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=FileDeleteResponse, ) def download( self, file_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, ) -> BinaryAPIResponse: """Download the binary content of a file. The response will contain the file data in the appropriate MIME type. 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 file_id: raise ValueError(f"Expected a non-empty value for `file_id` but received {file_id!r}") extra_headers = {"Accept": "application/octet-stream", **(extra_headers or {})} return self._get( path_template("/v1/files/{file_id}/download", file_id=file_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=BinaryAPIResponse, ) def retry( self, *, file_ids: SequenceNotStr[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, ) -> FileRetryResponse: """Retry processing of files that previously failed to process. This will re-attempt the processing of the specified files. Args: file_ids: The unique identifier of the files to retry. 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 """ return self._post( "/v1/files/retry", body=maybe_transform({"file_ids": file_ids}, file_retry_params.FileRetryParams), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=FileRetryResponse, ) def upload( self, content: FileTypes, *, content_disposition: str, content_type: str, graph_id: 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, ) -> File: """Upload a new file to the system. Supports various file formats including PDF, DOC, DOCX, PPT, PPTX, JPG, PNG, EML, HTML, SRT, CSV, XLS, and XLSX. File uploads do not accept team parameters. When you attach a file to a Knowledge Graph, the file inherits the team scope of that Knowledge Graph. Args: graph_id: The unique identifier of the Knowledge Graph to associate the uploaded file with. Note: The response from the upload endpoint does not include the `graphId` field, but the association will be visible when you retrieve the file using the file retrieval endpoint. 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 = { "Content-Disposition": content_disposition, "Content-Type": content_type, **(extra_headers or {}), } extra_headers["Content-Type"] = "text/plain" return self._post( "/v1/files", options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout, query=maybe_transform({"graph_id": graph_id}, file_upload_params.FileUploadParams), ), binary_request=content, cast_to=File, ) class AsyncFilesResource(AsyncAPIResource): @cached_property def with_raw_response(self) -> AsyncFilesResourceWithRawResponse: """ 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/writer/writer-python#accessing-raw-response-data-eg-headers """ return AsyncFilesResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> AsyncFilesResourceWithStreamingResponse: """ An alternative to `.with_raw_response` that doesn't eagerly read the response body. For more information, see https://www.github.com/writer/writer-python#with_streaming_response """ return AsyncFilesResourceWithStreamingResponse(self) async def retrieve( self, file_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, ) -> File: """ Retrieve detailed information about a specific file, including its metadata, status, and associated graphs. 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 file_id: raise ValueError(f"Expected a non-empty value for `file_id` but received {file_id!r}") return await self._get( path_template("/v1/files/{file_id}", file_id=file_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=File, ) def list( self, *, after: str | Omit = omit, before: str | Omit = omit, file_types: str | Omit = omit, graph_id: str | Omit = omit, limit: int | Omit = omit, order: Literal["asc", "desc"] | Omit = omit, status: Literal["in_progress", "completed", "failed"] | 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, ) -> AsyncPaginator[File, AsyncCursorPage[File]]: """ Retrieve a paginated list of files with optional filtering by status, graph association, and file type. Args: after: The ID of the last object in the previous page. This parameter instructs the API to return the next page of results. before: The ID of the first object in the previous page. This parameter instructs the API to return the previous page of results. file_types: The extensions of the files to retrieve. Separate multiple extensions with a comma. For example: `pdf,jpg,docx`. graph_id: The unique identifier of the graph to which the files belong. limit: Specifies the maximum number of objects returned in a page. The default value is 50. The minimum value is 1, and the maximum value is 100. order: Specifies the order of the results. Valid values are asc for ascending and desc for descending. status: Specifies the status of the files to retrieve. Valid values are in_progress, completed or failed. 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 """ return self._get_api_list( "/v1/files", page=AsyncCursorPage[File], options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout, query=maybe_transform( { "after": after, "before": before, "file_types": file_types, "graph_id": graph_id, "limit": limit, "order": order, "status": status, }, file_list_params.FileListParams, ), ), model=File, ) async def delete( self, file_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, ) -> FileDeleteResponse: """Permanently delete a file from the system. This action cannot be undone. 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 file_id: raise ValueError(f"Expected a non-empty value for `file_id` but received {file_id!r}") return await self._delete( path_template("/v1/files/{file_id}", file_id=file_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=FileDeleteResponse, ) async def download( self, file_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, ) -> AsyncBinaryAPIResponse: """Download the binary content of a file. The response will contain the file data in the appropriate MIME type. 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 file_id: raise ValueError(f"Expected a non-empty value for `file_id` but received {file_id!r}") extra_headers = {"Accept": "application/octet-stream", **(extra_headers or {})} return await self._get( path_template("/v1/files/{file_id}/download", file_id=file_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=AsyncBinaryAPIResponse, ) async def retry( self, *, file_ids: SequenceNotStr[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, ) -> FileRetryResponse: """Retry processing of files that previously failed to process. This will re-attempt the processing of the specified files. Args: file_ids: The unique identifier of the files to retry. 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 """ return await self._post( "/v1/files/retry", body=await async_maybe_transform({"file_ids": file_ids}, file_retry_params.FileRetryParams), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), cast_to=FileRetryResponse, ) async def upload( self, content: FileTypes, *, content_disposition: str, content_type: str, graph_id: 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, ) -> File: """Upload a new file to the system. Supports various file formats including PDF, DOC, DOCX, PPT, PPTX, JPG, PNG, EML, HTML, SRT, CSV, XLS, and XLSX. File uploads do not accept team parameters. When you attach a file to a Knowledge Graph, the file inherits the team scope of that Knowledge Graph. Args: graph_id: The unique identifier of the Knowledge Graph to associate the uploaded file with. Note: The response from the upload endpoint does not include the `graphId` field, but the association will be visible when you retrieve the file using the file retrieval endpoint. 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 = { "Content-Disposition": content_disposition, "Content-Type": content_type, **(extra_headers or {}), } extra_headers["Content-Type"] = "text/plain" return await self._post( "/v1/files", options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout, query=await async_maybe_transform({"graph_id": graph_id}, file_upload_params.FileUploadParams), ), binary_request=content, cast_to=File, ) class FilesResourceWithRawResponse: def __init__(self, files: FilesResource) -> None: self._files = files self.retrieve = to_raw_response_wrapper( files.retrieve, ) self.list = to_raw_response_wrapper( files.list, ) self.delete = to_raw_response_wrapper( files.delete, ) self.download = to_custom_raw_response_wrapper( files.download, BinaryAPIResponse, ) self.retry = to_raw_response_wrapper( files.retry, ) self.upload = to_raw_response_wrapper( files.upload, ) class AsyncFilesResourceWithRawResponse: def __init__(self, files: AsyncFilesResource) -> None: self._files = files self.retrieve = async_to_raw_response_wrapper( files.retrieve, ) self.list = async_to_raw_response_wrapper( files.list, ) self.delete = async_to_raw_response_wrapper( files.delete, ) self.download = async_to_custom_raw_response_wrapper( files.download, AsyncBinaryAPIResponse, ) self.retry = async_to_raw_response_wrapper( files.retry, ) self.upload = async_to_raw_response_wrapper( files.upload, ) class FilesResourceWithStreamingResponse: def __init__(self, files: FilesResource) -> None: self._files = files self.retrieve = to_streamed_response_wrapper( files.retrieve, ) self.list = to_streamed_response_wrapper( files.list, ) self.delete = to_streamed_response_wrapper( files.delete, ) self.download = to_custom_streamed_response_wrapper( files.download, StreamedBinaryAPIResponse, ) self.retry = to_streamed_response_wrapper( files.retry, ) self.upload = to_streamed_response_wrapper( files.upload, ) class AsyncFilesResourceWithStreamingResponse: def __init__(self, files: AsyncFilesResource) -> None: self._files = files self.retrieve = async_to_streamed_response_wrapper( files.retrieve, ) self.list = async_to_streamed_response_wrapper( files.list, ) self.delete = async_to_streamed_response_wrapper( files.delete, ) self.download = async_to_custom_streamed_response_wrapper( files.download, AsyncStreamedBinaryAPIResponse, ) self.retry = async_to_streamed_response_wrapper( files.retry, ) self.upload = async_to_streamed_response_wrapper( files.upload, )