Skip to content

Commit 3079574

Browse files
committed
refactor: Updates to use nitric-api package.
1 parent 8e9db54 commit 3079574

22 files changed

Lines changed: 106 additions & 100 deletions

nitric/api/documents.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626

2727
from nitric.api.const import MAX_SUB_COLLECTION_DEPTH
2828
from nitric.api.exception import exception_from_grpc_error
29-
from nitric.proto.nitric.document.v1 import (
29+
from nitricapi.nitric.document.v1 import (
3030
DocumentServiceStub,
3131
Collection as CollectionMessage,
3232
Key as KeyMessage,
3333
Expression as ExpressionMessage,
3434
ExpressionValue,
3535
Document as DocumentMessage,
3636
)
37+
3738
from nitric.utils import new_default_channel, _dict_from_struct, _struct_from_dict
3839

3940
NIL_DOC_ID = ""
@@ -228,7 +229,7 @@ def is_sub_collection(self):
228229
return self.parent is not None
229230

230231
def to_collection_ref(self):
231-
"""Return this collection group as a collection ref"""
232+
"""Return this collection group as a collection ref."""
232233
return CollectionRef(
233234
self._documents,
234235
self.name,
@@ -241,7 +242,7 @@ def to_collection_ref(self):
241242

242243
@staticmethod
243244
def from_collection_ref(collectionRef: CollectionRef, documents: Documents) -> CollectionGroupRef:
244-
"""Return a collection ref as a collection group"""
245+
"""Return a collection ref as a collection group."""
245246
if collectionRef.parent is not None:
246247
return CollectionGroupRef(
247248
documents,

nitric/api/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
from nitric.api.exception import exception_from_grpc_error
2626
from nitric.utils import new_default_channel, _struct_from_dict
27-
from nitric.proto.nitric.event.v1 import EventServiceStub, NitricEvent, TopicServiceStub
27+
from nitricapi.nitric.event.v1 import EventServiceStub, NitricEvent, TopicServiceStub
2828
from dataclasses import dataclass, field
2929

3030

nitric/api/exception.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ class NitricServiceException(Exception):
2626

2727

2828
class AbortedException(NitricServiceException):
29-
"""The operation was aborted, typically due to a concurrency issue such as a sequencer check failure or
30-
transaction abort."""
29+
"""The operation was aborted, typically due to a concurrency issue such as a transaction abort."""
3130

3231
pass
3332

@@ -144,6 +143,7 @@ class UnknownException(NitricServiceException):
144143

145144

146145
def exception_from_grpc_error(error: GRPCError):
146+
"""Translate a gRPC error to a nitric api exception."""
147147
return exception_from_grpc_code(error.status.value, error.message)
148148

149149

nitric/api/queues.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
from nitric.api.exception import FailedPreconditionException, exception_from_grpc_error, InvalidArgumentException
2626
from nitric.utils import new_default_channel, _struct_from_dict, _dict_from_struct
27-
from nitric.proto.nitric.queue.v1 import QueueServiceStub, NitricTask, FailedTask as WireFailedTask
27+
from nitricapi.nitric.queue.v1 import QueueServiceStub, NitricTask, FailedTask as WireFailedTask
2828
from dataclasses import dataclass, field
2929

3030

nitric/api/secrets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
from nitric.api.exception import exception_from_grpc_error
2626
from nitric.utils import new_default_channel
27-
from nitric.proto.nitric.secret.v1 import SecretServiceStub, Secret as SecretMessage, SecretVersion as VersionMessage
27+
from nitricapi.nitric.secret.v1 import SecretServiceStub, Secret as SecretMessage, SecretVersion as VersionMessage
2828

2929

3030
class Secrets(object):

nitric/api/storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from nitric.api.exception import exception_from_grpc_error
2424
from nitric.utils import new_default_channel
25-
from nitric.proto.nitric.storage.v1 import StorageServiceStub
25+
from nitricapi.nitric.storage.v1 import StorageServiceStub
2626

2727

2828
class Storage(object):

nitric/faas/faas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from nitric.utils import new_default_channel
2727
from nitric.faas import Trigger, Response
28-
from nitric.proto.nitric.faas.v1 import FaasServiceStub, InitRequest, ClientMessage
28+
from nitricapi.nitric.faas.v1 import FaasServiceStub, InitRequest, ClientMessage
2929
import asyncio
3030

3131
Handler = Callable[

nitric/faas/response.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import json
2020
from dataclasses import dataclass, field
2121
from typing import Union, Any
22-
from nitric.proto.nitric.faas import v1
23-
from nitric.proto.nitric.faas.v1 import TriggerResponse
22+
from nitricapi.nitric.faas import v1
23+
from nitricapi.nitric.faas.v1 import TriggerResponse
2424

2525

2626
@dataclass(order=True)

nitric/faas/trigger.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import betterproto
2323

24-
from nitric.proto.nitric.faas.v1 import TriggerRequest
24+
from nitricapi.nitric.faas.v1 import TriggerRequest
2525
from nitric.faas.response import Response, TopicResponseContext, HttpResponseContext, ResponseContext
2626

2727

@@ -94,9 +94,14 @@ def from_trigger_request(trigger_request: TriggerRequest):
9494
"""Return a TriggerContext from a TriggerRequest."""
9595
context_type, context = betterproto.which_one_of(trigger_request, "context")
9696
if context_type == "http":
97+
if len(trigger_request.http.headers.keys()) > 0:
98+
new_headers = {k: v[0].value for (k, v) in trigger_request.http.headers.items()}
99+
else:
100+
new_headers = trigger_request.http.headers_old
101+
97102
return TriggerContext(
98103
context=HttpTriggerContext(
99-
headers=trigger_request.http.headers,
104+
headers=new_headers,
100105
method=trigger_request.http.method,
101106
query_params=trigger_request.http.query_params,
102107
path=trigger_request.http.path,

tests/api/test_documents.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
QueryResultsPage,
3535
)
3636
from nitric.api.exception import UnknownException
37-
from nitric.proto.nitric.document.v1 import (
37+
from nitricapi.nitric.document.v1 import (
3838
Key,
3939
Collection,
4040
DocumentGetResponse,
@@ -44,7 +44,7 @@
4444
ExpressionValue,
4545
DocumentQueryStreamResponse,
4646
)
47-
from nitric.proto.nitric.event.v1 import TopicListResponse, NitricTopic
47+
from nitricapi.nitric.event.v1 import TopicListResponse, NitricTopic
4848
from nitric.utils import _struct_from_dict
4949

5050

@@ -56,7 +56,7 @@ class DocumentsClientTest(IsolatedAsyncioTestCase):
5656
async def test_set_document(self):
5757
mock_set = AsyncMock()
5858

59-
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.set", mock_set):
59+
with patch("nitricapi.nitric.document.v1.DocumentServiceStub.set", mock_set):
6060
await Documents().collection("a").doc("b").set({"a": 1})
6161

6262
mock_set.assert_called_once_with(
@@ -74,7 +74,7 @@ async def test_set_document(self):
7474
async def test_set_subcollection_document(self):
7575
mock_set = AsyncMock()
7676

77-
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.set", mock_set):
77+
with patch("nitricapi.nitric.document.v1.DocumentServiceStub.set", mock_set):
7878
await Documents().collection("a").doc("b").collection("c").doc("d").set({"a": 1})
7979

8080
mock_set.assert_called_once_with(
@@ -108,7 +108,7 @@ async def test_get_document(self):
108108
),
109109
)
110110

111-
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.get", mock_get):
111+
with patch("nitricapi.nitric.document.v1.DocumentServiceStub.get", mock_get):
112112
response = await Documents().collection("a").doc("b").get()
113113

114114
mock_get.assert_called_once_with(
@@ -139,7 +139,7 @@ async def test_get_subcollection_document(self):
139139
),
140140
)
141141

142-
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.get", mock_get):
142+
with patch("nitricapi.nitric.document.v1.DocumentServiceStub.get", mock_get):
143143
response: SdkDocument = await Documents().collection("a").doc("b").collection("c").doc("d").get()
144144

145145
mock_get.assert_called_once_with(
@@ -163,7 +163,7 @@ async def test_get_subcollection_document(self):
163163
async def test_delete_document(self):
164164
mock_delete = AsyncMock()
165165

166-
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.delete", mock_delete):
166+
with patch("nitricapi.nitric.document.v1.DocumentServiceStub.delete", mock_delete):
167167
await Documents().collection("a").doc("b").delete()
168168

169169
mock_delete.assert_called_once_with(
@@ -176,7 +176,7 @@ async def test_delete_document(self):
176176
async def test_delete_subcollection_document(self):
177177
mock_delete = AsyncMock()
178178

179-
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.delete", mock_delete):
179+
with patch("nitricapi.nitric.document.v1.DocumentServiceStub.delete", mock_delete):
180180
await Documents().collection("a").doc("b").collection("c").doc("d").delete()
181181

182182
mock_delete.assert_called_once_with(
@@ -231,7 +231,7 @@ async def test_collection_query_fetch(self):
231231
paging_token={"b": "c"},
232232
)
233233

234-
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query", mock_query):
234+
with patch("nitricapi.nitric.document.v1.DocumentServiceStub.query", mock_query):
235235
results = (
236236
await Documents()
237237
.collection("a")
@@ -272,7 +272,7 @@ async def test_subcollection_query_fetch(self):
272272
paging_token={"b": "c"},
273273
)
274274

275-
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query", mock_query):
275+
with patch("nitricapi.nitric.document.v1.DocumentServiceStub.query", mock_query):
276276
results = (
277277
await Documents()
278278
.collection("a")
@@ -323,7 +323,7 @@ async def mock_stream(self, **kwargs):
323323
document=Document(content=Struct(fields={"a": Value(number_value=i)}))
324324
)
325325

326-
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query_stream", mock_stream):
326+
with patch("nitricapi.nitric.document.v1.DocumentServiceStub.query_stream", mock_stream):
327327
results = []
328328
async for result in Documents().collection("a").query().where("name", "startsWith", "test").stream():
329329
results.append(result)
@@ -347,31 +347,31 @@ async def test_set_document_error(self):
347347
mock_set = AsyncMock()
348348
mock_set.side_effect = GRPCError(Status.UNKNOWN, "test error")
349349

350-
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.set", mock_set):
350+
with patch("nitricapi.nitric.document.v1.DocumentServiceStub.set", mock_set):
351351
with pytest.raises(UnknownException) as e:
352352
await Documents().collection("a").doc("b").set({"a": 1})
353353

354354
async def test_get_document_error(self):
355355
mock_get = AsyncMock()
356356
mock_get.side_effect = GRPCError(Status.UNKNOWN, "test error")
357357

358-
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.get", mock_get):
358+
with patch("nitricapi.nitric.document.v1.DocumentServiceStub.get", mock_get):
359359
with pytest.raises(UnknownException) as e:
360360
await Documents().collection("a").doc("b").get()
361361

362362
async def test_delete_document_error(self):
363363
mock_delete = AsyncMock()
364364
mock_delete.side_effect = GRPCError(Status.UNKNOWN, "test error")
365365

366-
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.delete", mock_delete):
366+
with patch("nitricapi.nitric.document.v1.DocumentServiceStub.delete", mock_delete):
367367
with pytest.raises(UnknownException) as e:
368368
await Documents().collection("a").doc("b").delete()
369369

370370
async def test_query_fetch_error(self):
371371
mock_fetch = AsyncMock()
372372
mock_fetch.side_effect = GRPCError(Status.UNKNOWN, "test error")
373373

374-
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query", mock_fetch):
374+
with patch("nitricapi.nitric.document.v1.DocumentServiceStub.query", mock_fetch):
375375
with pytest.raises(UnknownException) as e:
376376
await Documents().collection("a").query().fetch()
377377

@@ -390,7 +390,7 @@ async def mock_stream(self, **kwargs):
390390
document=Document(content=Struct(fields={"a": Value(number_value=i)}))
391391
)
392392

393-
with patch("nitric.proto.nitric.document.v1.DocumentServiceStub.query_stream", mock_stream):
393+
with patch("nitricapi.nitric.document.v1.DocumentServiceStub.query_stream", mock_stream):
394394
with pytest.raises(UnknownException) as e:
395395
async for result in Documents().collection("a").query().stream():
396396
assert False

0 commit comments

Comments
 (0)