Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement info and exists file API methods #318

Merged
merged 3 commits into from
Dec 24, 2024
Merged
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
36 changes: 36 additions & 0 deletions storage3/_async/file_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,42 @@ async def remove(self, paths: list) -> list[dict[str, Any]]:
)
return response.json()

async def info(
self,
path: str,
) -> list[dict[str, str]]:
"""
Lists info for a particular file.

Parameters
----------
path
The path to the file.
"""
response = await self._request(
"GET",
f"/object/info/{self.id}/{path}",
)
return response.json()

async def exists(
self,
path: str,
) -> bool:
"""
Returns True if the file exists, False otherwise.

Parameters
----------
path
The path to the file.
"""
response = await self._request(
"HEAD",
f"/object/info/{self.id}/{path}",
)
return response.status_code == 200

async def list(
self,
path: Optional[str] = None,
Expand Down
36 changes: 36 additions & 0 deletions storage3/_sync/file_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,42 @@ def remove(self, paths: list) -> list[dict[str, Any]]:
)
return response.json()

def info(
self,
path: str,
) -> list[dict[str, str]]:
"""
Lists info for a particular file.

Parameters
----------
path
The path to the file.
"""
response = self._request(
"GET",
f"/object/info/{self.id}/{path}",
)
return response.json()

def exists(
self,
path: str,
) -> bool:
"""
Returns True if the file exists, False otherwise.

Parameters
----------
path
The path to the file.
"""
response = self._request(
"HEAD",
f"/object/info/{self.id}/{path}",
)
return response.status_code == 200

def list(
self,
path: Optional[str] = None,
Expand Down
27 changes: 27 additions & 0 deletions tests/_async/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,30 @@ async def test_client_get_public_url(
response.raise_for_status()

assert response.content == file.file_content


async def test_client_info(
storage_file_client_public: SyncBucketProxy, file: FileForTesting
) -> None:
"""Ensure we can get the public url of a file in a bucket"""
await storage_file_client_public.upload(
file.bucket_path, file.local_path, {"content-type": file.mime_type}
)

info = await storage_file_client_public.info(file.bucket_path)
assert "metadata" in info.keys()
assert info["name"] == file.bucket_path
assert info["content_type"] == file.mime_type


async def test_client_exists(
storage_file_client_public: SyncBucketProxy, file: FileForTesting
) -> None:
"""Ensure we can get the public url of a file in a bucket"""
await storage_file_client_public.upload(
file.bucket_path, file.local_path, {"content-type": file.mime_type}
)

exists = await storage_file_client_public.exists(file.bucket_path)

assert exists
27 changes: 27 additions & 0 deletions tests/_sync/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,30 @@ def test_client_get_public_url(
response.raise_for_status()

assert response.content == file.file_content


def test_client_info(
storage_file_client_public: SyncBucketProxy, file: FileForTesting
) -> None:
"""Ensure we can get the public url of a file in a bucket"""
storage_file_client_public.upload(
file.bucket_path, file.local_path, {"content-type": file.mime_type}
)

info = storage_file_client_public.info(file.bucket_path)
assert "metadata" in info.keys()
assert info["name"] == file.bucket_path
assert info["content_type"] == file.mime_type


def test_client_exists(
storage_file_client_public: SyncBucketProxy, file: FileForTesting
) -> None:
"""Ensure we can get the public url of a file in a bucket"""
storage_file_client_public.upload(
file.bucket_path, file.local_path, {"content-type": file.mime_type}
)

exists = storage_file_client_public.exists(file.bucket_path)

assert exists
Loading