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 abc 
    17from typing import Awaitable, Callable, Dict, Optional, Sequence, Union 
    18 
    19from google.cloud.firestore_v1 import gapic_version as package_version 
    20 
    21import google.auth  # type: ignore 
    22import google.api_core 
    23from google.api_core import exceptions as core_exceptions 
    24from google.api_core import gapic_v1 
    25from google.api_core import retry as retries 
    26from google.auth import credentials as ga_credentials  # type: ignore 
    27from google.oauth2 import service_account  # type: ignore 
    28import google.protobuf 
    29 
    30from google.cloud.firestore_v1.types import document 
    31from google.cloud.firestore_v1.types import document as gf_document 
    32from google.cloud.firestore_v1.types import firestore 
    33from google.cloud.location import locations_pb2  # type: ignore 
    34from google.longrunning import operations_pb2  # type: ignore 
    35from google.protobuf import empty_pb2  # type: ignore 
    36 
    37DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( 
    38    gapic_version=package_version.__version__ 
    39) 
    40 
    41if hasattr(DEFAULT_CLIENT_INFO, "protobuf_runtime_version"):  # pragma: NO COVER 
    42    DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__ 
    43 
    44 
    45class FirestoreTransport(abc.ABC): 
    46    """Abstract transport class for Firestore.""" 
    47 
    48    AUTH_SCOPES = ( 
    49        "https://www.googleapis.com/auth/cloud-platform", 
    50        "https://www.googleapis.com/auth/datastore", 
    51    ) 
    52 
    53    DEFAULT_HOST: str = "firestore.googleapis.com" 
    54 
    55    def __init__( 
    56        self, 
    57        *, 
    58        host: str = DEFAULT_HOST, 
    59        credentials: Optional[ga_credentials.Credentials] = None, 
    60        credentials_file: Optional[str] = None, 
    61        scopes: Optional[Sequence[str]] = None, 
    62        quota_project_id: Optional[str] = None, 
    63        client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, 
    64        always_use_jwt_access: Optional[bool] = False, 
    65        api_audience: Optional[str] = None, 
    66        **kwargs, 
    67    ) -> None: 
    68        """Instantiate the transport. 
    69 
    70        Args: 
    71            host (Optional[str]): 
    72                 The hostname to connect to (default: 'firestore.googleapis.com'). 
    73            credentials (Optional[google.auth.credentials.Credentials]): The 
    74                authorization credentials to attach to requests. These 
    75                credentials identify the application to the service; if none 
    76                are specified, the client will attempt to ascertain the 
    77                credentials from the environment. 
    78            credentials_file (Optional[str]): A file with credentials that can 
    79                be loaded with :func:`google.auth.load_credentials_from_file`. 
    80                This argument is mutually exclusive with credentials. 
    81            scopes (Optional[Sequence[str]]): A list of scopes. 
    82            quota_project_id (Optional[str]): An optional project to use for billing 
    83                and quota. 
    84            client_info (google.api_core.gapic_v1.client_info.ClientInfo): 
    85                The client info used to send a user-agent string along with 
    86                API requests. If ``None``, then default info will be used. 
    87                Generally, you only need to set this if you're developing 
    88                your own client library. 
    89            always_use_jwt_access (Optional[bool]): Whether self signed JWT should 
    90                be used for service account credentials. 
    91        """ 
    92 
    93        scopes_kwargs = {"scopes": scopes, "default_scopes": self.AUTH_SCOPES} 
    94 
    95        # Save the scopes. 
    96        self._scopes = scopes 
    97        if not hasattr(self, "_ignore_credentials"): 
    98            self._ignore_credentials: bool = False 
    99 
    100        # If no credentials are provided, then determine the appropriate 
    101        # defaults. 
    102        if credentials and credentials_file: 
    103            raise core_exceptions.DuplicateCredentialArgs( 
    104                "'credentials_file' and 'credentials' are mutually exclusive" 
    105            ) 
    106 
    107        if credentials_file is not None: 
    108            credentials, _ = google.auth.load_credentials_from_file( 
    109                credentials_file, **scopes_kwargs, quota_project_id=quota_project_id 
    110            ) 
    111        elif credentials is None and not self._ignore_credentials: 
    112            credentials, _ = google.auth.default( 
    113                **scopes_kwargs, quota_project_id=quota_project_id 
    114            ) 
    115            # Don't apply audience if the credentials file passed from user. 
    116            if hasattr(credentials, "with_gdch_audience"): 
    117                credentials = credentials.with_gdch_audience( 
    118                    api_audience if api_audience else host 
    119                ) 
    120 
    121        # If the credentials are service account credentials, then always try to use self signed JWT. 
    122        if ( 
    123            always_use_jwt_access 
    124            and isinstance(credentials, service_account.Credentials) 
    125            and hasattr(service_account.Credentials, "with_always_use_jwt_access") 
    126        ): 
    127            credentials = credentials.with_always_use_jwt_access(True) 
    128 
    129        # Save the credentials. 
    130        self._credentials = credentials 
    131 
    132        # Save the hostname. Default to port 443 (HTTPS) if none is specified. 
    133        if ":" not in host: 
    134            host += ":443" 
    135        self._host = host 
    136 
    137    @property 
    138    def host(self): 
    139        return self._host 
    140 
    141    def _prep_wrapped_messages(self, client_info): 
    142        # Precompute the wrapped methods. 
    143        self._wrapped_methods = { 
    144            self.get_document: gapic_v1.method.wrap_method( 
    145                self.get_document, 
    146                default_retry=retries.Retry( 
    147                    initial=0.1, 
    148                    maximum=60.0, 
    149                    multiplier=1.3, 
    150                    predicate=retries.if_exception_type( 
    151                        core_exceptions.DeadlineExceeded, 
    152                        core_exceptions.InternalServerError, 
    153                        core_exceptions.ResourceExhausted, 
    154                        core_exceptions.ServiceUnavailable, 
    155                    ), 
    156                    deadline=60.0, 
    157                ), 
    158                default_timeout=60.0, 
    159                client_info=client_info, 
    160            ), 
    161            self.list_documents: gapic_v1.method.wrap_method( 
    162                self.list_documents, 
    163                default_retry=retries.Retry( 
    164                    initial=0.1, 
    165                    maximum=60.0, 
    166                    multiplier=1.3, 
    167                    predicate=retries.if_exception_type( 
    168                        core_exceptions.DeadlineExceeded, 
    169                        core_exceptions.InternalServerError, 
    170                        core_exceptions.ResourceExhausted, 
    171                        core_exceptions.ServiceUnavailable, 
    172                    ), 
    173                    deadline=60.0, 
    174                ), 
    175                default_timeout=60.0, 
    176                client_info=client_info, 
    177            ), 
    178            self.update_document: gapic_v1.method.wrap_method( 
    179                self.update_document, 
    180                default_retry=retries.Retry( 
    181                    initial=0.1, 
    182                    maximum=60.0, 
    183                    multiplier=1.3, 
    184                    predicate=retries.if_exception_type( 
    185                        core_exceptions.ResourceExhausted, 
    186                        core_exceptions.ServiceUnavailable, 
    187                    ), 
    188                    deadline=60.0, 
    189                ), 
    190                default_timeout=60.0, 
    191                client_info=client_info, 
    192            ), 
    193            self.delete_document: gapic_v1.method.wrap_method( 
    194                self.delete_document, 
    195                default_retry=retries.Retry( 
    196                    initial=0.1, 
    197                    maximum=60.0, 
    198                    multiplier=1.3, 
    199                    predicate=retries.if_exception_type( 
    200                        core_exceptions.DeadlineExceeded, 
    201                        core_exceptions.InternalServerError, 
    202                        core_exceptions.ResourceExhausted, 
    203                        core_exceptions.ServiceUnavailable, 
    204                    ), 
    205                    deadline=60.0, 
    206                ), 
    207                default_timeout=60.0, 
    208                client_info=client_info, 
    209            ), 
    210            self.batch_get_documents: gapic_v1.method.wrap_method( 
    211                self.batch_get_documents, 
    212                default_retry=retries.Retry( 
    213                    initial=0.1, 
    214                    maximum=60.0, 
    215                    multiplier=1.3, 
    216                    predicate=retries.if_exception_type( 
    217                        core_exceptions.DeadlineExceeded, 
    218                        core_exceptions.InternalServerError, 
    219                        core_exceptions.ResourceExhausted, 
    220                        core_exceptions.ServiceUnavailable, 
    221                    ), 
    222                    deadline=300.0, 
    223                ), 
    224                default_timeout=300.0, 
    225                client_info=client_info, 
    226            ), 
    227            self.begin_transaction: gapic_v1.method.wrap_method( 
    228                self.begin_transaction, 
    229                default_retry=retries.Retry( 
    230                    initial=0.1, 
    231                    maximum=60.0, 
    232                    multiplier=1.3, 
    233                    predicate=retries.if_exception_type( 
    234                        core_exceptions.DeadlineExceeded, 
    235                        core_exceptions.InternalServerError, 
    236                        core_exceptions.ResourceExhausted, 
    237                        core_exceptions.ServiceUnavailable, 
    238                    ), 
    239                    deadline=60.0, 
    240                ), 
    241                default_timeout=60.0, 
    242                client_info=client_info, 
    243            ), 
    244            self.commit: gapic_v1.method.wrap_method( 
    245                self.commit, 
    246                default_retry=retries.Retry( 
    247                    initial=0.1, 
    248                    maximum=60.0, 
    249                    multiplier=1.3, 
    250                    predicate=retries.if_exception_type( 
    251                        core_exceptions.ResourceExhausted, 
    252                        core_exceptions.ServiceUnavailable, 
    253                    ), 
    254                    deadline=60.0, 
    255                ), 
    256                default_timeout=60.0, 
    257                client_info=client_info, 
    258            ), 
    259            self.rollback: gapic_v1.method.wrap_method( 
    260                self.rollback, 
    261                default_retry=retries.Retry( 
    262                    initial=0.1, 
    263                    maximum=60.0, 
    264                    multiplier=1.3, 
    265                    predicate=retries.if_exception_type( 
    266                        core_exceptions.DeadlineExceeded, 
    267                        core_exceptions.InternalServerError, 
    268                        core_exceptions.ResourceExhausted, 
    269                        core_exceptions.ServiceUnavailable, 
    270                    ), 
    271                    deadline=60.0, 
    272                ), 
    273                default_timeout=60.0, 
    274                client_info=client_info, 
    275            ), 
    276            self.run_query: gapic_v1.method.wrap_method( 
    277                self.run_query, 
    278                default_retry=retries.Retry( 
    279                    initial=0.1, 
    280                    maximum=60.0, 
    281                    multiplier=1.3, 
    282                    predicate=retries.if_exception_type( 
    283                        core_exceptions.DeadlineExceeded, 
    284                        core_exceptions.InternalServerError, 
    285                        core_exceptions.ResourceExhausted, 
    286                        core_exceptions.ServiceUnavailable, 
    287                    ), 
    288                    deadline=300.0, 
    289                ), 
    290                default_timeout=300.0, 
    291                client_info=client_info, 
    292            ), 
    293            self.run_aggregation_query: gapic_v1.method.wrap_method( 
    294                self.run_aggregation_query, 
    295                default_retry=retries.Retry( 
    296                    initial=0.1, 
    297                    maximum=60.0, 
    298                    multiplier=1.3, 
    299                    predicate=retries.if_exception_type( 
    300                        core_exceptions.DeadlineExceeded, 
    301                        core_exceptions.InternalServerError, 
    302                        core_exceptions.ResourceExhausted, 
    303                        core_exceptions.ServiceUnavailable, 
    304                    ), 
    305                    deadline=300.0, 
    306                ), 
    307                default_timeout=300.0, 
    308                client_info=client_info, 
    309            ), 
    310            self.partition_query: gapic_v1.method.wrap_method( 
    311                self.partition_query, 
    312                default_retry=retries.Retry( 
    313                    initial=0.1, 
    314                    maximum=60.0, 
    315                    multiplier=1.3, 
    316                    predicate=retries.if_exception_type( 
    317                        core_exceptions.DeadlineExceeded, 
    318                        core_exceptions.InternalServerError, 
    319                        core_exceptions.ResourceExhausted, 
    320                        core_exceptions.ServiceUnavailable, 
    321                    ), 
    322                    deadline=300.0, 
    323                ), 
    324                default_timeout=300.0, 
    325                client_info=client_info, 
    326            ), 
    327            self.write: gapic_v1.method.wrap_method( 
    328                self.write, 
    329                default_timeout=86400.0, 
    330                client_info=client_info, 
    331            ), 
    332            self.listen: gapic_v1.method.wrap_method( 
    333                self.listen, 
    334                default_retry=retries.Retry( 
    335                    initial=0.1, 
    336                    maximum=60.0, 
    337                    multiplier=1.3, 
    338                    predicate=retries.if_exception_type( 
    339                        core_exceptions.DeadlineExceeded, 
    340                        core_exceptions.InternalServerError, 
    341                        core_exceptions.ResourceExhausted, 
    342                        core_exceptions.ServiceUnavailable, 
    343                    ), 
    344                    deadline=86400.0, 
    345                ), 
    346                default_timeout=86400.0, 
    347                client_info=client_info, 
    348            ), 
    349            self.list_collection_ids: gapic_v1.method.wrap_method( 
    350                self.list_collection_ids, 
    351                default_retry=retries.Retry( 
    352                    initial=0.1, 
    353                    maximum=60.0, 
    354                    multiplier=1.3, 
    355                    predicate=retries.if_exception_type( 
    356                        core_exceptions.DeadlineExceeded, 
    357                        core_exceptions.InternalServerError, 
    358                        core_exceptions.ResourceExhausted, 
    359                        core_exceptions.ServiceUnavailable, 
    360                    ), 
    361                    deadline=60.0, 
    362                ), 
    363                default_timeout=60.0, 
    364                client_info=client_info, 
    365            ), 
    366            self.batch_write: gapic_v1.method.wrap_method( 
    367                self.batch_write, 
    368                default_retry=retries.Retry( 
    369                    initial=0.1, 
    370                    maximum=60.0, 
    371                    multiplier=1.3, 
    372                    predicate=retries.if_exception_type( 
    373                        core_exceptions.Aborted, 
    374                        core_exceptions.ResourceExhausted, 
    375                        core_exceptions.ServiceUnavailable, 
    376                    ), 
    377                    deadline=60.0, 
    378                ), 
    379                default_timeout=60.0, 
    380                client_info=client_info, 
    381            ), 
    382            self.create_document: gapic_v1.method.wrap_method( 
    383                self.create_document, 
    384                default_retry=retries.Retry( 
    385                    initial=0.1, 
    386                    maximum=60.0, 
    387                    multiplier=1.3, 
    388                    predicate=retries.if_exception_type( 
    389                        core_exceptions.ResourceExhausted, 
    390                        core_exceptions.ServiceUnavailable, 
    391                    ), 
    392                    deadline=60.0, 
    393                ), 
    394                default_timeout=60.0, 
    395                client_info=client_info, 
    396            ), 
    397            self.cancel_operation: gapic_v1.method.wrap_method( 
    398                self.cancel_operation, 
    399                default_timeout=None, 
    400                client_info=client_info, 
    401            ), 
    402            self.delete_operation: gapic_v1.method.wrap_method( 
    403                self.delete_operation, 
    404                default_timeout=None, 
    405                client_info=client_info, 
    406            ), 
    407            self.get_operation: gapic_v1.method.wrap_method( 
    408                self.get_operation, 
    409                default_timeout=None, 
    410                client_info=client_info, 
    411            ), 
    412            self.list_operations: gapic_v1.method.wrap_method( 
    413                self.list_operations, 
    414                default_timeout=None, 
    415                client_info=client_info, 
    416            ), 
    417        } 
    418 
    419    def close(self): 
    420        """Closes resources associated with the transport. 
    421 
    422        .. warning:: 
    423             Only call this method if the transport is NOT shared 
    424             with other clients - this may cause errors in other clients! 
    425        """ 
    426        raise NotImplementedError() 
    427 
    428    @property 
    429    def get_document( 
    430        self, 
    431    ) -> Callable[ 
    432        [firestore.GetDocumentRequest], 
    433        Union[document.Document, Awaitable[document.Document]], 
    434    ]: 
    435        raise NotImplementedError() 
    436 
    437    @property 
    438    def list_documents( 
    439        self, 
    440    ) -> Callable[ 
    441        [firestore.ListDocumentsRequest], 
    442        Union[ 
    443            firestore.ListDocumentsResponse, Awaitable[firestore.ListDocumentsResponse] 
    444        ], 
    445    ]: 
    446        raise NotImplementedError() 
    447 
    448    @property 
    449    def update_document( 
    450        self, 
    451    ) -> Callable[ 
    452        [firestore.UpdateDocumentRequest], 
    453        Union[gf_document.Document, Awaitable[gf_document.Document]], 
    454    ]: 
    455        raise NotImplementedError() 
    456 
    457    @property 
    458    def delete_document( 
    459        self, 
    460    ) -> Callable[ 
    461        [firestore.DeleteDocumentRequest], 
    462        Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]], 
    463    ]: 
    464        raise NotImplementedError() 
    465 
    466    @property 
    467    def batch_get_documents( 
    468        self, 
    469    ) -> Callable[ 
    470        [firestore.BatchGetDocumentsRequest], 
    471        Union[ 
    472            firestore.BatchGetDocumentsResponse, 
    473            Awaitable[firestore.BatchGetDocumentsResponse], 
    474        ], 
    475    ]: 
    476        raise NotImplementedError() 
    477 
    478    @property 
    479    def begin_transaction( 
    480        self, 
    481    ) -> Callable[ 
    482        [firestore.BeginTransactionRequest], 
    483        Union[ 
    484            firestore.BeginTransactionResponse, 
    485            Awaitable[firestore.BeginTransactionResponse], 
    486        ], 
    487    ]: 
    488        raise NotImplementedError() 
    489 
    490    @property 
    491    def commit( 
    492        self, 
    493    ) -> Callable[ 
    494        [firestore.CommitRequest], 
    495        Union[firestore.CommitResponse, Awaitable[firestore.CommitResponse]], 
    496    ]: 
    497        raise NotImplementedError() 
    498 
    499    @property 
    500    def rollback( 
    501        self, 
    502    ) -> Callable[ 
    503        [firestore.RollbackRequest], Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]] 
    504    ]: 
    505        raise NotImplementedError() 
    506 
    507    @property 
    508    def run_query( 
    509        self, 
    510    ) -> Callable[ 
    511        [firestore.RunQueryRequest], 
    512        Union[firestore.RunQueryResponse, Awaitable[firestore.RunQueryResponse]], 
    513    ]: 
    514        raise NotImplementedError() 
    515 
    516    @property 
    517    def run_aggregation_query( 
    518        self, 
    519    ) -> Callable[ 
    520        [firestore.RunAggregationQueryRequest], 
    521        Union[ 
    522            firestore.RunAggregationQueryResponse, 
    523            Awaitable[firestore.RunAggregationQueryResponse], 
    524        ], 
    525    ]: 
    526        raise NotImplementedError() 
    527 
    528    @property 
    529    def partition_query( 
    530        self, 
    531    ) -> Callable[ 
    532        [firestore.PartitionQueryRequest], 
    533        Union[ 
    534            firestore.PartitionQueryResponse, 
    535            Awaitable[firestore.PartitionQueryResponse], 
    536        ], 
    537    ]: 
    538        raise NotImplementedError() 
    539 
    540    @property 
    541    def write( 
    542        self, 
    543    ) -> Callable[ 
    544        [firestore.WriteRequest], 
    545        Union[firestore.WriteResponse, Awaitable[firestore.WriteResponse]], 
    546    ]: 
    547        raise NotImplementedError() 
    548 
    549    @property 
    550    def listen( 
    551        self, 
    552    ) -> Callable[ 
    553        [firestore.ListenRequest], 
    554        Union[firestore.ListenResponse, Awaitable[firestore.ListenResponse]], 
    555    ]: 
    556        raise NotImplementedError() 
    557 
    558    @property 
    559    def list_collection_ids( 
    560        self, 
    561    ) -> Callable[ 
    562        [firestore.ListCollectionIdsRequest], 
    563        Union[ 
    564            firestore.ListCollectionIdsResponse, 
    565            Awaitable[firestore.ListCollectionIdsResponse], 
    566        ], 
    567    ]: 
    568        raise NotImplementedError() 
    569 
    570    @property 
    571    def batch_write( 
    572        self, 
    573    ) -> Callable[ 
    574        [firestore.BatchWriteRequest], 
    575        Union[firestore.BatchWriteResponse, Awaitable[firestore.BatchWriteResponse]], 
    576    ]: 
    577        raise NotImplementedError() 
    578 
    579    @property 
    580    def create_document( 
    581        self, 
    582    ) -> Callable[ 
    583        [firestore.CreateDocumentRequest], 
    584        Union[document.Document, Awaitable[document.Document]], 
    585    ]: 
    586        raise NotImplementedError() 
    587 
    588    @property 
    589    def list_operations( 
    590        self, 
    591    ) -> Callable[ 
    592        [operations_pb2.ListOperationsRequest], 
    593        Union[ 
    594            operations_pb2.ListOperationsResponse, 
    595            Awaitable[operations_pb2.ListOperationsResponse], 
    596        ], 
    597    ]: 
    598        raise NotImplementedError() 
    599 
    600    @property 
    601    def get_operation( 
    602        self, 
    603    ) -> Callable[ 
    604        [operations_pb2.GetOperationRequest], 
    605        Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]], 
    606    ]: 
    607        raise NotImplementedError() 
    608 
    609    @property 
    610    def cancel_operation( 
    611        self, 
    612    ) -> Callable[[operations_pb2.CancelOperationRequest], None,]: 
    613        raise NotImplementedError() 
    614 
    615    @property 
    616    def delete_operation( 
    617        self, 
    618    ) -> Callable[[operations_pb2.DeleteOperationRequest], None,]: 
    619        raise NotImplementedError() 
    620 
    621    @property 
    622    def kind(self) -> str: 
    623        raise NotImplementedError() 
    624 
    625 
    626__all__ = ("FirestoreTransport",)