1# -*- coding: utf-8 -*-
2# Copyright 2025 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import json # type: ignore
17from google.api_core import path_template
18from google.api_core import gapic_v1
19
20from google.protobuf import json_format
21from google.cloud.location import locations_pb2 # type: ignore
22from .base import FirestoreTransport, DEFAULT_CLIENT_INFO
23
24import re
25from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
26
27
28from google.cloud.firestore_v1.types import document
29from google.cloud.firestore_v1.types import document as gf_document
30from google.cloud.firestore_v1.types import firestore
31from google.protobuf import empty_pb2 # type: ignore
32from google.longrunning import operations_pb2 # type: ignore
33
34
35class _BaseFirestoreRestTransport(FirestoreTransport):
36 """Base REST backend transport for Firestore.
37
38 Note: This class is not meant to be used directly. Use its sync and
39 async sub-classes instead.
40
41 This class defines the same methods as the primary client, so the
42 primary client can load the underlying transport implementation
43 and call it.
44
45 It sends JSON representations of protocol buffers over HTTP/1.1
46 """
47
48 def __init__(
49 self,
50 *,
51 host: str = "firestore.googleapis.com",
52 credentials: Optional[Any] = None,
53 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
54 always_use_jwt_access: Optional[bool] = False,
55 url_scheme: str = "https",
56 api_audience: Optional[str] = None,
57 ) -> None:
58 """Instantiate the transport.
59 Args:
60 host (Optional[str]):
61 The hostname to connect to (default: 'firestore.googleapis.com').
62 credentials (Optional[Any]): The
63 authorization credentials to attach to requests. These
64 credentials identify the application to the service; if none
65 are specified, the client will attempt to ascertain the
66 credentials from the environment.
67 client_info (google.api_core.gapic_v1.client_info.ClientInfo):
68 The client info used to send a user-agent string along with
69 API requests. If ``None``, then default info will be used.
70 Generally, you only need to set this if you are developing
71 your own client library.
72 always_use_jwt_access (Optional[bool]): Whether self signed JWT should
73 be used for service account credentials.
74 url_scheme: the protocol scheme for the API endpoint. Normally
75 "https", but for testing or local servers,
76 "http" can be specified.
77 """
78 # Run the base constructor
79 maybe_url_match = re.match("^(?P<scheme>http(?:s)?://)?(?P<host>.*)$", host)
80 if maybe_url_match is None:
81 raise ValueError(
82 f"Unexpected hostname structure: {host}"
83 ) # pragma: NO COVER
84
85 url_match_items = maybe_url_match.groupdict()
86
87 host = f"{url_scheme}://{host}" if not url_match_items["scheme"] else host
88
89 super().__init__(
90 host=host,
91 credentials=credentials,
92 client_info=client_info,
93 always_use_jwt_access=always_use_jwt_access,
94 api_audience=api_audience,
95 )
96
97 class _BaseBatchGetDocuments:
98 def __hash__(self): # pragma: NO COVER
99 return NotImplementedError("__hash__ must be implemented.")
100
101 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
102
103 @classmethod
104 def _get_unset_required_fields(cls, message_dict):
105 return {
106 k: v
107 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
108 if k not in message_dict
109 }
110
111 @staticmethod
112 def _get_http_options():
113 http_options: List[Dict[str, str]] = [
114 {
115 "method": "post",
116 "uri": "/v1/{database=projects/*/databases/*}/documents:batchGet",
117 "body": "*",
118 },
119 ]
120 return http_options
121
122 @staticmethod
123 def _get_transcoded_request(http_options, request):
124 pb_request = firestore.BatchGetDocumentsRequest.pb(request)
125 transcoded_request = path_template.transcode(http_options, pb_request)
126 return transcoded_request
127
128 @staticmethod
129 def _get_request_body_json(transcoded_request):
130 # Jsonify the request body
131
132 body = json_format.MessageToJson(
133 transcoded_request["body"], use_integers_for_enums=True
134 )
135 return body
136
137 @staticmethod
138 def _get_query_params_json(transcoded_request):
139 query_params = json.loads(
140 json_format.MessageToJson(
141 transcoded_request["query_params"],
142 use_integers_for_enums=True,
143 )
144 )
145 query_params.update(
146 _BaseFirestoreRestTransport._BaseBatchGetDocuments._get_unset_required_fields(
147 query_params
148 )
149 )
150
151 query_params["$alt"] = "json;enum-encoding=int"
152 return query_params
153
154 class _BaseBatchWrite:
155 def __hash__(self): # pragma: NO COVER
156 return NotImplementedError("__hash__ must be implemented.")
157
158 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
159
160 @classmethod
161 def _get_unset_required_fields(cls, message_dict):
162 return {
163 k: v
164 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
165 if k not in message_dict
166 }
167
168 @staticmethod
169 def _get_http_options():
170 http_options: List[Dict[str, str]] = [
171 {
172 "method": "post",
173 "uri": "/v1/{database=projects/*/databases/*}/documents:batchWrite",
174 "body": "*",
175 },
176 ]
177 return http_options
178
179 @staticmethod
180 def _get_transcoded_request(http_options, request):
181 pb_request = firestore.BatchWriteRequest.pb(request)
182 transcoded_request = path_template.transcode(http_options, pb_request)
183 return transcoded_request
184
185 @staticmethod
186 def _get_request_body_json(transcoded_request):
187 # Jsonify the request body
188
189 body = json_format.MessageToJson(
190 transcoded_request["body"], use_integers_for_enums=True
191 )
192 return body
193
194 @staticmethod
195 def _get_query_params_json(transcoded_request):
196 query_params = json.loads(
197 json_format.MessageToJson(
198 transcoded_request["query_params"],
199 use_integers_for_enums=True,
200 )
201 )
202 query_params.update(
203 _BaseFirestoreRestTransport._BaseBatchWrite._get_unset_required_fields(
204 query_params
205 )
206 )
207
208 query_params["$alt"] = "json;enum-encoding=int"
209 return query_params
210
211 class _BaseBeginTransaction:
212 def __hash__(self): # pragma: NO COVER
213 return NotImplementedError("__hash__ must be implemented.")
214
215 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
216
217 @classmethod
218 def _get_unset_required_fields(cls, message_dict):
219 return {
220 k: v
221 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
222 if k not in message_dict
223 }
224
225 @staticmethod
226 def _get_http_options():
227 http_options: List[Dict[str, str]] = [
228 {
229 "method": "post",
230 "uri": "/v1/{database=projects/*/databases/*}/documents:beginTransaction",
231 "body": "*",
232 },
233 ]
234 return http_options
235
236 @staticmethod
237 def _get_transcoded_request(http_options, request):
238 pb_request = firestore.BeginTransactionRequest.pb(request)
239 transcoded_request = path_template.transcode(http_options, pb_request)
240 return transcoded_request
241
242 @staticmethod
243 def _get_request_body_json(transcoded_request):
244 # Jsonify the request body
245
246 body = json_format.MessageToJson(
247 transcoded_request["body"], use_integers_for_enums=True
248 )
249 return body
250
251 @staticmethod
252 def _get_query_params_json(transcoded_request):
253 query_params = json.loads(
254 json_format.MessageToJson(
255 transcoded_request["query_params"],
256 use_integers_for_enums=True,
257 )
258 )
259 query_params.update(
260 _BaseFirestoreRestTransport._BaseBeginTransaction._get_unset_required_fields(
261 query_params
262 )
263 )
264
265 query_params["$alt"] = "json;enum-encoding=int"
266 return query_params
267
268 class _BaseCommit:
269 def __hash__(self): # pragma: NO COVER
270 return NotImplementedError("__hash__ must be implemented.")
271
272 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
273
274 @classmethod
275 def _get_unset_required_fields(cls, message_dict):
276 return {
277 k: v
278 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
279 if k not in message_dict
280 }
281
282 @staticmethod
283 def _get_http_options():
284 http_options: List[Dict[str, str]] = [
285 {
286 "method": "post",
287 "uri": "/v1/{database=projects/*/databases/*}/documents:commit",
288 "body": "*",
289 },
290 ]
291 return http_options
292
293 @staticmethod
294 def _get_transcoded_request(http_options, request):
295 pb_request = firestore.CommitRequest.pb(request)
296 transcoded_request = path_template.transcode(http_options, pb_request)
297 return transcoded_request
298
299 @staticmethod
300 def _get_request_body_json(transcoded_request):
301 # Jsonify the request body
302
303 body = json_format.MessageToJson(
304 transcoded_request["body"], use_integers_for_enums=True
305 )
306 return body
307
308 @staticmethod
309 def _get_query_params_json(transcoded_request):
310 query_params = json.loads(
311 json_format.MessageToJson(
312 transcoded_request["query_params"],
313 use_integers_for_enums=True,
314 )
315 )
316 query_params.update(
317 _BaseFirestoreRestTransport._BaseCommit._get_unset_required_fields(
318 query_params
319 )
320 )
321
322 query_params["$alt"] = "json;enum-encoding=int"
323 return query_params
324
325 class _BaseCreateDocument:
326 def __hash__(self): # pragma: NO COVER
327 return NotImplementedError("__hash__ must be implemented.")
328
329 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
330
331 @classmethod
332 def _get_unset_required_fields(cls, message_dict):
333 return {
334 k: v
335 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
336 if k not in message_dict
337 }
338
339 @staticmethod
340 def _get_http_options():
341 http_options: List[Dict[str, str]] = [
342 {
343 "method": "post",
344 "uri": "/v1/{parent=projects/*/databases/*/documents/**}/{collection_id}",
345 "body": "document",
346 },
347 ]
348 return http_options
349
350 @staticmethod
351 def _get_transcoded_request(http_options, request):
352 pb_request = firestore.CreateDocumentRequest.pb(request)
353 transcoded_request = path_template.transcode(http_options, pb_request)
354 return transcoded_request
355
356 @staticmethod
357 def _get_request_body_json(transcoded_request):
358 # Jsonify the request body
359
360 body = json_format.MessageToJson(
361 transcoded_request["body"], use_integers_for_enums=True
362 )
363 return body
364
365 @staticmethod
366 def _get_query_params_json(transcoded_request):
367 query_params = json.loads(
368 json_format.MessageToJson(
369 transcoded_request["query_params"],
370 use_integers_for_enums=True,
371 )
372 )
373 query_params.update(
374 _BaseFirestoreRestTransport._BaseCreateDocument._get_unset_required_fields(
375 query_params
376 )
377 )
378
379 query_params["$alt"] = "json;enum-encoding=int"
380 return query_params
381
382 class _BaseDeleteDocument:
383 def __hash__(self): # pragma: NO COVER
384 return NotImplementedError("__hash__ must be implemented.")
385
386 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
387
388 @classmethod
389 def _get_unset_required_fields(cls, message_dict):
390 return {
391 k: v
392 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
393 if k not in message_dict
394 }
395
396 @staticmethod
397 def _get_http_options():
398 http_options: List[Dict[str, str]] = [
399 {
400 "method": "delete",
401 "uri": "/v1/{name=projects/*/databases/*/documents/*/**}",
402 },
403 ]
404 return http_options
405
406 @staticmethod
407 def _get_transcoded_request(http_options, request):
408 pb_request = firestore.DeleteDocumentRequest.pb(request)
409 transcoded_request = path_template.transcode(http_options, pb_request)
410 return transcoded_request
411
412 @staticmethod
413 def _get_query_params_json(transcoded_request):
414 query_params = json.loads(
415 json_format.MessageToJson(
416 transcoded_request["query_params"],
417 use_integers_for_enums=True,
418 )
419 )
420 query_params.update(
421 _BaseFirestoreRestTransport._BaseDeleteDocument._get_unset_required_fields(
422 query_params
423 )
424 )
425
426 query_params["$alt"] = "json;enum-encoding=int"
427 return query_params
428
429 class _BaseGetDocument:
430 def __hash__(self): # pragma: NO COVER
431 return NotImplementedError("__hash__ must be implemented.")
432
433 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
434
435 @classmethod
436 def _get_unset_required_fields(cls, message_dict):
437 return {
438 k: v
439 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
440 if k not in message_dict
441 }
442
443 @staticmethod
444 def _get_http_options():
445 http_options: List[Dict[str, str]] = [
446 {
447 "method": "get",
448 "uri": "/v1/{name=projects/*/databases/*/documents/*/**}",
449 },
450 ]
451 return http_options
452
453 @staticmethod
454 def _get_transcoded_request(http_options, request):
455 pb_request = firestore.GetDocumentRequest.pb(request)
456 transcoded_request = path_template.transcode(http_options, pb_request)
457 return transcoded_request
458
459 @staticmethod
460 def _get_query_params_json(transcoded_request):
461 query_params = json.loads(
462 json_format.MessageToJson(
463 transcoded_request["query_params"],
464 use_integers_for_enums=True,
465 )
466 )
467 query_params.update(
468 _BaseFirestoreRestTransport._BaseGetDocument._get_unset_required_fields(
469 query_params
470 )
471 )
472
473 query_params["$alt"] = "json;enum-encoding=int"
474 return query_params
475
476 class _BaseListCollectionIds:
477 def __hash__(self): # pragma: NO COVER
478 return NotImplementedError("__hash__ must be implemented.")
479
480 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
481
482 @classmethod
483 def _get_unset_required_fields(cls, message_dict):
484 return {
485 k: v
486 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
487 if k not in message_dict
488 }
489
490 @staticmethod
491 def _get_http_options():
492 http_options: List[Dict[str, str]] = [
493 {
494 "method": "post",
495 "uri": "/v1/{parent=projects/*/databases/*/documents}:listCollectionIds",
496 "body": "*",
497 },
498 {
499 "method": "post",
500 "uri": "/v1/{parent=projects/*/databases/*/documents/*/**}:listCollectionIds",
501 "body": "*",
502 },
503 ]
504 return http_options
505
506 @staticmethod
507 def _get_transcoded_request(http_options, request):
508 pb_request = firestore.ListCollectionIdsRequest.pb(request)
509 transcoded_request = path_template.transcode(http_options, pb_request)
510 return transcoded_request
511
512 @staticmethod
513 def _get_request_body_json(transcoded_request):
514 # Jsonify the request body
515
516 body = json_format.MessageToJson(
517 transcoded_request["body"], use_integers_for_enums=True
518 )
519 return body
520
521 @staticmethod
522 def _get_query_params_json(transcoded_request):
523 query_params = json.loads(
524 json_format.MessageToJson(
525 transcoded_request["query_params"],
526 use_integers_for_enums=True,
527 )
528 )
529 query_params.update(
530 _BaseFirestoreRestTransport._BaseListCollectionIds._get_unset_required_fields(
531 query_params
532 )
533 )
534
535 query_params["$alt"] = "json;enum-encoding=int"
536 return query_params
537
538 class _BaseListDocuments:
539 def __hash__(self): # pragma: NO COVER
540 return NotImplementedError("__hash__ must be implemented.")
541
542 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
543
544 @classmethod
545 def _get_unset_required_fields(cls, message_dict):
546 return {
547 k: v
548 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
549 if k not in message_dict
550 }
551
552 @staticmethod
553 def _get_http_options():
554 http_options: List[Dict[str, str]] = [
555 {
556 "method": "get",
557 "uri": "/v1/{parent=projects/*/databases/*/documents/*/**}/{collection_id}",
558 },
559 {
560 "method": "get",
561 "uri": "/v1/{parent=projects/*/databases/*/documents}/{collection_id}",
562 },
563 ]
564 return http_options
565
566 @staticmethod
567 def _get_transcoded_request(http_options, request):
568 pb_request = firestore.ListDocumentsRequest.pb(request)
569 transcoded_request = path_template.transcode(http_options, pb_request)
570 return transcoded_request
571
572 @staticmethod
573 def _get_query_params_json(transcoded_request):
574 query_params = json.loads(
575 json_format.MessageToJson(
576 transcoded_request["query_params"],
577 use_integers_for_enums=True,
578 )
579 )
580 query_params.update(
581 _BaseFirestoreRestTransport._BaseListDocuments._get_unset_required_fields(
582 query_params
583 )
584 )
585
586 query_params["$alt"] = "json;enum-encoding=int"
587 return query_params
588
589 class _BaseListen:
590 def __hash__(self): # pragma: NO COVER
591 return NotImplementedError("__hash__ must be implemented.")
592
593 class _BasePartitionQuery:
594 def __hash__(self): # pragma: NO COVER
595 return NotImplementedError("__hash__ must be implemented.")
596
597 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
598
599 @classmethod
600 def _get_unset_required_fields(cls, message_dict):
601 return {
602 k: v
603 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
604 if k not in message_dict
605 }
606
607 @staticmethod
608 def _get_http_options():
609 http_options: List[Dict[str, str]] = [
610 {
611 "method": "post",
612 "uri": "/v1/{parent=projects/*/databases/*/documents}:partitionQuery",
613 "body": "*",
614 },
615 {
616 "method": "post",
617 "uri": "/v1/{parent=projects/*/databases/*/documents/*/**}:partitionQuery",
618 "body": "*",
619 },
620 ]
621 return http_options
622
623 @staticmethod
624 def _get_transcoded_request(http_options, request):
625 pb_request = firestore.PartitionQueryRequest.pb(request)
626 transcoded_request = path_template.transcode(http_options, pb_request)
627 return transcoded_request
628
629 @staticmethod
630 def _get_request_body_json(transcoded_request):
631 # Jsonify the request body
632
633 body = json_format.MessageToJson(
634 transcoded_request["body"], use_integers_for_enums=True
635 )
636 return body
637
638 @staticmethod
639 def _get_query_params_json(transcoded_request):
640 query_params = json.loads(
641 json_format.MessageToJson(
642 transcoded_request["query_params"],
643 use_integers_for_enums=True,
644 )
645 )
646 query_params.update(
647 _BaseFirestoreRestTransport._BasePartitionQuery._get_unset_required_fields(
648 query_params
649 )
650 )
651
652 query_params["$alt"] = "json;enum-encoding=int"
653 return query_params
654
655 class _BaseRollback:
656 def __hash__(self): # pragma: NO COVER
657 return NotImplementedError("__hash__ must be implemented.")
658
659 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
660
661 @classmethod
662 def _get_unset_required_fields(cls, message_dict):
663 return {
664 k: v
665 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
666 if k not in message_dict
667 }
668
669 @staticmethod
670 def _get_http_options():
671 http_options: List[Dict[str, str]] = [
672 {
673 "method": "post",
674 "uri": "/v1/{database=projects/*/databases/*}/documents:rollback",
675 "body": "*",
676 },
677 ]
678 return http_options
679
680 @staticmethod
681 def _get_transcoded_request(http_options, request):
682 pb_request = firestore.RollbackRequest.pb(request)
683 transcoded_request = path_template.transcode(http_options, pb_request)
684 return transcoded_request
685
686 @staticmethod
687 def _get_request_body_json(transcoded_request):
688 # Jsonify the request body
689
690 body = json_format.MessageToJson(
691 transcoded_request["body"], use_integers_for_enums=True
692 )
693 return body
694
695 @staticmethod
696 def _get_query_params_json(transcoded_request):
697 query_params = json.loads(
698 json_format.MessageToJson(
699 transcoded_request["query_params"],
700 use_integers_for_enums=True,
701 )
702 )
703 query_params.update(
704 _BaseFirestoreRestTransport._BaseRollback._get_unset_required_fields(
705 query_params
706 )
707 )
708
709 query_params["$alt"] = "json;enum-encoding=int"
710 return query_params
711
712 class _BaseRunAggregationQuery:
713 def __hash__(self): # pragma: NO COVER
714 return NotImplementedError("__hash__ must be implemented.")
715
716 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
717
718 @classmethod
719 def _get_unset_required_fields(cls, message_dict):
720 return {
721 k: v
722 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
723 if k not in message_dict
724 }
725
726 @staticmethod
727 def _get_http_options():
728 http_options: List[Dict[str, str]] = [
729 {
730 "method": "post",
731 "uri": "/v1/{parent=projects/*/databases/*/documents}:runAggregationQuery",
732 "body": "*",
733 },
734 {
735 "method": "post",
736 "uri": "/v1/{parent=projects/*/databases/*/documents/*/**}:runAggregationQuery",
737 "body": "*",
738 },
739 ]
740 return http_options
741
742 @staticmethod
743 def _get_transcoded_request(http_options, request):
744 pb_request = firestore.RunAggregationQueryRequest.pb(request)
745 transcoded_request = path_template.transcode(http_options, pb_request)
746 return transcoded_request
747
748 @staticmethod
749 def _get_request_body_json(transcoded_request):
750 # Jsonify the request body
751
752 body = json_format.MessageToJson(
753 transcoded_request["body"], use_integers_for_enums=True
754 )
755 return body
756
757 @staticmethod
758 def _get_query_params_json(transcoded_request):
759 query_params = json.loads(
760 json_format.MessageToJson(
761 transcoded_request["query_params"],
762 use_integers_for_enums=True,
763 )
764 )
765 query_params.update(
766 _BaseFirestoreRestTransport._BaseRunAggregationQuery._get_unset_required_fields(
767 query_params
768 )
769 )
770
771 query_params["$alt"] = "json;enum-encoding=int"
772 return query_params
773
774 class _BaseRunQuery:
775 def __hash__(self): # pragma: NO COVER
776 return NotImplementedError("__hash__ must be implemented.")
777
778 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
779
780 @classmethod
781 def _get_unset_required_fields(cls, message_dict):
782 return {
783 k: v
784 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
785 if k not in message_dict
786 }
787
788 @staticmethod
789 def _get_http_options():
790 http_options: List[Dict[str, str]] = [
791 {
792 "method": "post",
793 "uri": "/v1/{parent=projects/*/databases/*/documents}:runQuery",
794 "body": "*",
795 },
796 {
797 "method": "post",
798 "uri": "/v1/{parent=projects/*/databases/*/documents/*/**}:runQuery",
799 "body": "*",
800 },
801 ]
802 return http_options
803
804 @staticmethod
805 def _get_transcoded_request(http_options, request):
806 pb_request = firestore.RunQueryRequest.pb(request)
807 transcoded_request = path_template.transcode(http_options, pb_request)
808 return transcoded_request
809
810 @staticmethod
811 def _get_request_body_json(transcoded_request):
812 # Jsonify the request body
813
814 body = json_format.MessageToJson(
815 transcoded_request["body"], use_integers_for_enums=True
816 )
817 return body
818
819 @staticmethod
820 def _get_query_params_json(transcoded_request):
821 query_params = json.loads(
822 json_format.MessageToJson(
823 transcoded_request["query_params"],
824 use_integers_for_enums=True,
825 )
826 )
827 query_params.update(
828 _BaseFirestoreRestTransport._BaseRunQuery._get_unset_required_fields(
829 query_params
830 )
831 )
832
833 query_params["$alt"] = "json;enum-encoding=int"
834 return query_params
835
836 class _BaseUpdateDocument:
837 def __hash__(self): # pragma: NO COVER
838 return NotImplementedError("__hash__ must be implemented.")
839
840 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
841
842 @classmethod
843 def _get_unset_required_fields(cls, message_dict):
844 return {
845 k: v
846 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
847 if k not in message_dict
848 }
849
850 @staticmethod
851 def _get_http_options():
852 http_options: List[Dict[str, str]] = [
853 {
854 "method": "patch",
855 "uri": "/v1/{document.name=projects/*/databases/*/documents/*/**}",
856 "body": "document",
857 },
858 ]
859 return http_options
860
861 @staticmethod
862 def _get_transcoded_request(http_options, request):
863 pb_request = firestore.UpdateDocumentRequest.pb(request)
864 transcoded_request = path_template.transcode(http_options, pb_request)
865 return transcoded_request
866
867 @staticmethod
868 def _get_request_body_json(transcoded_request):
869 # Jsonify the request body
870
871 body = json_format.MessageToJson(
872 transcoded_request["body"], use_integers_for_enums=True
873 )
874 return body
875
876 @staticmethod
877 def _get_query_params_json(transcoded_request):
878 query_params = json.loads(
879 json_format.MessageToJson(
880 transcoded_request["query_params"],
881 use_integers_for_enums=True,
882 )
883 )
884 query_params.update(
885 _BaseFirestoreRestTransport._BaseUpdateDocument._get_unset_required_fields(
886 query_params
887 )
888 )
889
890 query_params["$alt"] = "json;enum-encoding=int"
891 return query_params
892
893 class _BaseWrite:
894 def __hash__(self): # pragma: NO COVER
895 return NotImplementedError("__hash__ must be implemented.")
896
897 class _BaseCancelOperation:
898 def __hash__(self): # pragma: NO COVER
899 return NotImplementedError("__hash__ must be implemented.")
900
901 @staticmethod
902 def _get_http_options():
903 http_options: List[Dict[str, str]] = [
904 {
905 "method": "post",
906 "uri": "/v1/{name=projects/*/databases/*/operations/*}:cancel",
907 "body": "*",
908 },
909 ]
910 return http_options
911
912 @staticmethod
913 def _get_transcoded_request(http_options, request):
914 request_kwargs = json_format.MessageToDict(request)
915 transcoded_request = path_template.transcode(http_options, **request_kwargs)
916 return transcoded_request
917
918 @staticmethod
919 def _get_request_body_json(transcoded_request):
920 body = json.dumps(transcoded_request["body"])
921 return body
922
923 @staticmethod
924 def _get_query_params_json(transcoded_request):
925 query_params = json.loads(json.dumps(transcoded_request["query_params"]))
926 return query_params
927
928 class _BaseDeleteOperation:
929 def __hash__(self): # pragma: NO COVER
930 return NotImplementedError("__hash__ must be implemented.")
931
932 @staticmethod
933 def _get_http_options():
934 http_options: List[Dict[str, str]] = [
935 {
936 "method": "delete",
937 "uri": "/v1/{name=projects/*/databases/*/operations/*}",
938 },
939 ]
940 return http_options
941
942 @staticmethod
943 def _get_transcoded_request(http_options, request):
944 request_kwargs = json_format.MessageToDict(request)
945 transcoded_request = path_template.transcode(http_options, **request_kwargs)
946 return transcoded_request
947
948 @staticmethod
949 def _get_query_params_json(transcoded_request):
950 query_params = json.loads(json.dumps(transcoded_request["query_params"]))
951 return query_params
952
953 class _BaseGetOperation:
954 def __hash__(self): # pragma: NO COVER
955 return NotImplementedError("__hash__ must be implemented.")
956
957 @staticmethod
958 def _get_http_options():
959 http_options: List[Dict[str, str]] = [
960 {
961 "method": "get",
962 "uri": "/v1/{name=projects/*/databases/*/operations/*}",
963 },
964 ]
965 return http_options
966
967 @staticmethod
968 def _get_transcoded_request(http_options, request):
969 request_kwargs = json_format.MessageToDict(request)
970 transcoded_request = path_template.transcode(http_options, **request_kwargs)
971 return transcoded_request
972
973 @staticmethod
974 def _get_query_params_json(transcoded_request):
975 query_params = json.loads(json.dumps(transcoded_request["query_params"]))
976 return query_params
977
978 class _BaseListOperations:
979 def __hash__(self): # pragma: NO COVER
980 return NotImplementedError("__hash__ must be implemented.")
981
982 @staticmethod
983 def _get_http_options():
984 http_options: List[Dict[str, str]] = [
985 {
986 "method": "get",
987 "uri": "/v1/{name=projects/*/databases/*}/operations",
988 },
989 ]
990 return http_options
991
992 @staticmethod
993 def _get_transcoded_request(http_options, request):
994 request_kwargs = json_format.MessageToDict(request)
995 transcoded_request = path_template.transcode(http_options, **request_kwargs)
996 return transcoded_request
997
998 @staticmethod
999 def _get_query_params_json(transcoded_request):
1000 query_params = json.loads(json.dumps(transcoded_request["query_params"]))
1001 return query_params
1002
1003
1004__all__ = ("_BaseFirestoreRestTransport",)