1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2
  3from __future__ import annotations
  4
  5import os
  6from typing import Any, cast
  7
  8import httpx
  9import pytest
 10from respx import MockRouter
 11
 12import openai._legacy_response as _legacy_response
 13from openai import OpenAI, AsyncOpenAI
 14from tests.utils import assert_matches_type
 15
 16# pyright: reportDeprecated=false
 17
 18base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
 19
 20
 21class TestContent:
 22    parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
 23
 24    @parametrize
 25    @pytest.mark.respx(base_url=base_url)
 26    def test_method_retrieve(self, client: OpenAI, respx_mock: MockRouter) -> None:
 27        respx_mock.get("/containers/container_id/files/file_id/content").mock(
 28            return_value=httpx.Response(200, json={"foo": "bar"})
 29        )
 30        content = client.containers.files.content.retrieve(
 31            file_id="file_id",
 32            container_id="container_id",
 33        )
 34        assert isinstance(content, _legacy_response.HttpxBinaryResponseContent)
 35        assert content.json() == {"foo": "bar"}
 36
 37    @parametrize
 38    @pytest.mark.respx(base_url=base_url)
 39    def test_raw_response_retrieve(self, client: OpenAI, respx_mock: MockRouter) -> None:
 40        respx_mock.get("/containers/container_id/files/file_id/content").mock(
 41            return_value=httpx.Response(200, json={"foo": "bar"})
 42        )
 43
 44        response = client.containers.files.content.with_raw_response.retrieve(
 45            file_id="file_id",
 46            container_id="container_id",
 47        )
 48
 49        assert response.is_closed is True
 50        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
 51        content = response.parse()
 52        assert_matches_type(_legacy_response.HttpxBinaryResponseContent, content, path=["response"])
 53
 54    @parametrize
 55    @pytest.mark.respx(base_url=base_url)
 56    def test_streaming_response_retrieve(self, client: OpenAI, respx_mock: MockRouter) -> None:
 57        respx_mock.get("/containers/container_id/files/file_id/content").mock(
 58            return_value=httpx.Response(200, json={"foo": "bar"})
 59        )
 60        with client.containers.files.content.with_streaming_response.retrieve(
 61            file_id="file_id",
 62            container_id="container_id",
 63        ) as response:
 64            assert not response.is_closed
 65            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
 66
 67            content = response.parse()
 68            assert_matches_type(bytes, content, path=["response"])
 69
 70        assert cast(Any, response.is_closed) is True
 71
 72    @parametrize
 73    @pytest.mark.respx(base_url=base_url)
 74    def test_path_params_retrieve(self, client: OpenAI) -> None:
 75        with pytest.raises(ValueError, match=r"Expected a non-empty value for `container_id` but received ''"):
 76            client.containers.files.content.with_raw_response.retrieve(
 77                file_id="file_id",
 78                container_id="",
 79            )
 80
 81        with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
 82            client.containers.files.content.with_raw_response.retrieve(
 83                file_id="",
 84                container_id="container_id",
 85            )
 86
 87
 88class TestAsyncContent:
 89    parametrize = pytest.mark.parametrize(
 90        "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
 91    )
 92
 93    @parametrize
 94    @pytest.mark.respx(base_url=base_url)
 95    async def test_method_retrieve(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
 96        respx_mock.get("/containers/container_id/files/file_id/content").mock(
 97            return_value=httpx.Response(200, json={"foo": "bar"})
 98        )
 99        content = await async_client.containers.files.content.retrieve(
100            file_id="file_id",
101            container_id="container_id",
102        )
103        assert isinstance(content, _legacy_response.HttpxBinaryResponseContent)
104        assert content.json() == {"foo": "bar"}
105
106    @parametrize
107    @pytest.mark.respx(base_url=base_url)
108    async def test_raw_response_retrieve(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
109        respx_mock.get("/containers/container_id/files/file_id/content").mock(
110            return_value=httpx.Response(200, json={"foo": "bar"})
111        )
112
113        response = await async_client.containers.files.content.with_raw_response.retrieve(
114            file_id="file_id",
115            container_id="container_id",
116        )
117
118        assert response.is_closed is True
119        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
120        content = response.parse()
121        assert_matches_type(_legacy_response.HttpxBinaryResponseContent, content, path=["response"])
122
123    @parametrize
124    @pytest.mark.respx(base_url=base_url)
125    async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
126        respx_mock.get("/containers/container_id/files/file_id/content").mock(
127            return_value=httpx.Response(200, json={"foo": "bar"})
128        )
129        async with async_client.containers.files.content.with_streaming_response.retrieve(
130            file_id="file_id",
131            container_id="container_id",
132        ) as response:
133            assert not response.is_closed
134            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
135
136            content = await response.parse()
137            assert_matches_type(bytes, content, path=["response"])
138
139        assert cast(Any, response.is_closed) is True
140
141    @parametrize
142    @pytest.mark.respx(base_url=base_url)
143    async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
144        with pytest.raises(ValueError, match=r"Expected a non-empty value for `container_id` but received ''"):
145            await async_client.containers.files.content.with_raw_response.retrieve(
146                file_id="file_id",
147                container_id="",
148            )
149
150        with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
151            await async_client.containers.files.content.with_raw_response.retrieve(
152                file_id="",
153                container_id="container_id",
154            )