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 _BaseExecutePipeline:
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": "post",
448 "uri": "/v1/{database=projects/*/databases/*}/documents:executePipeline",
449 "body": "*",
450 },
451 ]
452 return http_options
453
454 @staticmethod
455 def _get_transcoded_request(http_options, request):
456 pb_request = firestore.ExecutePipelineRequest.pb(request)
457 transcoded_request = path_template.transcode(http_options, pb_request)
458 return transcoded_request
459
460 @staticmethod
461 def _get_request_body_json(transcoded_request):
462 # Jsonify the request body
463
464 body = json_format.MessageToJson(
465 transcoded_request["body"], use_integers_for_enums=True
466 )
467 return body
468
469 @staticmethod
470 def _get_query_params_json(transcoded_request):
471 query_params = json.loads(
472 json_format.MessageToJson(
473 transcoded_request["query_params"],
474 use_integers_for_enums=True,
475 )
476 )
477 query_params.update(
478 _BaseFirestoreRestTransport._BaseExecutePipeline._get_unset_required_fields(
479 query_params
480 )
481 )
482
483 query_params["$alt"] = "json;enum-encoding=int"
484 return query_params
485
486 class _BaseGetDocument:
487 def __hash__(self): # pragma: NO COVER
488 return NotImplementedError("__hash__ must be implemented.")
489
490 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
491
492 @classmethod
493 def _get_unset_required_fields(cls, message_dict):
494 return {
495 k: v
496 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
497 if k not in message_dict
498 }
499
500 @staticmethod
501 def _get_http_options():
502 http_options: List[Dict[str, str]] = [
503 {
504 "method": "get",
505 "uri": "/v1/{name=projects/*/databases/*/documents/*/**}",
506 },
507 ]
508 return http_options
509
510 @staticmethod
511 def _get_transcoded_request(http_options, request):
512 pb_request = firestore.GetDocumentRequest.pb(request)
513 transcoded_request = path_template.transcode(http_options, pb_request)
514 return transcoded_request
515
516 @staticmethod
517 def _get_query_params_json(transcoded_request):
518 query_params = json.loads(
519 json_format.MessageToJson(
520 transcoded_request["query_params"],
521 use_integers_for_enums=True,
522 )
523 )
524 query_params.update(
525 _BaseFirestoreRestTransport._BaseGetDocument._get_unset_required_fields(
526 query_params
527 )
528 )
529
530 query_params["$alt"] = "json;enum-encoding=int"
531 return query_params
532
533 class _BaseListCollectionIds:
534 def __hash__(self): # pragma: NO COVER
535 return NotImplementedError("__hash__ must be implemented.")
536
537 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
538
539 @classmethod
540 def _get_unset_required_fields(cls, message_dict):
541 return {
542 k: v
543 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
544 if k not in message_dict
545 }
546
547 @staticmethod
548 def _get_http_options():
549 http_options: List[Dict[str, str]] = [
550 {
551 "method": "post",
552 "uri": "/v1/{parent=projects/*/databases/*/documents}:listCollectionIds",
553 "body": "*",
554 },
555 {
556 "method": "post",
557 "uri": "/v1/{parent=projects/*/databases/*/documents/*/**}:listCollectionIds",
558 "body": "*",
559 },
560 ]
561 return http_options
562
563 @staticmethod
564 def _get_transcoded_request(http_options, request):
565 pb_request = firestore.ListCollectionIdsRequest.pb(request)
566 transcoded_request = path_template.transcode(http_options, pb_request)
567 return transcoded_request
568
569 @staticmethod
570 def _get_request_body_json(transcoded_request):
571 # Jsonify the request body
572
573 body = json_format.MessageToJson(
574 transcoded_request["body"], use_integers_for_enums=True
575 )
576 return body
577
578 @staticmethod
579 def _get_query_params_json(transcoded_request):
580 query_params = json.loads(
581 json_format.MessageToJson(
582 transcoded_request["query_params"],
583 use_integers_for_enums=True,
584 )
585 )
586 query_params.update(
587 _BaseFirestoreRestTransport._BaseListCollectionIds._get_unset_required_fields(
588 query_params
589 )
590 )
591
592 query_params["$alt"] = "json;enum-encoding=int"
593 return query_params
594
595 class _BaseListDocuments:
596 def __hash__(self): # pragma: NO COVER
597 return NotImplementedError("__hash__ must be implemented.")
598
599 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
600
601 @classmethod
602 def _get_unset_required_fields(cls, message_dict):
603 return {
604 k: v
605 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
606 if k not in message_dict
607 }
608
609 @staticmethod
610 def _get_http_options():
611 http_options: List[Dict[str, str]] = [
612 {
613 "method": "get",
614 "uri": "/v1/{parent=projects/*/databases/*/documents/*/**}/{collection_id}",
615 },
616 {
617 "method": "get",
618 "uri": "/v1/{parent=projects/*/databases/*/documents}/{collection_id}",
619 },
620 ]
621 return http_options
622
623 @staticmethod
624 def _get_transcoded_request(http_options, request):
625 pb_request = firestore.ListDocumentsRequest.pb(request)
626 transcoded_request = path_template.transcode(http_options, pb_request)
627 return transcoded_request
628
629 @staticmethod
630 def _get_query_params_json(transcoded_request):
631 query_params = json.loads(
632 json_format.MessageToJson(
633 transcoded_request["query_params"],
634 use_integers_for_enums=True,
635 )
636 )
637 query_params.update(
638 _BaseFirestoreRestTransport._BaseListDocuments._get_unset_required_fields(
639 query_params
640 )
641 )
642
643 query_params["$alt"] = "json;enum-encoding=int"
644 return query_params
645
646 class _BaseListen:
647 def __hash__(self): # pragma: NO COVER
648 return NotImplementedError("__hash__ must be implemented.")
649
650 class _BasePartitionQuery:
651 def __hash__(self): # pragma: NO COVER
652 return NotImplementedError("__hash__ must be implemented.")
653
654 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
655
656 @classmethod
657 def _get_unset_required_fields(cls, message_dict):
658 return {
659 k: v
660 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
661 if k not in message_dict
662 }
663
664 @staticmethod
665 def _get_http_options():
666 http_options: List[Dict[str, str]] = [
667 {
668 "method": "post",
669 "uri": "/v1/{parent=projects/*/databases/*/documents}:partitionQuery",
670 "body": "*",
671 },
672 {
673 "method": "post",
674 "uri": "/v1/{parent=projects/*/databases/*/documents/*/**}:partitionQuery",
675 "body": "*",
676 },
677 ]
678 return http_options
679
680 @staticmethod
681 def _get_transcoded_request(http_options, request):
682 pb_request = firestore.PartitionQueryRequest.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._BasePartitionQuery._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 _BaseRollback:
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/{database=projects/*/databases/*}/documents:rollback",
732 "body": "*",
733 },
734 ]
735 return http_options
736
737 @staticmethod
738 def _get_transcoded_request(http_options, request):
739 pb_request = firestore.RollbackRequest.pb(request)
740 transcoded_request = path_template.transcode(http_options, pb_request)
741 return transcoded_request
742
743 @staticmethod
744 def _get_request_body_json(transcoded_request):
745 # Jsonify the request body
746
747 body = json_format.MessageToJson(
748 transcoded_request["body"], use_integers_for_enums=True
749 )
750 return body
751
752 @staticmethod
753 def _get_query_params_json(transcoded_request):
754 query_params = json.loads(
755 json_format.MessageToJson(
756 transcoded_request["query_params"],
757 use_integers_for_enums=True,
758 )
759 )
760 query_params.update(
761 _BaseFirestoreRestTransport._BaseRollback._get_unset_required_fields(
762 query_params
763 )
764 )
765
766 query_params["$alt"] = "json;enum-encoding=int"
767 return query_params
768
769 class _BaseRunAggregationQuery:
770 def __hash__(self): # pragma: NO COVER
771 return NotImplementedError("__hash__ must be implemented.")
772
773 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
774
775 @classmethod
776 def _get_unset_required_fields(cls, message_dict):
777 return {
778 k: v
779 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
780 if k not in message_dict
781 }
782
783 @staticmethod
784 def _get_http_options():
785 http_options: List[Dict[str, str]] = [
786 {
787 "method": "post",
788 "uri": "/v1/{parent=projects/*/databases/*/documents}:runAggregationQuery",
789 "body": "*",
790 },
791 {
792 "method": "post",
793 "uri": "/v1/{parent=projects/*/databases/*/documents/*/**}:runAggregationQuery",
794 "body": "*",
795 },
796 ]
797 return http_options
798
799 @staticmethod
800 def _get_transcoded_request(http_options, request):
801 pb_request = firestore.RunAggregationQueryRequest.pb(request)
802 transcoded_request = path_template.transcode(http_options, pb_request)
803 return transcoded_request
804
805 @staticmethod
806 def _get_request_body_json(transcoded_request):
807 # Jsonify the request body
808
809 body = json_format.MessageToJson(
810 transcoded_request["body"], use_integers_for_enums=True
811 )
812 return body
813
814 @staticmethod
815 def _get_query_params_json(transcoded_request):
816 query_params = json.loads(
817 json_format.MessageToJson(
818 transcoded_request["query_params"],
819 use_integers_for_enums=True,
820 )
821 )
822 query_params.update(
823 _BaseFirestoreRestTransport._BaseRunAggregationQuery._get_unset_required_fields(
824 query_params
825 )
826 )
827
828 query_params["$alt"] = "json;enum-encoding=int"
829 return query_params
830
831 class _BaseRunQuery:
832 def __hash__(self): # pragma: NO COVER
833 return NotImplementedError("__hash__ must be implemented.")
834
835 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
836
837 @classmethod
838 def _get_unset_required_fields(cls, message_dict):
839 return {
840 k: v
841 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
842 if k not in message_dict
843 }
844
845 @staticmethod
846 def _get_http_options():
847 http_options: List[Dict[str, str]] = [
848 {
849 "method": "post",
850 "uri": "/v1/{parent=projects/*/databases/*/documents}:runQuery",
851 "body": "*",
852 },
853 {
854 "method": "post",
855 "uri": "/v1/{parent=projects/*/databases/*/documents/*/**}:runQuery",
856 "body": "*",
857 },
858 ]
859 return http_options
860
861 @staticmethod
862 def _get_transcoded_request(http_options, request):
863 pb_request = firestore.RunQueryRequest.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._BaseRunQuery._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 _BaseUpdateDocument:
894 def __hash__(self): # pragma: NO COVER
895 return NotImplementedError("__hash__ must be implemented.")
896
897 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {}
898
899 @classmethod
900 def _get_unset_required_fields(cls, message_dict):
901 return {
902 k: v
903 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items()
904 if k not in message_dict
905 }
906
907 @staticmethod
908 def _get_http_options():
909 http_options: List[Dict[str, str]] = [
910 {
911 "method": "patch",
912 "uri": "/v1/{document.name=projects/*/databases/*/documents/*/**}",
913 "body": "document",
914 },
915 ]
916 return http_options
917
918 @staticmethod
919 def _get_transcoded_request(http_options, request):
920 pb_request = firestore.UpdateDocumentRequest.pb(request)
921 transcoded_request = path_template.transcode(http_options, pb_request)
922 return transcoded_request
923
924 @staticmethod
925 def _get_request_body_json(transcoded_request):
926 # Jsonify the request body
927
928 body = json_format.MessageToJson(
929 transcoded_request["body"], use_integers_for_enums=True
930 )
931 return body
932
933 @staticmethod
934 def _get_query_params_json(transcoded_request):
935 query_params = json.loads(
936 json_format.MessageToJson(
937 transcoded_request["query_params"],
938 use_integers_for_enums=True,
939 )
940 )
941 query_params.update(
942 _BaseFirestoreRestTransport._BaseUpdateDocument._get_unset_required_fields(
943 query_params
944 )
945 )
946
947 query_params["$alt"] = "json;enum-encoding=int"
948 return query_params
949
950 class _BaseWrite:
951 def __hash__(self): # pragma: NO COVER
952 return NotImplementedError("__hash__ must be implemented.")
953
954 class _BaseCancelOperation:
955 def __hash__(self): # pragma: NO COVER
956 return NotImplementedError("__hash__ must be implemented.")
957
958 @staticmethod
959 def _get_http_options():
960 http_options: List[Dict[str, str]] = [
961 {
962 "method": "post",
963 "uri": "/v1/{name=projects/*/databases/*/operations/*}:cancel",
964 "body": "*",
965 },
966 ]
967 return http_options
968
969 @staticmethod
970 def _get_transcoded_request(http_options, request):
971 request_kwargs = json_format.MessageToDict(request)
972 transcoded_request = path_template.transcode(http_options, **request_kwargs)
973 return transcoded_request
974
975 @staticmethod
976 def _get_request_body_json(transcoded_request):
977 body = json.dumps(transcoded_request["body"])
978 return body
979
980 @staticmethod
981 def _get_query_params_json(transcoded_request):
982 query_params = json.loads(json.dumps(transcoded_request["query_params"]))
983 return query_params
984
985 class _BaseDeleteOperation:
986 def __hash__(self): # pragma: NO COVER
987 return NotImplementedError("__hash__ must be implemented.")
988
989 @staticmethod
990 def _get_http_options():
991 http_options: List[Dict[str, str]] = [
992 {
993 "method": "delete",
994 "uri": "/v1/{name=projects/*/databases/*/operations/*}",
995 },
996 ]
997 return http_options
998
999 @staticmethod
1000 def _get_transcoded_request(http_options, request):
1001 request_kwargs = json_format.MessageToDict(request)
1002 transcoded_request = path_template.transcode(http_options, **request_kwargs)
1003 return transcoded_request
1004
1005 @staticmethod
1006 def _get_query_params_json(transcoded_request):
1007 query_params = json.loads(json.dumps(transcoded_request["query_params"]))
1008 return query_params
1009
1010 class _BaseGetOperation:
1011 def __hash__(self): # pragma: NO COVER
1012 return NotImplementedError("__hash__ must be implemented.")
1013
1014 @staticmethod
1015 def _get_http_options():
1016 http_options: List[Dict[str, str]] = [
1017 {
1018 "method": "get",
1019 "uri": "/v1/{name=projects/*/databases/*/operations/*}",
1020 },
1021 ]
1022 return http_options
1023
1024 @staticmethod
1025 def _get_transcoded_request(http_options, request):
1026 request_kwargs = json_format.MessageToDict(request)
1027 transcoded_request = path_template.transcode(http_options, **request_kwargs)
1028 return transcoded_request
1029
1030 @staticmethod
1031 def _get_query_params_json(transcoded_request):
1032 query_params = json.loads(json.dumps(transcoded_request["query_params"]))
1033 return query_params
1034
1035 class _BaseListOperations:
1036 def __hash__(self): # pragma: NO COVER
1037 return NotImplementedError("__hash__ must be implemented.")
1038
1039 @staticmethod
1040 def _get_http_options():
1041 http_options: List[Dict[str, str]] = [
1042 {
1043 "method": "get",
1044 "uri": "/v1/{name=projects/*/databases/*}/operations",
1045 },
1046 ]
1047 return http_options
1048
1049 @staticmethod
1050 def _get_transcoded_request(http_options, request):
1051 request_kwargs = json_format.MessageToDict(request)
1052 transcoded_request = path_template.transcode(http_options, **request_kwargs)
1053 return transcoded_request
1054
1055 @staticmethod
1056 def _get_query_params_json(transcoded_request):
1057 query_params = json.loads(json.dumps(transcoded_request["query_params"]))
1058 return query_params
1059
1060
1061__all__ = ("_BaseFirestoreRestTransport",)