diff --git a/.stats.yml b/.stats.yml index b2a2ac22..468de8f6 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1,2 +1,2 @@ -configured_endpoints: 12 +configured_endpoints: 15 openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/writerai%2Fwriter-2aa0ec0ea99873da5dac6ecf610efdd6ccfe5601d2400f8c475d994cd47c2c36.yml diff --git a/api.md b/api.md index cf33548b..abecbd8a 100644 --- a/api.md +++ b/api.md @@ -63,10 +63,13 @@ Methods: Types: ```python -from writerai.types import File +from writerai.types import File, FileDeleteResponse ``` Methods: +- client.files.retrieve(file_id) -> File - client.files.list(\*\*params) -> SyncCursorPage[File] +- client.files.delete(file_id) -> FileDeleteResponse +- client.files.download(file_id) -> BinaryAPIResponse - client.files.upload(\*\*params) -> File diff --git a/src/writerai/resources/files.py b/src/writerai/resources/files.py index c1e7e78a..11df91bc 100644 --- a/src/writerai/resources/files.py +++ b/src/writerai/resources/files.py @@ -15,14 +15,23 @@ 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_delete_response import FileDeleteResponse __all__ = ["FilesResource", "AsyncFilesResource"] @@ -36,6 +45,39 @@ def with_raw_response(self) -> FilesResourceWithRawResponse: def with_streaming_response(self) -> FilesResourceWithStreamingResponse: 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 file + + 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( + f"/v1/files/{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, *, @@ -100,6 +142,73 @@ def list( 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: + """ + Delete file + + 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( + f"/v1/files/{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 file + + 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": "file contents", **(extra_headers or {})} + return self._get( + f"/v1/files/{file_id}/download", + options=make_request_options( + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout + ), + cast_to=BinaryAPIResponse, + ) + def upload( self, *, @@ -151,6 +260,39 @@ def with_raw_response(self) -> AsyncFilesResourceWithRawResponse: def with_streaming_response(self) -> AsyncFilesResourceWithStreamingResponse: 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 file + + 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( + f"/v1/files/{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, *, @@ -215,6 +357,73 @@ def list( 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: + """ + Delete file + + 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( + f"/v1/files/{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 file + + 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": "file contents", **(extra_headers or {})} + return await self._get( + f"/v1/files/{file_id}/download", + options=make_request_options( + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout + ), + cast_to=AsyncBinaryAPIResponse, + ) + async def upload( self, *, @@ -261,9 +470,19 @@ 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.upload = to_raw_response_wrapper( files.upload, ) @@ -273,9 +492,19 @@ 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.upload = async_to_raw_response_wrapper( files.upload, ) @@ -285,9 +514,19 @@ 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.upload = to_streamed_response_wrapper( files.upload, ) @@ -297,9 +536,19 @@ 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.upload = async_to_streamed_response_wrapper( files.upload, ) diff --git a/src/writerai/types/__init__.py b/src/writerai/types/__init__.py index b2e31c1b..0e549136 100644 --- a/src/writerai/types/__init__.py +++ b/src/writerai/types/__init__.py @@ -15,6 +15,7 @@ from .graph_create_params import GraphCreateParams as GraphCreateParams from .graph_update_params import GraphUpdateParams as GraphUpdateParams from .model_list_response import ModelListResponse as ModelListResponse +from .file_delete_response import FileDeleteResponse as FileDeleteResponse from .graph_create_response import GraphCreateResponse as GraphCreateResponse from .graph_delete_response import GraphDeleteResponse as GraphDeleteResponse from .graph_update_response import GraphUpdateResponse as GraphUpdateResponse diff --git a/src/writerai/types/file_delete_response.py b/src/writerai/types/file_delete_response.py new file mode 100644 index 00000000..3ab627d5 --- /dev/null +++ b/src/writerai/types/file_delete_response.py @@ -0,0 +1,15 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + + + +from .._models import BaseModel + +__all__ = ["FileDeleteResponse"] + + +class FileDeleteResponse(BaseModel): + id: str + """A unique identifier of the deleted graph.""" + + deleted: bool + """Indicates whether the graph was successfully deleted.""" diff --git a/tests/api_resources/test_files.py b/tests/api_resources/test_files.py index 925cde45..af5d40a1 100644 --- a/tests/api_resources/test_files.py +++ b/tests/api_resources/test_files.py @@ -5,11 +5,19 @@ import os from typing import Any, cast +import httpx import pytest +from respx import MockRouter from writerai import Writer, AsyncWriter from tests.utils import assert_matches_type -from writerai.types import File +from writerai.types import File, FileDeleteResponse +from writerai._response import ( + BinaryAPIResponse, + AsyncBinaryAPIResponse, + StreamedBinaryAPIResponse, + AsyncStreamedBinaryAPIResponse, +) from writerai.pagination import SyncCursorPage, AsyncCursorPage base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") @@ -18,6 +26,44 @@ class TestFiles: parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) + @parametrize + def test_method_retrieve(self, client: Writer) -> None: + file = client.files.retrieve( + "file_id", + ) + assert_matches_type(File, file, path=["response"]) + + @parametrize + def test_raw_response_retrieve(self, client: Writer) -> None: + response = client.files.with_raw_response.retrieve( + "file_id", + ) + + assert response.is_closed is True + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + file = response.parse() + assert_matches_type(File, file, path=["response"]) + + @parametrize + def test_streaming_response_retrieve(self, client: Writer) -> None: + with client.files.with_streaming_response.retrieve( + "file_id", + ) as response: + assert not response.is_closed + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + + file = response.parse() + assert_matches_type(File, file, path=["response"]) + + assert cast(Any, response.is_closed) is True + + @parametrize + def test_path_params_retrieve(self, client: Writer) -> None: + with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): + client.files.with_raw_response.retrieve( + "", + ) + @parametrize def test_method_list(self, client: Writer) -> None: file = client.files.list() @@ -54,6 +100,98 @@ def test_streaming_response_list(self, client: Writer) -> None: assert cast(Any, response.is_closed) is True + @parametrize + def test_method_delete(self, client: Writer) -> None: + file = client.files.delete( + "file_id", + ) + assert_matches_type(FileDeleteResponse, file, path=["response"]) + + @parametrize + def test_raw_response_delete(self, client: Writer) -> None: + response = client.files.with_raw_response.delete( + "file_id", + ) + + assert response.is_closed is True + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + file = response.parse() + assert_matches_type(FileDeleteResponse, file, path=["response"]) + + @parametrize + def test_streaming_response_delete(self, client: Writer) -> None: + with client.files.with_streaming_response.delete( + "file_id", + ) as response: + assert not response.is_closed + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + + file = response.parse() + assert_matches_type(FileDeleteResponse, file, path=["response"]) + + assert cast(Any, response.is_closed) is True + + @parametrize + def test_path_params_delete(self, client: Writer) -> None: + with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): + client.files.with_raw_response.delete( + "", + ) + + @pytest.mark.skip(reason="requests with binary data not yet supported in test environment") + @parametrize + @pytest.mark.respx(base_url=base_url) + def test_method_download(self, client: Writer, respx_mock: MockRouter) -> None: + respx_mock.get("/v1/files/file_id/download").mock(return_value=httpx.Response(200, json={"foo": "bar"})) + file = client.files.download( + "file_id", + ) + assert file.is_closed + assert file.json() == {"foo": "bar"} + assert cast(Any, file.is_closed) is True + assert isinstance(file, BinaryAPIResponse) + + @pytest.mark.skip(reason="requests with binary data not yet supported in test environment") + @parametrize + @pytest.mark.respx(base_url=base_url) + def test_raw_response_download(self, client: Writer, respx_mock: MockRouter) -> None: + respx_mock.get("/v1/files/file_id/download").mock(return_value=httpx.Response(200, json={"foo": "bar"})) + + file = client.files.with_raw_response.download( + "file_id", + ) + + assert file.is_closed is True + assert file.http_request.headers.get("X-Stainless-Lang") == "python" + assert file.json() == {"foo": "bar"} + assert isinstance(file, BinaryAPIResponse) + + @pytest.mark.skip(reason="requests with binary data not yet supported in test environment") + @parametrize + @pytest.mark.respx(base_url=base_url) + def test_streaming_response_download(self, client: Writer, respx_mock: MockRouter) -> None: + respx_mock.get("/v1/files/file_id/download").mock(return_value=httpx.Response(200, json={"foo": "bar"})) + with client.files.with_streaming_response.download( + "file_id", + ) as file: + assert not file.is_closed + assert file.http_request.headers.get("X-Stainless-Lang") == "python" + + assert file.json() == {"foo": "bar"} + assert cast(Any, file.is_closed) is True + assert isinstance(file, StreamedBinaryAPIResponse) + + assert cast(Any, file.is_closed) is True + + @pytest.mark.skip(reason="requests with binary data not yet supported in test environment") + @parametrize + @pytest.mark.respx(base_url=base_url) + def test_path_params_download(self, client: Writer) -> None: + with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): + client.files.with_raw_response.download( + "", + ) + @pytest.mark.skip(reason="requests with binary data not yet supported in test environment") @parametrize def test_method_upload(self, client: Writer) -> None: @@ -101,6 +239,44 @@ def test_streaming_response_upload(self, client: Writer) -> None: class TestAsyncFiles: parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) + @parametrize + async def test_method_retrieve(self, async_client: AsyncWriter) -> None: + file = await async_client.files.retrieve( + "file_id", + ) + assert_matches_type(File, file, path=["response"]) + + @parametrize + async def test_raw_response_retrieve(self, async_client: AsyncWriter) -> None: + response = await async_client.files.with_raw_response.retrieve( + "file_id", + ) + + assert response.is_closed is True + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + file = await response.parse() + assert_matches_type(File, file, path=["response"]) + + @parametrize + async def test_streaming_response_retrieve(self, async_client: AsyncWriter) -> None: + async with async_client.files.with_streaming_response.retrieve( + "file_id", + ) as response: + assert not response.is_closed + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + + file = await response.parse() + assert_matches_type(File, file, path=["response"]) + + assert cast(Any, response.is_closed) is True + + @parametrize + async def test_path_params_retrieve(self, async_client: AsyncWriter) -> None: + with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): + await async_client.files.with_raw_response.retrieve( + "", + ) + @parametrize async def test_method_list(self, async_client: AsyncWriter) -> None: file = await async_client.files.list() @@ -137,6 +313,98 @@ async def test_streaming_response_list(self, async_client: AsyncWriter) -> None: assert cast(Any, response.is_closed) is True + @parametrize + async def test_method_delete(self, async_client: AsyncWriter) -> None: + file = await async_client.files.delete( + "file_id", + ) + assert_matches_type(FileDeleteResponse, file, path=["response"]) + + @parametrize + async def test_raw_response_delete(self, async_client: AsyncWriter) -> None: + response = await async_client.files.with_raw_response.delete( + "file_id", + ) + + assert response.is_closed is True + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + file = await response.parse() + assert_matches_type(FileDeleteResponse, file, path=["response"]) + + @parametrize + async def test_streaming_response_delete(self, async_client: AsyncWriter) -> None: + async with async_client.files.with_streaming_response.delete( + "file_id", + ) as response: + assert not response.is_closed + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + + file = await response.parse() + assert_matches_type(FileDeleteResponse, file, path=["response"]) + + assert cast(Any, response.is_closed) is True + + @parametrize + async def test_path_params_delete(self, async_client: AsyncWriter) -> None: + with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): + await async_client.files.with_raw_response.delete( + "", + ) + + @pytest.mark.skip(reason="requests with binary data not yet supported in test environment") + @parametrize + @pytest.mark.respx(base_url=base_url) + async def test_method_download(self, async_client: AsyncWriter, respx_mock: MockRouter) -> None: + respx_mock.get("/v1/files/file_id/download").mock(return_value=httpx.Response(200, json={"foo": "bar"})) + file = await async_client.files.download( + "file_id", + ) + assert file.is_closed + assert await file.json() == {"foo": "bar"} + assert cast(Any, file.is_closed) is True + assert isinstance(file, AsyncBinaryAPIResponse) + + @pytest.mark.skip(reason="requests with binary data not yet supported in test environment") + @parametrize + @pytest.mark.respx(base_url=base_url) + async def test_raw_response_download(self, async_client: AsyncWriter, respx_mock: MockRouter) -> None: + respx_mock.get("/v1/files/file_id/download").mock(return_value=httpx.Response(200, json={"foo": "bar"})) + + file = await async_client.files.with_raw_response.download( + "file_id", + ) + + assert file.is_closed is True + assert file.http_request.headers.get("X-Stainless-Lang") == "python" + assert await file.json() == {"foo": "bar"} + assert isinstance(file, AsyncBinaryAPIResponse) + + @pytest.mark.skip(reason="requests with binary data not yet supported in test environment") + @parametrize + @pytest.mark.respx(base_url=base_url) + async def test_streaming_response_download(self, async_client: AsyncWriter, respx_mock: MockRouter) -> None: + respx_mock.get("/v1/files/file_id/download").mock(return_value=httpx.Response(200, json={"foo": "bar"})) + async with async_client.files.with_streaming_response.download( + "file_id", + ) as file: + assert not file.is_closed + assert file.http_request.headers.get("X-Stainless-Lang") == "python" + + assert await file.json() == {"foo": "bar"} + assert cast(Any, file.is_closed) is True + assert isinstance(file, AsyncStreamedBinaryAPIResponse) + + assert cast(Any, file.is_closed) is True + + @pytest.mark.skip(reason="requests with binary data not yet supported in test environment") + @parametrize + @pytest.mark.respx(base_url=base_url) + async def test_path_params_download(self, async_client: AsyncWriter) -> None: + with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): + await async_client.files.with_raw_response.download( + "", + ) + @pytest.mark.skip(reason="requests with binary data not yet supported in test environment") @parametrize async def test_method_upload(self, async_client: AsyncWriter) -> None: