Skip to content

Commit

Permalink
feat: Implement info and exists file API methods (#318)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Smith <[email protected]>
  • Loading branch information
bogdanteleaga and silentworks authored Dec 24, 2024
1 parent 2699126 commit 0100ff1
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
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

0 comments on commit 0100ff1

Please sign in to comment.