Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,5 @@ dmypy.json
# Cython debug symbols
cython_debug/


.vscode/
9 changes: 8 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ repos:
rev: 3.7.9
hooks:
- id: flake8
exclude: ^(venv|tests|build|dist|nitric/proto|examples)/
exclude: ^(venv|tests|build|dist|nitric/proto|examples)/
- repo: https://github.com/pycqa/pydocstyle
rev: 6.0.0
hooks:
- id: pydocstyle
args:
- --ignore=D100, D105, D203, D212, D415
exclude: ^(venv|tests|build|dist|nitric/proto|examples)/
13 changes: 8 additions & 5 deletions examples/queues/failed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
from typing import List
from nitric.api import Queues, Task
from nitric.api.queues import FailedTask

# [END import]
async def queues_failed():
# [START snippet]
# Construct a new queue client with default settings
# [START snippet]
# Construct a new queue client with default settings
queues = Queues()

payload = {"content": "of task"}

# Publish tasks to queue
failed_task = await queues.queue("my-queue").send([Task(payload=payload) for i in range(2)])

# Process the failed task
for task in failed_task:
print(task.message)
# [END snippet]
print(task.message)


# [END snippet]
4 changes: 2 additions & 2 deletions nitric/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# limitations under the License.
#
"""Nitric API SDK."""
from nitric.api.events import Events, Event, Topic
from nitric.api.events import Events, Event, TopicRef
from nitric.api.queues import Queues, Task, FailedTask
from nitric.api.storage import Storage
from nitric.api.documents import Documents
Expand All @@ -31,6 +31,6 @@
"Event",
"Task",
"FailedTask",
"Topic",
"TopicRef",
"Secrets",
]
8 changes: 4 additions & 4 deletions nitric/api/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _event_to_wire(event: Event) -> NitricEvent:


@dataclass(frozen=True, order=True)
class Topic(object):
class TopicRef(object):
"""A reference to a topic on an event service, used to perform operations on that topic."""

_events: Events
Expand Down Expand Up @@ -94,14 +94,14 @@ def __del__(self):
if self.channel is not None:
self.channel.close()

async def topics(self) -> List[Topic]:
async def topics(self) -> List[TopicRef]:
"""Get a list of topics available for publishing or subscription."""
try:
response = await self._topic_stub.list()
return [self.topic(topic.name) for topic in response.topics]
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)

def topic(self, name: str) -> Topic:
def topic(self, name: str) -> TopicRef:
"""Return a reference to a topic."""
return Topic(_events=self, name=name)
return TopicRef(_events=self, name=name)
8 changes: 4 additions & 4 deletions nitric/api/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ReceivedTask(object):
payload: dict = field(default_factory=dict)
lease_id: str = field(default=None)
_queueing: Queues = field(default=None)
_queue: Queue = field(default=None)
_queue: QueueRef = field(default=None)

async def complete(self):
"""
Expand Down Expand Up @@ -85,7 +85,7 @@ def _task_to_wire(task: Task) -> NitricTask:
)


def _wire_to_received_task(task: NitricTask, queueing: Queues = None, queue: Queue = None) -> ReceivedTask:
def _wire_to_received_task(task: NitricTask, queueing: Queues = None, queue: QueueRef = None) -> ReceivedTask:
"""
Convert a Nitric Queue Task (protobuf) to a Nitric Task (python SDK).

Expand Down Expand Up @@ -120,7 +120,7 @@ def _wire_to_failed_task(failed_task: WireFailedTask) -> FailedTask:


@dataclass(frozen=True, order=True)
class Queue(object):
class QueueRef(object):
"""A reference to a queue from a queue service, used to perform operations on that queue."""

_queueing: Queues
Expand Down Expand Up @@ -212,4 +212,4 @@ def __del__(self):

def queue(self, name: str):
"""Return a reference to a queue from the connected queue service."""
return Queue(_queueing=self, name=name)
return QueueRef(_queueing=self, name=name)
8 changes: 4 additions & 4 deletions nitric/api/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ def __del__(self):

def secret(self, name: str):
"""Return a reference to a secret container from the connected secrets management service."""
return SecretContainer(_secrets=self, name=name)
return SecretContainerRef(_secrets=self, name=name)


def _secret_to_wire(secret: SecretContainer) -> SecretMessage:
def _secret_to_wire(secret: SecretContainerRef) -> SecretMessage:
return SecretMessage(name=secret.name)


@dataclass(frozen=True)
class SecretContainer(object):
class SecretContainerRef(object):
"""A reference to a secret container, used to store and retrieve secret versions."""

_secrets: Secrets
Expand Down Expand Up @@ -104,7 +104,7 @@ class SecretVersion(object):
"""A reference to a version of a secret, used to access the value of the version."""

_secrets: Secrets
secret: SecretContainer
secret: SecretContainerRef
id: str

async def access(self) -> SecretValue:
Expand Down
9 changes: 4 additions & 5 deletions nitric/api/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ def __del__(self):

def bucket(self, name: str):
"""Return a reference to a bucket from the connected storage service."""
return Bucket(_storage=self, name=name)
return BucketRef(_storage=self, name=name)


@dataclass(frozen=True, order=True)
class Bucket(object):
class BucketRef(object):
"""A reference to a bucket in a storage service, used to the perform operations on that bucket."""

_storage: Storage
Expand All @@ -67,7 +67,7 @@ class FileMode(Enum):
WRITE = 1

def to_request_operation(self) -> StoragePreSignUrlRequestOperation:
"""Convert FileMode to a StoragePreSignUrlRequestOperation"""
"""Convert FileMode to a StoragePreSignUrlRequestOperation."""
if self == FileMode.READ:
return StoragePreSignUrlRequestOperation.READ
elif self == FileMode.WRITE:
Expand Down Expand Up @@ -111,8 +111,7 @@ async def delete(self):
raise exception_from_grpc_error(grpc_err)

async def sign_url(self, mode: FileMode = FileMode.READ, expiry: int = 3600):
"""Generated a signed url for reading or writing to a file"""

"""Generate a signed URL for reading or writing to a file."""
try:
await self._storage._storage_stub.pre_sign_url(
bucket_name=self._bucket, key=self.key, operation=mode.to_request_operation(), expiry=expiry
Expand Down
49 changes: 49 additions & 0 deletions nitric/application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import asyncio
from nitric.faas import FunctionServer
# from nitric.resources.base import BaseResource
from typing import Dict, List, Type, Any, TypeVar


BT = TypeVar('BT')

class Nitric:
_workers: List[FunctionServer] = []
_cache: Dict[str, Dict[str, Any]] = {
"api": {},
"bucket": {},
"topic": {},
"secret": {},
"queue": {},
"collection": {}
}

@classmethod
def _register_worker(cls, srv: FunctionServer):
"""
Register a worker for this
"""
cls._workers.append(srv)

@classmethod
def _create_resource(cls, resource: Type[BT], name: str) -> BT:
resource_type = resource.__name__.lower()
if cls._cache.get(resource_type).get(name) is None:
cls._cache[resource_type][name] = resource.make(name)

return cls._cache[resource_type][name]

@classmethod
def run(cls):
"""
Start the nitric application, this will execute in an existing event loop if there is one, otherwise it will
attempt to create its own
"""
try:
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.get_event_loop()

loop.run_until_complete(asyncio.gather(*[wkr.start() for wkr in cls._workers]))
except KeyboardInterrupt:
print("\nexiting")
Loading