Skip to content

Commit

Permalink
fix: upload and update method returns correct response body (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
silentworks authored Oct 25, 2024
1 parent faa914d commit a9e874a
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 203 deletions.
363 changes: 177 additions & 186 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ version = "0.8.2" # {x-release-please-version}
[tool.poetry.dependencies]
httpx = {version = ">=0.26,<0.28", extras = ["http2"]}
python = "^3.9"
typing-extensions = "^4.2.0"
python-dateutil = "^2.8.2"

[tool.poetry.dev-dependencies]
Expand Down
4 changes: 1 addition & 3 deletions storage3/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from __future__ import annotations

from typing import Union, overload

from typing_extensions import Literal
from typing import Literal, Union, overload

from storage3._async import AsyncStorageClient
from storage3._sync import SyncStorageClient
Expand Down
4 changes: 2 additions & 2 deletions storage3/_async/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async def list_buckets(self) -> list[AsyncBucket]:
"""Retrieves the details of all storage buckets within an existing product."""
# if the request doesn't error, it is assured to return a list
res = await self._request("GET", "/bucket")
return [AsyncBucket(**bucket, _client=self._client) for bucket in res.json()]
return [AsyncBucket(**bucket) for bucket in res.json()]

async def get_bucket(self, id: str) -> AsyncBucket:
"""Retrieves the details of an existing storage bucket.
Expand All @@ -49,7 +49,7 @@ async def get_bucket(self, id: str) -> AsyncBucket:
"""
res = await self._request("GET", f"/bucket/{id}")
json = res.json()
return AsyncBucket(**json, _client=self._client)
return AsyncBucket(**json)

async def create_bucket(
self,
Expand Down
12 changes: 9 additions & 3 deletions storage3/_async/file_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
ListBucketFilesOptions,
RequestMethod,
SignedUploadURL,
UploadData,
UploadResponse,
URLOptions,
)
from ..utils import AsyncClient, StorageException
Expand Down Expand Up @@ -353,7 +355,7 @@ async def _upload_or_update(
path: str,
file: Union[BufferedReader, bytes, FileIO, str, Path],
file_options: Optional[FileOptions] = None,
) -> Response:
) -> UploadResponse:
"""
Uploads a file to an existing bucket.
Expand Down Expand Up @@ -410,10 +412,14 @@ async def _upload_or_update(

_path = self._get_final_path(path)

return await self._request(
response = await self._request(
method, f"/object/{_path}", files=files, headers=headers, data=_data
)

data: UploadData = response.json()

return UploadResponse(path=path, Key=data.get("Key"))

async def upload(
self,
path: str,
Expand All @@ -440,7 +446,7 @@ async def update(
path: str,
file: Union[BufferedReader, bytes, FileIO, str, Path],
file_options: Optional[FileOptions] = None,
) -> Response:
) -> UploadResponse:
return await self._upload_or_update("PUT", path, file, file_options)

def _get_final_path(self, path: str) -> str:
Expand Down
4 changes: 2 additions & 2 deletions storage3/_sync/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def list_buckets(self) -> list[SyncBucket]:
"""Retrieves the details of all storage buckets within an existing product."""
# if the request doesn't error, it is assured to return a list
res = self._request("GET", "/bucket")
return [SyncBucket(**bucket, _client=self._client) for bucket in res.json()]
return [SyncBucket(**bucket) for bucket in res.json()]

def get_bucket(self, id: str) -> SyncBucket:
"""Retrieves the details of an existing storage bucket.
Expand All @@ -49,7 +49,7 @@ def get_bucket(self, id: str) -> SyncBucket:
"""
res = self._request("GET", f"/bucket/{id}")
json = res.json()
return SyncBucket(**json, _client=self._client)
return SyncBucket(**json)

def create_bucket(
self,
Expand Down
12 changes: 9 additions & 3 deletions storage3/_sync/file_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
ListBucketFilesOptions,
RequestMethod,
SignedUploadURL,
UploadData,
UploadResponse,
URLOptions,
)
from ..utils import StorageException, SyncClient
Expand Down Expand Up @@ -351,7 +353,7 @@ def _upload_or_update(
path: str,
file: Union[BufferedReader, bytes, FileIO, str, Path],
file_options: Optional[FileOptions] = None,
) -> Response:
) -> UploadResponse:
"""
Uploads a file to an existing bucket.
Expand Down Expand Up @@ -408,10 +410,14 @@ def _upload_or_update(

_path = self._get_final_path(path)

return self._request(
response = self._request(
method, f"/object/{_path}", files=files, headers=headers, data=_data
)

data: UploadData = response.json()

return UploadResponse(path=path, Key=data.get("Key"))

def upload(
self,
path: str,
Expand All @@ -438,7 +444,7 @@ def update(
path: str,
file: Union[BufferedReader, bytes, FileIO, str, Path],
file_options: Optional[FileOptions] = None,
) -> Response:
) -> UploadResponse:
return self._upload_or_update("PUT", path, file, file_options)

def _get_final_path(self, path: str) -> str:
Expand Down
22 changes: 19 additions & 3 deletions storage3/types.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import annotations

from dataclasses import dataclass
from dataclasses import asdict, dataclass
from datetime import datetime
from typing import Optional, Union
from typing import Literal, Optional, TypedDict, Union

import dateutil.parser
from typing_extensions import Literal, TypedDict

RequestMethod = Literal["GET", "POST", "DELETE", "PUT", "HEAD"]

Expand Down Expand Up @@ -80,3 +79,20 @@ class DownloadOptions(TypedDict, total=False):
{"cache-control": str, "content-type": str, "x-upsert": str, "upsert": str},
total=False,
)


class UploadData(TypedDict, total=False):
Id: str
Key: str


@dataclass
class UploadResponse:
path: str
full_path: str

def __init__(self, path, Key):
self.path = path
self.full_path = Key

dict = asdict

0 comments on commit a9e874a

Please sign in to comment.