Skip to content

Commit

Permalink
build: Add local gRPC client generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm committed Nov 22, 2022
1 parent 80614d0 commit 66e6e22
Show file tree
Hide file tree
Showing 59 changed files with 3,606 additions and 465 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install
run: make install
- name: Check generated sources
run: |
make grpc-client
git add .
git diff --cached --quiet
- name: Run Tox
# Run tox using the version of Python in `PATH`
run: tox -e py
Expand Down
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Protoc Output Files
nitric/proto/

.idea/

# Byte-compiled / optimized / DLL files
Expand Down Expand Up @@ -145,3 +142,5 @@ cython_debug/


.vscode/

contracts/
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ repos:
rev: stable
hooks:
- id: black
exclude: ^(examples)/
exclude: ^(examples|nitric/proto)/
- repo: https://github.com/pycqa/flake8
rev: 3.7.9
hooks:
Expand Down
2 changes: 1 addition & 1 deletion docs/nitric/api/documents.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ <h1 class="title">Module <code>nitric.api.documents</code></h1>

from nitric.api.const import MAX_SUB_COLLECTION_DEPTH
from nitric.api.exception import exception_from_grpc_error
from nitricapi.nitric.document.v1 import (
from proto.nitric.document.v1 import (
DocumentServiceStub,
Collection as CollectionMessage,
Key as KeyMessage,
Expand Down
2 changes: 1 addition & 1 deletion docs/nitric/api/events.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ <h1 class="title">Module <code>nitric.api.events</code></h1>

from nitric.api.exception import exception_from_grpc_error
from nitric.utils import new_default_channel, _struct_from_dict
from nitricapi.nitric.event.v1 import EventServiceStub, NitricEvent, TopicServiceStub
from proto.nitric.event.v1 import EventServiceStub, NitricEvent, TopicServiceStub
from dataclasses import dataclass, field


Expand Down
2 changes: 1 addition & 1 deletion docs/nitric/api/queues.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ <h1 class="title">Module <code>nitric.api.queues</code></h1>

from nitric.api.exception import FailedPreconditionException, exception_from_grpc_error, InvalidArgumentException
from nitric.utils import new_default_channel, _struct_from_dict, _dict_from_struct
from nitricapi.nitric.queue.v1 import QueueServiceStub, NitricTask, FailedTask as WireFailedTask
from proto.nitric.queue.v1 import QueueServiceStub, NitricTask, FailedTask as WireFailedTask
from dataclasses import dataclass, field


Expand Down
2 changes: 1 addition & 1 deletion docs/nitric/api/secrets.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ <h1 class="title">Module <code>nitric.api.secrets</code></h1>

from nitric.api.exception import exception_from_grpc_error
from nitric.utils import new_default_channel
from nitricapi.nitric.secret.v1 import SecretServiceStub, Secret as SecretMessage, SecretVersion as VersionMessage
from proto.nitric.secret.v1 import SecretServiceStub, Secret as SecretMessage, SecretVersion as VersionMessage


class Secrets(object):
Expand Down
2 changes: 1 addition & 1 deletion docs/nitric/api/storage.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ <h1 class="title">Module <code>nitric.api.storage</code></h1>

from nitric.api.exception import exception_from_grpc_error
from nitric.utils import new_default_channel
from nitricapi.nitric.storage.v1 import StorageServiceStub
from proto.nitric.storage.v1 import StorageServiceStub


class Storage(object):
Expand Down
2 changes: 1 addition & 1 deletion docs/nitric/faas.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h1 class="title">Module <code>nitric.faas</code></h1>
import betterproto
from betterproto.grpc.util.async_channel import AsyncChannel
from nitric.utils import new_default_channel
from nitricapi.nitric.faas.v1 import (
from proto.nitric.faas.v1 import (
FaasServiceStub,
InitRequest,
ClientMessage,
Expand Down
17 changes: 17 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ clean:
@rm -rf ./build
@rm -rf ./dist

NITRIC_VERSION="v0.20.0-rc.2"

download:
@curl -L https://github.com/nitrictech/nitric/releases/download/${NITRIC_VERSION}/contracts.tgz -o contracts.tgz
@tar xvzf contracts.tgz
@rm contracts.tgz

OUTPUT="./nitric/proto"
CONTRACTS="./contracts"

grpc-client: install download
@echo Generating Proto Sources
@echo $(OUTPUT)
@mkdir -p $(OUTPUT)
@python3 -m grpc_tools.protoc -I $(CONTRACTS) --python_betterproto_out=$(OUTPUT) ./contracts/proto/*/*/*.proto


license:
@echo Applying Apache 2 header to source files
@licenseheaders -t tools/apache-2.tmpl -o "Nitric Technologies Pty Ltd" -y 2021 -n "Nitric Python 3 SDK" -u "https://github.com/nitrictech/python-sdk" -d nitric
Expand Down
24 changes: 15 additions & 9 deletions nitric/api/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@

from nitric.api.const import MAX_SUB_COLLECTION_DEPTH
from nitric.api.exception import exception_from_grpc_error
from nitricapi.nitric.document.v1 import (
from nitric.proto.nitric.document.v1 import (
DocumentServiceStub,
Collection as CollectionMessage,
Key as KeyMessage,
Expression as ExpressionMessage,
ExpressionValue,
Document as DocumentMessage, DocumentSetRequest, DocumentGetRequest, DocumentDeleteRequest,
DocumentQueryStreamRequest, DocumentQueryRequest,
Document as DocumentMessage,
DocumentSetRequest,
DocumentGetRequest,
DocumentDeleteRequest,
DocumentQueryStreamRequest,
DocumentQueryRequest,
)

from nitric.utils import new_default_channel, _dict_from_struct, _struct_from_dict
Expand Down Expand Up @@ -75,9 +79,9 @@ def collection(self, name: str) -> CollectionRef:
async def get(self) -> Document:
"""Retrieve the contents of this document, if it exists."""
try:
response = await self._documents._stub.get(document_get_request=DocumentGetRequest(
key=_doc_ref_to_wire(self)
))
response = await self._documents._stub.get(
document_get_request=DocumentGetRequest(key=_doc_ref_to_wire(self))
)
return _document_from_wire(documents=self._documents, message=response.document)
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)
Expand All @@ -101,9 +105,11 @@ async def set(self, content: dict):
async def delete(self):
"""Delete this document, if it exists."""
try:
await self._documents._stub.delete(document_delete_request=DocumentDeleteRequest(
key=_doc_ref_to_wire(self),
))
await self._documents._stub.delete(
document_delete_request=DocumentDeleteRequest(
key=_doc_ref_to_wire(self),
)
)
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)

Expand Down
13 changes: 8 additions & 5 deletions nitric/api/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@

from nitric.api.exception import exception_from_grpc_error
from nitric.utils import new_default_channel, _struct_from_dict
from nitricapi.nitric.event.v1 import EventServiceStub, NitricEvent, TopicServiceStub, EventPublishRequest, \
TopicListRequest
from nitric.proto.nitric.event.v1 import (
EventServiceStub,
NitricEvent,
TopicServiceStub,
EventPublishRequest,
TopicListRequest,
)
from dataclasses import dataclass, field


Expand Down Expand Up @@ -71,9 +76,7 @@ async def publish(

try:
response = await self._events._stub.publish(
event_publish_request=EventPublishRequest(
topic=self.name, event=_event_to_wire(event)
)
event_publish_request=EventPublishRequest(topic=self.name, event=_event_to_wire(event))
)
return Event(**{**event.__dict__.copy(), **{"id": response.id}})
except GRPCError as grpc_err:
Expand Down
27 changes: 21 additions & 6 deletions nitric/api/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@

from nitric.api.exception import FailedPreconditionException, exception_from_grpc_error, InvalidArgumentException
from nitric.utils import new_default_channel, _struct_from_dict, _dict_from_struct
from nitricapi.nitric.queue.v1 import QueueServiceStub, NitricTask, FailedTask as WireFailedTask, QueueCompleteRequest, \
QueueSendRequest, QueueSendBatchRequest, QueueReceiveRequest
from nitric.proto.nitric.queue.v1 import (
QueueServiceStub,
NitricTask,
FailedTask as WireFailedTask,
QueueCompleteRequest,
QueueSendRequest,
QueueSendBatchRequest,
QueueReceiveRequest,
)
from dataclasses import dataclass, field


Expand Down Expand Up @@ -60,7 +67,9 @@ async def complete(self):
"Task is missing internal client or lease id, was it returned from " "queue.receive?"
)
try:
await self._queueing._queue_stub.complete(queue_complete_request=QueueCompleteRequest(queue=self._queue.name, lease_id=self.lease_id))
await self._queueing._queue_stub.complete(
queue_complete_request=QueueCompleteRequest(queue=self._queue.name, lease_id=self.lease_id)
)
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)

Expand Down Expand Up @@ -150,7 +159,9 @@ async def send(
task = Task(**task)

try:
await self._queueing._queue_stub.send(queue_send_request=QueueSendRequest(queue=self.name, task=_task_to_wire(task)))
await self._queueing._queue_stub.send(
queue_send_request=QueueSendRequest(queue=self.name, task=_task_to_wire(task))
)
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)

Expand All @@ -168,7 +179,9 @@ async def _send_batch(self, tasks: List[Union[Task, dict]], raise_on_failure: bo
wire_tasks = [_task_to_wire(Task(**task) if isinstance(task, dict) else task) for task in tasks]

try:
response = await self._queueing._queue_stub.send_batch(queue_send_batch_request=QueueSendBatchRequest(queue=self.name, tasks=wire_tasks))
response = await self._queueing._queue_stub.send_batch(
queue_send_batch_request=QueueSendBatchRequest(queue=self.name, tasks=wire_tasks)
)
return [_wire_to_failed_task(failed_task) for failed_task in response.failed_tasks]
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)
Expand All @@ -191,7 +204,9 @@ async def receive(self, limit: int = None) -> List[Task]:
limit = 1

try:
response = await self._queueing._queue_stub.receive(queue_receive_request=QueueReceiveRequest(queue=self.name, depth=limit))
response = await self._queueing._queue_stub.receive(
queue_receive_request=QueueReceiveRequest(queue=self.name, depth=limit)
)
# Map the response protobuf response items to Python SDK Nitric Tasks
return [_wire_to_received_task(task=task, queueing=self._queueing, queue=self) for task in response.tasks]
except GRPCError as grpc_err:
Expand Down
17 changes: 13 additions & 4 deletions nitric/api/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@

from nitric.api.exception import exception_from_grpc_error
from nitric.utils import new_default_channel
from nitricapi.nitric.secret.v1 import SecretServiceStub, Secret as SecretMessage, SecretVersion as VersionMessage, \
SecretPutRequest, SecretAccessRequest
from nitric.proto.nitric.secret.v1 import (
SecretServiceStub,
Secret as SecretMessage,
SecretVersion as VersionMessage,
SecretPutRequest,
SecretAccessRequest,
)


class Secrets(object):
Expand Down Expand Up @@ -73,7 +78,9 @@ async def put(self, value: Union[str, bytes]) -> SecretVersion:
secret_message = _secret_to_wire(self)

try:
response = await self._secrets._secrets_stub.put(secret_put_request=SecretPutRequest(secret=secret_message, value=value))
response = await self._secrets._secrets_stub.put(
secret_put_request=SecretPutRequest(secret=secret_message, value=value)
)
return self.version(version=response.secret_version.version)
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)
Expand Down Expand Up @@ -112,7 +119,9 @@ async def access(self) -> SecretValue:
"""Return the value stored against this version of the secret."""
version_message = _secret_version_to_wire(self)
try:
response = await self._secrets._secrets_stub.access(secret_access_request=SecretAccessRequest(secret_version=version_message))
response = await self._secrets._secrets_stub.access(
secret_access_request=SecretAccessRequest(secret_version=version_message)
)
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)

Expand Down
18 changes: 14 additions & 4 deletions nitric/api/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@
# limitations under the License.
#
from dataclasses import dataclass
from typing import List

from grpclib import GRPCError
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, \
StorageReadRequest, StorageDeleteRequest, StoragePreSignUrlRequest, StorageListFilesRequest
from nitric.proto.nitric.storage.v1 import (
StorageServiceStub,
StoragePreSignUrlRequestOperation,
StorageWriteRequest,
StorageReadRequest,
StorageDeleteRequest,
StoragePreSignUrlRequest,
StorageListFilesRequest,
)
from enum import Enum


Expand Down Expand Up @@ -61,8 +67,10 @@ def file(self, key: str):
return File(_storage=self._storage, _bucket=self.name, key=key)

async def files(self):
"""Return a list of files in this bucket."""
resp = await self._storage._storage_stub.list_files(
storage_list_files_request=StorageListFilesRequest(bucket_name=self.name))
storage_list_files_request=StorageListFilesRequest(bucket_name=self.name)
)
return [self.file(f.key) for f in resp.files]


Expand Down Expand Up @@ -123,9 +131,11 @@ async def delete(self):
raise exception_from_grpc_error(grpc_err)

async def upload_url(self, expiry: int = 600):
"""Get a temporary writable URL to this file."""
await self.sign_url(mode=FileMode.WRITE, expiry=expiry)

async def download_url(self, expiry: int = 600):
"""Get a temporary readable URL to this file."""
await self.sign_url(mode=FileMode.READ, expiry=expiry)

async def sign_url(self, mode: FileMode = FileMode.READ, expiry: int = 3600):
Expand Down
3 changes: 2 additions & 1 deletion nitric/faas.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import betterproto
from betterproto.grpc.util.async_channel import AsyncChannel
from nitric.utils import new_default_channel
from nitricapi.nitric.faas.v1 import (
from nitric.proto.nitric.faas.v1 import (
FaasServiceStub,
InitRequest,
ClientMessage,
Expand Down Expand Up @@ -398,6 +398,7 @@ def __init__(self, security: dict[str, List[str]] = None):

class FaasWorkerOptions:
"""Empty worker options for generic function handlers."""

pass


Expand Down
Empty file added nitric/proto/__init__.py
Empty file.
Empty file.
Loading

0 comments on commit 66e6e22

Please sign in to comment.