unioslo / harborapi

Python async client for the Harbor REST API v2.0.
https://unioslo.github.io/harborapi/
MIT License
28 stars 6 forks source link

Return file metadata along with file contents from `HarborAsyncClient.get_file` #35

Closed pederhan closed 1 year ago

pederhan commented 1 year ago

In addition to the response body, we should probably return the headers from the response, so clients know how to process the data if they want to.

https://github.com/pederhan/harborapi/blob/1b1a25089b6040dca1057fa1fcd57f02bfc4e8dd/harborapi/client.py#L4516-L4520

We also provide no exception handling for failed reads here...

Suggestions

Adding some sort of NamedTuple that contains the response content along with its headers, encoding, etc.

class FileResponse(NamedTuple):
    content: bytes
    encoding: Optional[str] # easier access than through header
    content_type: Optional[str] # easier access than through header
    headers: Dict[str, Any]

    def __bytes__(self) -> bytes:
        return self.contents

This will break the API of methods currently using get_file(). However, with the __bytes__ method implemented on the class, passing the tuple to bytes() will return only the file contents. Thus existing invocations will either have to explicitly access resp.content or do bytes(resp).

pederhan commented 1 year ago

Added in d4815cc