Skip to content

Commit

Permalink
fix: return the generated sign url
Browse files Browse the repository at this point in the history
  • Loading branch information
raksiv authored and tjholm committed Nov 7, 2022
1 parent e49c9f9 commit ffcc097
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
12 changes: 8 additions & 4 deletions nitric/api/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,17 @@ async def write(self, body: bytes):
"""
try:
await self._storage._storage_stub.write(
storage_write_request=StorageWriteRequest(bucket_name=self._bucket, key=self.key, body=body))
storage_write_request=StorageWriteRequest(bucket_name=self._bucket, key=self.key, body=body)
)
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)

async def read(self) -> bytes:
"""Read this files contents from the bucket."""
try:
response = await self._storage._storage_stub.read(
storage_read_request=StorageReadRequest(bucket_name=self._bucket, key=self.key))
storage_read_request=StorageReadRequest(bucket_name=self._bucket, key=self.key)
)
return response.body
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)
Expand All @@ -115,7 +117,8 @@ async def delete(self):
"""Delete this file from the bucket."""
try:
await self._storage._storage_stub.delete(
storage_delete_request=StorageDeleteRequest(bucket_name=self._bucket, key=self.key))
storage_delete_request=StorageDeleteRequest(bucket_name=self._bucket, key=self.key)
)
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)

Expand All @@ -128,10 +131,11 @@ async def download_url(self, expiry: int = 600):
async def sign_url(self, mode: FileMode = FileMode.READ, expiry: int = 3600):
"""Generate a signed URL for reading or writing to a file."""
try:
await self._storage._storage_stub.pre_sign_url(
response = await self._storage._storage_stub.pre_sign_url(
storage_pre_sign_url_request=StoragePreSignUrlRequest(
bucket_name=self._bucket, key=self.key, operation=mode.to_request_operation(), expiry=expiry
)
)
return response.url
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)
54 changes: 32 additions & 22 deletions tests/api/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@

import pytest
from grpclib import GRPCError, Status
from nitricapi.nitric.storage.v1 import StorageWriteRequest, StorageReadRequest, StorageDeleteRequest, \
StoragePreSignUrlRequest, StoragePreSignUrlRequestOperation
from nitricapi.nitric.storage.v1 import (
StorageWriteRequest,
StorageReadRequest,
StorageDeleteRequest,
StoragePreSignUrlRequest,
StoragePreSignUrlRequestOperation,
StoragePreSignUrlResponse,
)

from nitric.api import Storage
from nitric.api.exception import UnknownException
Expand All @@ -46,11 +52,9 @@ async def test_write(self):
await file.write(contents)

# Check expected values were passed to Stub
mock_write.assert_called_once_with(storage_write_request=StorageWriteRequest(
bucket_name="test-bucket",
key="test-file",
body=contents
))
mock_write.assert_called_once_with(
storage_write_request=StorageWriteRequest(bucket_name="test-bucket", key="test-file", body=contents)
)

async def test_read(self):
contents = b"some text as bytes"
Expand All @@ -68,10 +72,12 @@ async def test_read(self):
assert response == contents

# Check expected values were passed to Stub
mock_read.assert_called_once_with(storage_read_request=StorageReadRequest(
bucket_name="test-bucket",
key="test-file",
))
mock_read.assert_called_once_with(
storage_read_request=StorageReadRequest(
bucket_name="test-bucket",
key="test-file",
)
)

async def test_delete(self):
mock_delete = AsyncMock()
Expand All @@ -83,27 +89,31 @@ async def test_delete(self):
await file.delete()

# Check expected values were passed to Stub
mock_delete.assert_called_once_with(storage_delete_request=StorageDeleteRequest(
bucket_name="test-bucket",
key="test-file",
))
mock_delete.assert_called_once_with(
storage_delete_request=StorageDeleteRequest(
bucket_name="test-bucket",
key="test-file",
)
)

async def test_sign_url(self):
mock_pre_sign_url = AsyncMock()
mock_pre_sign_url.return_value = Object()
mock_pre_sign_url.return_value = StoragePreSignUrlResponse(url="www.example.com")

with patch("nitricapi.nitric.storage.v1.StorageServiceStub.pre_sign_url", mock_pre_sign_url):
bucket = Storage().bucket("test-bucket")
file = bucket.file("test-file")
await file.sign_url()

# Check expected values were passed to Stub
mock_pre_sign_url.assert_called_once_with(storage_pre_sign_url_request=StoragePreSignUrlRequest(
bucket_name="test-bucket",
key="test-file",
operation=StoragePreSignUrlRequestOperation.READ,
expiry=3600
))
mock_pre_sign_url.assert_called_once_with(
storage_pre_sign_url_request=StoragePreSignUrlRequest(
bucket_name="test-bucket",
key="test-file",
operation=StoragePreSignUrlRequestOperation.READ,
expiry=3600,
)
)

async def test_write_error(self):
mock_write = AsyncMock()
Expand Down

0 comments on commit ffcc097

Please sign in to comment.