def __init__(self, project=None, namespace=None, credentials=None):
        self.namespace = namespace
        self.host = os.environ.get(environment_vars.GCD_HOST,
                                   DATASTORE_API_HOST)
        self.client_info = _CLIENT_INFO

        # Use insecure connection when using Datastore Emulator, otherwise
        # use secure connection
        emulator = bool(os.environ.get("DATASTORE_EMULATOR_HOST"))
        self.secure = not emulator

        if emulator:
            # When using the emulator, in theory, the client shouldn't need to
            # call home to authenticate, as you don't need to authenticate to
            # use the local emulator. Unfortunately, the client calls home to
            # authenticate anyway, unless you pass ``requests.Session`` to
            # ``_http`` which seems to be the preferred work around.
            super(Client, self).__init__(
                project=project,
                credentials=credentials,
                _http=requests.Session,
            )
        else:
            super(Client, self).__init__(project=project,
                                         credentials=credentials)

        if emulator:
            channel = grpc.insecure_channel(self.host)

        else:
            user_agent = _CLIENT_INFO.to_user_agent()
            channel = _helpers.make_secure_channel(self._credentials,
                                                   user_agent, self.host)

        self.stub = datastore_pb2_grpc.DatastoreStub(channel)
Exemple #2
0
    def __init__(
        self, channel=None, credentials=None, address="datastore.googleapis.com:443"
    ):
        """Instantiate the transport class.

        Args:
            channel (grpc.Channel): A ``Channel`` instance through
                which to make calls. This argument is mutually exclusive
                with ``credentials``; providing both will raise an exception.
            credentials (google.auth.credentials.Credentials): The
                authorization credentials to attach to requests. These
                credentials identify this application to the service. If none
                are specified, the client will attempt to ascertain the
                credentials from the environment.
            address (str): The address where the service is hosted.
        """
        # If both `channel` and `credentials` are specified, raise an
        # exception (channels come with credentials baked in already).
        if channel is not None and credentials is not None:
            raise ValueError(
                "The `channel` and `credentials` arguments are mutually " "exclusive."
            )

        # Create the channel.
        if channel is None:
            channel = self.create_channel(address=address, credentials=credentials)

        self._channel = channel

        # gRPC uses objects called "stubs" that are bound to the
        # channel and provide a basic method for each RPC.
        self._stubs = {"datastore_stub": datastore_pb2_grpc.DatastoreStub(channel)}
Exemple #3
0
 def test_connect(self):
     # when zipped grpc needs to load a resource that it tries to get from disk
     # that resource must be unzipped along with the native code library
     credentials = grpc.ssl_channel_credentials()
     channel = grpc.secure_channel('datastore.googleapis.com', credentials)
     datastore_stub = datastore_pb2_grpc.DatastoreStub(channel)
     request = datastore_pb2.LookupRequest()
     with self.assertRaisesRegexp(grpc.RpcError, 'missing required authentication') as context:
         datastore_stub.Lookup(request)
     self.assertEqual(context.exception.code(), grpc.StatusCode.UNAUTHENTICATED)
Exemple #4
0
def make_stub(client):
    """Create the stub for the `Google Datastore` API.

    Args:
        client (client.Client): The NDB client.

    Returns:
        :class:`~google.cloud.datastore_v1.proto.datastore_pb2_grpc.DatastoreStub`:
            The stub instance.
    """
    if client.secure:
        channel = _helpers.make_secure_channel(client._credentials,
                                               _http.DEFAULT_USER_AGENT,
                                               client.host)
    else:
        channel = grpc.insecure_channel(client.host)

    return datastore_pb2_grpc.DatastoreStub(channel)
Exemple #5
0
def make_stub(client):
    """Create the stub for the `Google Datastore` API.

    Args:
        client (client.Client): The NDB client.

    Returns:
        :class:`~google.cloud.datastore_v1.proto.datastore_pb2_grpc.DatastoreStub`:
            The stub instance.
    """
    if client.secure:
        user_agent = client.client_info.to_user_agent()
        channel = _helpers.make_secure_channel(client._credentials, user_agent,
                                               client.host)
    else:
        channel = grpc.insecure_channel(client.host)

    return datastore_pb2_grpc.DatastoreStub(channel)
def stub():
    """Get the stub for the `Google Datastore` API.

    Gets the stub from the current context, creating one if there isn't one
    already.

    Returns:
        :class:`~google.cloud.datastore_v1.proto.datastore_pb2_grpc.DatastoreStub`:
            The stub instance.
    """
    state = _runstate.current()

    if state.stub is None:
        client = state.client
        if client.secure:
            channel = _helpers.make_secure_channel(client._credentials,
                                                   _http.DEFAULT_USER_AGENT,
                                                   client.host)
        else:
            channel = grpc.insecure_channel(client.host)

        state.stub = datastore_pb2_grpc.DatastoreStub(channel)

    return state.stub