@@ -44,44 +44,38 @@ <h1 class="title">Module <code>nitric.api.kv</code></h1>
4444# See the License for the specific language governing permissions and
4545# limitations under the License.
4646#
47- from nitric.proto import key_value
48- from nitric.proto import key_value_service
49- from nitric.api._base_client import BaseClient
50- from google.protobuf.struct_pb2 import Struct
51- from google.protobuf.json_format import MessageToDict
52- from nitric.proto.kv.v1.kv_pb2 import KeyValueGetResponse
47+ from nitric.utils import new_default_channel, _struct_from_dict
48+ from nitric.proto.nitric.kv.v1 import KeyValueStub
5349
5450
55- class KeyValueClient(BaseClient ):
51+ class KeyValueClient(object ):
5652 """
5753 Nitric generic document store/db client.
5854
5955 This client insulates application code from stack specific document CRUD operations or SDKs.
6056 """
6157
62- def __init__(self):
63- """Construct a new DocumentClient."""
64- super(self.__class__, self).__init__()
65- self._stub = key_value_service.KeyValueStub(self._channel)
58+ def __init__(self, collection: str):
59+ """
60+ Construct a new DocumentClient.
6661
67- def put(self, collection: str, key: str, value: dict):
68- """Create a new document with the specified key in the specified collection."""
69- value_struct = Struct()
70- value_struct.update(value)
71- request = key_value.KeyValuePutRequest(collection=collection, key=key, value=value_struct)
72- return self._exec("Put", request)
62+ :param collection: name of the key/value collection
63+ """
64+ self.collection = collection
65+ self._stub = KeyValueStub(channel=new_default_channel())
7366
74- def get(self, collection: str, key: str) -> dict:
75- """Retrieve a document from the specified collection by its key."""
76- request = key_value.KeyValueGetRequest(collection=collection, key=key)
77- reply: KeyValueGetResponse = self._exec("Get", request)
78- document = MessageToDict(reply)["value"]
79- return document
67+ async def put(self, key: str, value: dict):
68+ """Create a new document with the specified key."""
69+ await self._stub.put(collection=self.collection, key=key, value=_struct_from_dict(value))
8070
81- def delete(self, collection: str, key: str):
71+ async def get(self, key: str) -> dict:
72+ """Retrieve a document from the specified key."""
73+ response = await self._stub.get(collection=self.collection, key=key)
74+ return response.value.to_dict()
75+
76+ async def delete(self, key: str):
8277 """Delete the specified document from the collection."""
83- request = key_value.KeyValueDeleteRequest(collection=collection, key=key)
84- return self._exec("Delete", request)</ code > </ pre >
78+ await self._stub.delete(collection=self.collection, key=key)</ code > </ pre >
8579</ details >
8680</ section >
8781< section >
@@ -95,94 +89,94 @@ <h2 class="section-title" id="header-classes">Classes</h2>
9589< dl >
9690< dt id ="nitric.api.kv.KeyValueClient "> < code class ="flex name class ">
9791< span > class < span class ="ident "> KeyValueClient</ span > </ span >
92+ < span > (</ span > < span > collection: str)</ span >
9893</ code > </ dt >
9994< dd >
10095< div class ="desc "> < p > Nitric generic document store/db client.</ p >
10196< p > This client insulates application code from stack specific document CRUD operations or SDKs.</ p >
102- < p > Construct a new DocumentClient.</ p > </ div >
97+ < p > Construct a new DocumentClient.</ p >
98+ < p > :param collection: name of the key/value collection</ p > </ div >
10399< details class ="source ">
104100< summary >
105101< span > Expand source code</ span >
106102</ summary >
107- < pre > < code class ="python "> class KeyValueClient(BaseClient ):
103+ < pre > < code class ="python "> class KeyValueClient(object ):
108104 """
109105 Nitric generic document store/db client.
110106
111107 This client insulates application code from stack specific document CRUD operations or SDKs.
112108 """
113109
114- def __init__(self):
115- """Construct a new DocumentClient."""
116- super(self.__class__, self).__init__()
117- self._stub = key_value_service.KeyValueStub(self._channel)
110+ def __init__(self, collection: str):
111+ """
112+ Construct a new DocumentClient.
113+
114+ :param collection: name of the key/value collection
115+ """
116+ self.collection = collection
117+ self._stub = KeyValueStub(channel=new_default_channel())
118118
119- def put(self, collection: str, key: str, value: dict):
120- """Create a new document with the specified key in the specified collection."""
121- value_struct = Struct()
122- value_struct.update(value)
123- request = key_value.KeyValuePutRequest(collection=collection, key=key, value=value_struct)
124- return self._exec("Put", request)
119+ async def put(self, key: str, value: dict):
120+ """Create a new document with the specified key."""
121+ await self._stub.put(collection=self.collection, key=key, value=_struct_from_dict(value))
125122
126- def get(self, collection: str, key: str) -> dict:
127- """Retrieve a document from the specified collection by its key."""
128- request = key_value.KeyValueGetRequest(collection=collection, key=key)
129- reply: KeyValueGetResponse = self._exec("Get", request)
130- document = MessageToDict(reply)["value"]
131- return document
123+ async def get(self, key: str) -> dict:
124+ """Retrieve a document from the specified key."""
125+ response = await self._stub.get(collection=self.collection, key=key)
126+ return response.value.to_dict()
132127
133- def delete(self, collection: str , key: str):
128+ async def delete(self, key: str):
134129 """Delete the specified document from the collection."""
135- request = key_value.KeyValueDeleteRequest(collection=collection, key=key)
136- return self._exec("Delete", request)</ code > </ pre >
130+ await self._stub.delete(collection=self.collection, key=key)</ code > </ pre >
137131</ details >
138- < h3 > Ancestors</ h3 >
139- < ul class ="hlist ">
140- < li > nitric.api._base_client.BaseClient</ li >
141- < li > abc.ABC</ li >
142- </ ul >
143132< h3 > Methods</ h3 >
144133< dl >
145134< dt id ="nitric.api.kv.KeyValueClient.delete "> < code class ="name flex ">
146- < span > def < span class ="ident "> delete</ span > </ span > (< span > self, collection: str , key: str)</ span >
135+ < span > async def < span class ="ident "> delete</ span > </ span > (< span > self, key: str)</ span >
147136</ code > </ dt >
148137< dd >
149138< div class ="desc "> < p > Delete the specified document from the collection.</ p > </ div >
150139< details class ="source ">
151140< summary >
152141< span > Expand source code</ span >
153142</ summary >
154- < pre > < code class ="python "> def delete(self, collection: str , key: str):
143+ < pre > < code class ="python "> async def delete(self, key: str):
155144 """Delete the specified document from the collection."""
156- request = key_value.KeyValueDeleteRequest(collection=collection, key=key)
157- return self._exec("Delete", request)</ code > </ pre >
145+ await self._stub.delete(collection=self.collection, key=key)</ code > </ pre >
158146</ details >
159147</ dd >
160148< dt id ="nitric.api.kv.KeyValueClient.get "> < code class ="name flex ">
161- < span > def < span class ="ident "> get</ span > </ span > (< span > self, collection: str , key: str) ‑> dict</ span >
149+ < span > async def < span class ="ident "> get</ span > </ span > (< span > self, key: str) ‑> dict</ span >
162150</ code > </ dt >
163151< dd >
164- < div class ="desc "> < p > Retrieve a document from the specified collection by its key.</ p > </ div >
152+ < div class ="desc "> < p > Retrieve a document from the specified key.</ p > </ div >
165153< details class ="source ">
166154< summary >
167155< span > Expand source code</ span >
168156</ summary >
169- < pre > < code class ="python "> def get(self, collection: str, key: str) -> dict:
170- """Retrieve a document from the specified collection by its key."""
171- request = key_value.KeyValueGetRequest(collection=collection, key=key)
172- reply: KeyValueGetResponse = self._exec("Get", request)
173- document = MessageToDict(reply)["value"]
174- return document</ code > </ pre >
157+ < pre > < code class ="python "> async def get(self, key: str) -> dict:
158+ """Retrieve a document from the specified key."""
159+ response = await self._stub.get(collection=self.collection, key=key)
160+ return response.value.to_dict()</ code > </ pre >
175161</ details >
176162</ dd >
177163< dt id ="nitric.api.kv.KeyValueClient.put "> < code class ="name flex ">
164+ < < < < < < < refs /remotes/origin/main
178165< span > def < span class ="ident "> put</ span > </ span > (< span > self, collection: str, key: str, value: dict)</ span >
179166</ code > </ dt >
180167< dd >
181168< div class ="desc "> < p > Create a new document with the specified key in the specified collection.</ p > </ div >
169+ =======
170+ < span > async def < span class ="ident "> put</ span > </ span > (< span > self, key: str, value: dict)</ span >
171+ </ code > </ dt >
172+ < dd >
173+ < div class ="desc "> < p > Create a new document with the specified key.</ p > </ div >
174+ > > > > > > > feat: port faas.start to bi-di streaming with membrane
182175< details class ="source ">
183176< summary >
184177< span > Expand source code</ span >
185178</ summary >
179+ < < < < < < < refs /remotes/origin/main
186180< pre > < code class ="python "> def put(self, collection: str, key: str, value: dict):
187181 """Create a new document with the specified key in the specified collection."""
188182 value_struct = Struct()
@@ -216,6 +210,12 @@ <h3>Instance variables</h3>
216210< dt id ="nitric.api.kv.KeyValueGetResponse.value "> < code class ="name "> var < span class ="ident "> value</ span > </ code > </ dt >
217211< dd >
218212< div class ="desc "> < p > Field nitric.kv.v1.KeyValueGetResponse.value</ p > </ div >
213+ =======
214+ < pre > < code class ="python "> async def put(self, key: str, value: dict):
215+ """Create a new document with the specified key."""
216+ await self._stub.put(collection=self.collection, key=key, value=_struct_from_dict(value))</ code > </ pre >
217+ </ details >
218+ > > > > > > > feat: port faas.start to bi-di streaming with membrane
219219</ dd >
220220</ dl >
221221</ dd >
@@ -243,13 +243,16 @@ <h4><code><a title="nitric.api.kv.KeyValueClient" href="#nitric.api.kv.KeyValueC
243243< li > < code > < a title ="nitric.api.kv.KeyValueClient.put " href ="#nitric.api.kv.KeyValueClient.put "> put</ a > </ code > </ li >
244244</ ul >
245245</ li >
246+ < < < < < < < refs /remotes/origin/main
246247< li >
247248< h4 > < code > < a title ="nitric.api.kv.KeyValueGetResponse " href ="#nitric.api.kv.KeyValueGetResponse "> KeyValueGetResponse</ a > </ code > </ h4 >
248249< ul class ="">
249250< li > < code > < a title ="nitric.api.kv.KeyValueGetResponse.DESCRIPTOR " href ="#nitric.api.kv.KeyValueGetResponse.DESCRIPTOR "> DESCRIPTOR</ a > </ code > </ li >
250251< li > < code > < a title ="nitric.api.kv.KeyValueGetResponse.value " href ="#nitric.api.kv.KeyValueGetResponse.value "> value</ a > </ code > </ li >
251252</ ul >
252253</ li >
254+ =======
255+ > > > > > > > feat: port faas.start to bi-di streaming with membrane
253256</ ul >
254257</ li >
255258</ ul >
0 commit comments