Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,13 @@ all_content = result.get_content()
all_content = await result.aget_content()
```

or access response metadata:

```python
headers = result.headers
content_type = result.content_type
```

or write it to the file:

```python
Expand Down
10 changes: 9 additions & 1 deletion aidial_client/types/file.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Union
from typing import Optional, Union

import aiofiles
import httpx
Expand Down Expand Up @@ -43,3 +43,11 @@ async def aget_content(self) -> bytes:
@property
def filename(self) -> str:
return self._filename

@property
def headers(self) -> httpx.Headers:
return self._response.headers

@property
def content_type(self) -> Optional[str]:
return self.headers.get("content-type")
26 changes: 26 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import httpx
import pytest
from pydantic import ValidationError

from aidial_client.types.chat.response import Attachment
from aidial_client.types.file import FileDownloadResponse
from aidial_client.types.metadata import BaseMetadata


Expand Down Expand Up @@ -54,3 +56,27 @@ def test_metadata_population():
assert getattr(metadata_by_name, field) == getattr(
metadata_by_alias, field
)


def test_file_download_response_metadata():
response = httpx.Response(
200,
content=b"test content",
headers={"content-type": "text/plain", "x-test-header": "test"},
)

download_response = FileDownloadResponse(
response=response, filename="test.txt"
)

assert download_response.headers["x-test-header"] == "test"
assert download_response.content_type == "text/plain"


def test_file_download_response_metadata_without_content_type():
response = httpx.Response(200, content=b"test content")
download_response = FileDownloadResponse(
response=response, filename="test.txt"
)

assert download_response.content_type is None
Loading