Repository: googleapis/google-cloud-python

Issue:
Datastore: Ability to override base URL
Seee https://github.com/GoogleCloudPlatform/google-cloud-python/blob/master/datastore/google/cloud/datastore/client.py.

It is currently only possible to override the base URL:

`_DATASTORE_BASE_URL = 'https://datastore.googleapis.com'`

by setting the `GCD_HOST` environment variable:

```
        try:
            host = os.environ[GCD_HOST]
            self._base_url = 'http://' + host
        except KeyError:
            self._base_url = _DATASTORE_BASE_URL
```

but this is meant to be used with the emulator and so does not use SSL.

It would be helpful to be able to override base_url to an https address.


Discussion:
@eddavisson Can you please clarify the usecase?  Do you intend to run the emulator behind a proxy addressable via HTTPS (rather than running it on your local machine)?
@tseaver This would be for contacting an actual deployed instance of the API, either a non-prod environment or the prod environment via an alternative hostname. For example, the Dataflow SDK contacts the prod environment via the `batch-datastore.googleapis.com` hostname:
https://issues.apache.org/jira/browse/BEAM-1387

Resolving patch:
diff --git a/datastore/google/cloud/datastore/client.py b/datastore/google/cloud/datastore/client.py
--- a/datastore/google/cloud/datastore/client.py
+++ b/datastore/google/cloud/datastore/client.py
@@ -221,6 +221,16 @@ def _determine_default(project):
         """Helper:  override default project detection."""
         return _determine_default_project(project)
 
+    @property
+    def base_url(self):
+        """Getter for API base URL."""
+        return self._base_url
+
+    @base_url.setter
+    def base_url(self, value):
+        """Setter for API base URL."""
+        self._base_url = value
+
     @property
     def _datastore_api(self):
         """Getter for a wrapped API object."""


Test patch:
diff --git a/datastore/tests/unit/test_client.py b/datastore/tests/unit/test_client.py
--- a/datastore/tests/unit/test_client.py
+++ b/datastore/tests/unit/test_client.py
@@ -164,7 +164,7 @@ def test_constructor_w_implicit_inputs(self):
         self.assertIsNone(client.namespace)
         self.assertIs(client._credentials, creds)
         self.assertIsNone(client._http_internal)
-        self.assertEqual(client._base_url, _DATASTORE_BASE_URL)
+        self.assertEqual(client.base_url, _DATASTORE_BASE_URL)
 
         self.assertIsNone(client.current_batch)
         self.assertIsNone(client.current_transaction)
@@ -189,7 +189,7 @@ def test_constructor_w_explicit_inputs(self):
         self.assertIs(client._http_internal, http)
         self.assertIsNone(client.current_batch)
         self.assertEqual(list(client._batch_stack), [])
-        self.assertEqual(client._base_url, _DATASTORE_BASE_URL)
+        self.assertEqual(client.base_url, _DATASTORE_BASE_URL)
 
     def test_constructor_use_grpc_default(self):
         import google.cloud.datastore.client as MUT
@@ -230,7 +230,7 @@ def test_constructor_gcd_host(self):
         with mock.patch('os.environ', new=fake_environ):
             client = self._make_one(
                 project=project, credentials=creds, _http=http)
-            self.assertEqual(client._base_url, 'http://' + host)
+            self.assertEqual(client.base_url, 'http://' + host)
 
     def test__datastore_api_property_gapic(self):
         client = self._make_one(
@@ -253,6 +253,17 @@ def test__datastore_api_property_gapic(self):
                 client._datastore_api, mock.sentinel.ds_api)
             self.assertEqual(make_api.call_count, 1)
 
+    def test_base_url_property(self):
+        alternate_url = 'https://alias.example.com/'
+        project = 'PROJECT'
+        creds = _make_credentials()
+        http = object()
+
+        client = self._make_one(
+            project=project, credentials=creds, _http=http)
+        client.base_url = alternate_url
+        self.assertEqual(client.base_url, alternate_url)
+
     def test__datastore_api_property_http(self):
         from google.cloud.datastore._http import HTTPDatastoreAPI