Skip to content

Commit

Permalink
feat: SDK improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm committed Nov 2, 2022
1 parent b60b6a2 commit 224a2e9
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 51 deletions.
3 changes: 1 addition & 2 deletions nitric/api/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ async def publish(
event = Event()

if isinstance(event, dict):
# TODO: handle events that are just a payload
event = Event(**event)
event = Event(payload=event)

try:
response = await self._events._stub.publish(
Expand Down
18 changes: 11 additions & 7 deletions nitric/api/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
from typing import List

from grpclib import GRPCError
from nitric.api.storage import File

from nitric.api.exception import exception_from_grpc_error, InvalidArgumentException
from nitric.utils import new_default_channel
from nitricapi.nitric.storage.v1 import StorageServiceStub, StoragePreSignUrlRequestOperation, StorageWriteRequest, \
Expand Down Expand Up @@ -62,10 +60,12 @@ def file(self, key: str):
"""Return a reference to a file in this bucket."""
return File(_storage=self._storage, _bucket=self.name, key=key)

async def files(self) -> List[File]:
resp = await self._storage._storage_stub.list_files(storage_list_files_request=StorageListFilesRequest(bucket_name=self.name))
async def files(self):
resp = await self._storage._storage_stub.list_files(
storage_list_files_request=StorageListFilesRequest(bucket_name=self.name))
return [self.file(f.key) for f in resp.files]


class FileMode(Enum):
"""Definition of available operation modes for file signed URLs."""

Expand All @@ -81,6 +81,7 @@ def to_request_operation(self) -> StoragePreSignUrlRequestOperation:
else:
raise InvalidArgumentException("Invalid FileMode")


@dataclass(frozen=True, order=True)
class File(object):
"""A reference to a file in a bucket, used to perform operations on that file."""
Expand All @@ -96,22 +97,25 @@ async def write(self, body: bytes):
Will create the file if it doesn't already exist.
"""
try:
await self._storage._storage_stub.write(storage_write_request=StorageWriteRequest(bucket_name=self._bucket, key=self.key, body=body))
await self._storage._storage_stub.write(
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))
response = await self._storage._storage_stub.read(
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)

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))
await self._storage._storage_stub.delete(
storage_delete_request=StorageDeleteRequest(bucket_name=self._bucket, key=self.key))
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)

Expand Down
10 changes: 5 additions & 5 deletions nitric/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ def _to_resource(self) -> Resource:
pass

@abstractmethod
def _perms_to_actions(self, permissions: List[str]) -> List[Action]:
def _perms_to_actions(self, *args: str) -> List[Action]:
pass

async def _register_policy_async(self, permissions: List[str]):
async def _register_policy_async(self, *args: str):
# if self._reg is not None:
# await asyncio.wait({self._reg})

policy = PolicyResource(
principals=[Resource(type=ResourceType.Function)],
actions=self._perms_to_actions(permissions),
actions=self._perms_to_actions(*args),
resources=[self._to_resource()],
)
try:
Expand All @@ -95,7 +95,7 @@ async def _register_policy_async(self, permissions: List[str]):
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)

def _register_policy(self, permissions: List[str]):
def _register_policy(self, *args: str):
loop = asyncio.get_event_loop()
loop.run_until_complete(self._register_policy_async(permissions))
loop.run_until_complete(self._register_policy_async(*args))

8 changes: 4 additions & 4 deletions nitric/resources/buckets.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async def _register(self):
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)

def _perms_to_actions(self, permissions: List[Union[BucketPermission, str]]) -> List[Action]:
def _perms_to_actions(self, *args: [Union[BucketPermission, str]]) -> List[Action]:
permission_actions_map = {
BucketPermission.reading: [Action.BucketFileGet, Action.BucketFileList],
BucketPermission.writing: [Action.BucketFilePut],
Expand All @@ -69,16 +69,16 @@ def _perms_to_actions(self, permissions: List[Union[BucketPermission, str]]) ->
# convert strings to the enum value where needed
perms = [
permission if isinstance(permission, BucketPermission) else BucketPermission[permission.lower()]
for permission in permissions
for permission in args
]
return [action for perm in perms for action in permission_actions_map[perm]]

def _to_resource(self) -> Resource:
return Resource(name=self.name, type=ResourceType.Bucket)

def allow(self, permissions: List[Union[BucketPermission, str]]) -> BucketRef:
def allow(self, *args: Union[BucketPermission, str]) -> BucketRef:
"""Request the required permissions for this resource."""
self._register_policy(permissions)
self._register_policy(*args)

return Storage().bucket(self.name)

Expand Down
8 changes: 4 additions & 4 deletions nitric/resources/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async def _register(self):
def _to_resource(self) -> Resource:
return Resource(name=self.name, type=ResourceType.Collection)

def _perms_to_actions(self, permissions: List[Union[CollectionPermission, str]]) -> List[Action]:
def _perms_to_actions(self, *args: Union[CollectionPermission, str]) -> List[Action]:
permission_actions_map = {
CollectionPermission.reading: [Action.CollectionDocumentRead, Action.CollectionQuery,
Action.CollectionList],
Expand All @@ -71,15 +71,15 @@ def _perms_to_actions(self, permissions: List[Union[CollectionPermission, str]])
# convert strings to the enum value where needed
perms = [
permission if isinstance(permission, CollectionPermission) else CollectionPermission[permission.lower()]
for permission in permissions
for permission in args
]

return [action for perm in perms for action in permission_actions_map[perm]]

def allow(self, permissions: List[Union[CollectionPermission, str]]) -> CollectionRef:
def allow(self, *args: Union[CollectionPermission, str]) -> CollectionRef:
"""Request the required permissions for this collection."""
# Ensure registration of the resource is complete before requesting permissions.
self._register_policy(permissions)
self._register_policy(*args)

return Documents().collection(self.name)

Expand Down
8 changes: 4 additions & 4 deletions nitric/resources/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ def __init__(self, name: str):
def _to_resource(self) -> Resource:
return Resource(name=self.name, type=ResourceType.Queue)

def _perms_to_actions(self, permissions: List[Union[QueuePermission, str]]) -> List[Action]:
def _perms_to_actions(self, *args: Union[QueuePermission, str]) -> List[Action]:
permission_actions_map = {
QueuePermission.sending: [Action.QueueSend, Action.QueueList, Action.QueueDetail],
QueuePermission.receiving: [Action.QueueReceive, Action.QueueList, Action.QueueDetail],
}
# convert strings to the enum value where needed
perms = [
permission if isinstance(permission, QueuePermission) else QueuePermission[permission.lower()]
for permission in permissions
for permission in args
]

return [action for perm in perms for action in permission_actions_map[perm]]
Expand All @@ -74,10 +74,10 @@ async def _register(self):
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)

def allow(self, permissions: List[Union[QueuePermission, str]]) -> QueueRef:
def allow(self, *args: Union[QueuePermission, str]) -> QueueRef:
"""Request the required permissions for this queue."""
# Ensure registration of the resource is complete before requesting permissions.
self._register_policy(permissions)
self._register_policy(*args)

return Queues().queue(self.name)

Expand Down
8 changes: 4 additions & 4 deletions nitric/resources/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ async def _register(self):
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)

def _perms_to_actions(self, permissions: List[Union[SecretPermission, str]]) -> List[Action]:
def _perms_to_actions(self, *args: Union[SecretPermission, str]) -> List[Action]:
permissions_actions_map = {
SecretPermission.accessing: [Action.SecretAccess],
SecretPermission.putting: [Action.SecretPut],
}
# convert strings to the enum value where needed
perms = [
permission if isinstance(permission, SecretPermission) else SecretPermission[permission.lower()]
for permission in permissions
for permission in args
]

return [action for perm in perms for action in permissions_actions_map[perm]]

def allow(self, permissions: List[Union[SecretPermission, str]]) -> SecretContainerRef:
def allow(self, *args: Union[SecretPermission, str]) -> SecretContainerRef:
"""Request the specified permissions to this resource."""

self._register_policy(permissions)
self._register_policy(*args)

return Secrets().secret(self.name)

Expand Down
8 changes: 4 additions & 4 deletions nitric/resources/topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,20 @@ async def _register(self):
def _to_resource(self) -> Resource:
return Resource(name=self.name, type=ResourceType.Topic)

def _perms_to_actions(self, permissions: List[Union[TopicPermission, str]]) -> List[Action]:
def _perms_to_actions(self, *args: Union[TopicPermission, str]) -> List[Action]:
_permMap = {TopicPermission.publishing: [Action.TopicEventPublish]}
# convert strings to the enum value where needed
perms = [
permission if isinstance(permission, TopicPermission) else TopicPermission[permission.lower()]
for permission in permissions
for permission in args
]

return [action for perm in perms for action in _permMap[perm]]

def allow(self, permissions: List[Union[TopicPermission, str]]) -> TopicRef:
def allow(self, *args: Union[TopicPermission, str]) -> TopicRef:
"""Request the specified permissions to this resource."""

self._register_policy(permissions)
self._register_policy(*args)

return Events().topic(self.name)

Expand Down
2 changes: 1 addition & 1 deletion tests/api/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async def test_publish_dict(self):

with patch("nitricapi.nitric.event.v1.EventServiceStub.publish", mock_publish):
topic = Events().topic("test-topic")
await topic.publish({"id": "123", "payload": payload})
await topic.publish(Event(id="123", payload=payload))

mock_publish.assert_called_once_with(
event_publish_request=EventPublishRequest(
Expand Down
10 changes: 5 additions & 5 deletions tests/resources/test_buckets.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_create_allow_writing(self):
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
bucket("test-bucket").allow(["writing"])
bucket("test-bucket").allow("writing")

# Check expected values were passed to Stub
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
Expand All @@ -55,7 +55,7 @@ def test_create_allow_reading(self):
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
bucket("test-bucket").allow(["reading"])
bucket("test-bucket").allow("reading")

# Check expected values were passed to Stub
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
Expand All @@ -73,7 +73,7 @@ def test_create_allow_deleting(self):
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
bucket("test-bucket").allow(["deleting"])
bucket("test-bucket").allow("deleting")

# Check expected values were passed to Stub
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
Expand All @@ -91,7 +91,7 @@ def test_create_allow_all(self):
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
bucket("test-bucket").allow(["deleting", "reading", "writing"])
bucket("test-bucket").allow("deleting", "reading", "writing")

# Check expected values were passed to Stub
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
Expand All @@ -114,7 +114,7 @@ def test_create_allow_all_reversed_policy(self):
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
bucket("test-bucket").allow(["writing", "reading", "deleting"])
bucket("test-bucket").allow("writing", "reading", "deleting")

# Check expected values were passed to Stub
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
Expand Down
10 changes: 5 additions & 5 deletions tests/resources/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_create_allow_writing(self):
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
collection("test-collection").allow(["writing"])
collection("test-collection").allow("writing")

# Check expected values were passed to Stub
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
Expand All @@ -61,7 +61,7 @@ def test_create_allow_reading(self):
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
collection("test-collection").allow(["reading"])
collection("test-collection").allow("reading")

# Check expected values were passed to Stub
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
Expand All @@ -83,7 +83,7 @@ def test_create_allow_deleting(self):
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
collection("test-collection").allow(["deleting"])
collection("test-collection").allow("deleting")

# Check expected values were passed to Stub
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
Expand All @@ -104,7 +104,7 @@ def test_create_allow_all(self):
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
collection("test-collection").allow(["deleting", "reading", "writing"])
collection("test-collection").allow("deleting", "reading", "writing")

# Check expected values were passed to Stub
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
Expand All @@ -130,7 +130,7 @@ def test_create_allow_all_reversed_policy(self):
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
collection("test-collection").allow(["writing", "reading", "deleting"])
collection("test-collection").allow("writing", "reading", "deleting")

# Check expected values were passed to Stub
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
Expand Down
6 changes: 3 additions & 3 deletions tests/resources/test_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_create_allow_sending(self):
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
queue("test-queue").allow(["sending"])
queue("test-queue").allow("sending")

# Check expected values were passed to Stub
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
Expand All @@ -57,7 +57,7 @@ def test_create_allow_receiving(self):
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
queue("test-queue").allow(["receiving"])
queue("test-queue").allow("receiving")

# Check expected values were passed to Stub
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
Expand All @@ -79,7 +79,7 @@ def test_create_allow_all(self):
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
queue("test-queue").allow(["sending", "receiving"])
queue("test-queue").allow("sending", "receiving")

# Check expected values were passed to Stub
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
Expand Down
4 changes: 2 additions & 2 deletions tests/resources/test_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_allow_put(self):
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
secret("test-secret").allow(["putting"])
secret("test-secret").allow("putting")

# Check expected values were passed to Stub
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
Expand All @@ -56,7 +56,7 @@ def test_allow_access(self):
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
secret("test-secret").allow(["accessing"])
secret("test-secret").allow("accessing")

# Check expected values were passed to Stub
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/test_topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_create_allow_publishing(self):
mock_declare.return_value = mock_response

with patch("nitricapi.nitric.resource.v1.ResourceServiceStub.declare", mock_declare):
topic("test-topic").allow(["publishing"])
topic("test-topic").allow("publishing")

# Check expected values were passed to Stub
mock_declare.assert_called_with(resource_declare_request=ResourceDeclareRequest(
Expand Down

0 comments on commit 224a2e9

Please sign in to comment.