1414
1515"""Connections to gcloud datastore API servers."""
1616
17+ import os
18+
1719from gcloud import connection
1820from gcloud .exceptions import make_exception
1921from gcloud .datastore import _datastore_v1_pb2 as datastore_pb
2022from gcloud .datastore import helpers
2123
2224
25+ _GCD_HOST_ENV_VAR_NAME = 'DATASTORE_HOST'
26+
27+
2328class Connection (connection .Connection ):
2429 """A connection to the Google Cloud Datastore via the Protobuf API.
2530
@@ -28,6 +33,10 @@ class Connection(connection.Connection):
2833
2934 :type credentials: :class:`oauth2client.client.OAuth2Credentials`
3035 :param credentials: The OAuth2 Credentials to use for this connection.
36+
37+ :type api_base_url: string
38+ :param api_base_url: The base of the API call URL. Defaults to the value
39+ from :mod:`gcloud.connection`.
3140 """
3241
3342 API_VERSION = 'v1beta2'
@@ -37,6 +46,13 @@ class Connection(connection.Connection):
3746 '/datasets/{dataset_id}/{method}' )
3847 """A template for the URL of a particular API call."""
3948
49+ def __init__ (self , credentials = None , http = None , api_base_url = None ):
50+ super (Connection , self ).__init__ (credentials = credentials , http = http )
51+ if api_base_url is None :
52+ api_base_url = os .getenv (_GCD_HOST_ENV_VAR_NAME ,
53+ connection .API_BASE_URL )
54+ self .api_base_url = api_base_url
55+
4056 def _request (self , dataset_id , method , data ):
4157 """Make a request over the Http transport to the Cloud Datastore API.
4258
@@ -93,8 +109,7 @@ def _rpc(self, dataset_id, method, request_pb, response_pb_cls):
93109 data = request_pb .SerializeToString ())
94110 return response_pb_cls .FromString (response )
95111
96- @classmethod
97- def build_api_url (cls , dataset_id , method , base_url = None ,
112+ def build_api_url (self , dataset_id , method , base_url = None ,
98113 api_version = None ):
99114 """Construct the URL for a particular API call.
100115
@@ -116,9 +131,9 @@ def build_api_url(cls, dataset_id, method, base_url=None,
116131 :param api_version: The version of the API to connect to.
117132 You shouldn't have to provide this.
118133 """
119- return cls .API_URL_TEMPLATE .format (
120- api_base = (base_url or cls . API_BASE_URL ),
121- api_version = (api_version or cls .API_VERSION ),
134+ return self .API_URL_TEMPLATE .format (
135+ api_base = (base_url or self . api_base_url ),
136+ api_version = (api_version or self .API_VERSION ),
122137 dataset_id = dataset_id , method = method )
123138
124139 def lookup (self , dataset_id , key_pbs ,
0 commit comments