Skip to content

Commit

Permalink
python: correct return type for raw stream in ObjectReader
Browse files Browse the repository at this point in the history
Changed the return type of the raw() method in ObjectReader to return the correct file-like object instead of bytes. Improved docstrings and added type annotations for clarity.

Signed-off-by: Abhishek Gaikwad <[email protected]>
  • Loading branch information
gaikwadabhishek committed Jun 6, 2024
1 parent 74a8a5b commit 198935a
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions python/aistore/sdk/object_reader.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from typing import Iterator

import requests
from requests.structures import CaseInsensitiveDict

from aistore.sdk.const import DEFAULT_CHUNK_SIZE
from aistore.sdk.object_attributes import ObjectAttributes


class ObjectReader:
"""
Represents the data returned by the API when getting an object, including access to the content stream and object
attributes
attributes.
"""

def __init__(
Expand All @@ -26,37 +24,42 @@ def __init__(
@property
def attributes(self) -> ObjectAttributes:
"""
Object metadata attributes
Object metadata attributes.
Returns:
Object attributes parsed from the headers returned by AIS
ObjectAttributes: Parsed object attributes from the headers returned by AIS
"""
return self._attributes

def read_all(self) -> bytes:
"""
Read all byte data from the object content stream.
This uses a bytes cast which makes it slightly slower and requires all object content to fit in memory at once
Returns:
Object content as bytes
This uses a bytes cast which makes it slightly slower and requires all object content to fit in memory at once.
Returns:
bytes: Object content as bytes.
"""
obj_arr = bytearray()
for chunk in self:
obj_arr.extend(chunk)
return bytes(obj_arr)

def raw(self) -> bytes:
def raw(self) -> requests.Response:
"""
Returns: Raw byte stream of object content
Returns the raw byte stream of object content.
Returns:
requests.Response: Raw byte stream of the object content
"""
return self._stream.raw

def __iter__(self) -> Iterator[bytes]:
"""
Creates a generator to read the stream content in chunks
Creates a generator to read the stream content in chunks.
Returns:
An iterator with access to the next chunk of bytes
Iterator[bytes]: An iterator to access the next chunk of bytes
"""
try:
for chunk in self._stream.iter_content(chunk_size=self._chunk_size):
Expand Down

0 comments on commit 198935a

Please sign in to comment.