Skip to content

Commit 2c42b7c

Browse files
committed
Manually creating Client._connection in subclasses.
1 parent ae2d90c commit 2c42b7c

File tree

15 files changed

+209
-150
lines changed

15 files changed

+209
-150
lines changed

bigquery/google/cloud/bigquery/client.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,25 @@ class Client(JSONClient):
5858
passed when creating a dataset / job. If not passed,
5959
falls back to the default inferred from the environment.
6060
61-
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
62-
:class:`NoneType`
63-
:param credentials: The OAuth2 Credentials to use for the connection
64-
owned by this client. If not passed (and if no ``http``
65-
object is passed), falls back to the default inferred
66-
from the environment.
67-
68-
:type http: :class:`httplib2.Http` or class that defines ``request()``.
69-
:param http: An optional HTTP object to make requests. If not passed, an
61+
:type credentials: :class:`~google.auth.credentials.Credentials`
62+
:param credentials: (Optional) The OAuth2 Credentials to use for this
63+
client. If not passed (and if no ``http`` object is
64+
passed), falls back to the default inferred from the
65+
environment.
66+
67+
:type http: :class:`~httplib2.Http`
68+
:param http: (Optional) HTTP object to make requests. Can be any object
69+
that defines ``request()`` with the same interface as
70+
:meth:`~httplib2.Http.request`. If not passed, an
7071
``http`` object is created that is bound to the
7172
``credentials`` for the current object.
7273
"""
7374

74-
_connection_class = Connection
75+
def __init__(self, project=None, credentials=None, http=None):
76+
super(Client, self).__init__(
77+
project=project, credentials=credentials, http=http)
78+
self._connection = Connection(
79+
credentials=self._credentials, http=self._http)
7580

7681
def list_projects(self, max_results=None, page_token=None):
7782
"""List projects for the project associated with this client.

core/google/cloud/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ class Client(_ClientFactoryMixin):
8282
8383
:type http: :class:`~httplib2.Http`
8484
:param http: (Optional) HTTP object to make requests. Can be any object
85-
that defines ``request()`` with the same interface as
86-
:meth:`~httplib2.Http.request`. If not passed, an
85+
that defines ``request()`` with the same interface as
86+
:meth:`~httplib2.Http.request`. If not passed, an
8787
``http`` object is created that is bound to the
8888
``credentials`` for the current object.
8989
"""
@@ -148,8 +148,8 @@ class JSONClient(Client, _ClientProjectMixin):
148148
149149
:type http: :class:`~httplib2.Http`
150150
:param http: (Optional) HTTP object to make requests. Can be any object
151-
that defines ``request()`` with the same interface as
152-
:meth:`~httplib2.Http.request`. If not passed, an
151+
that defines ``request()`` with the same interface as
152+
:meth:`~httplib2.Http.request`. If not passed, an
153153
``http`` object is created that is bound to the
154154
``credentials`` for the current object.
155155

datastore/google/cloud/datastore/client.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,26 +157,29 @@ class Client(_BaseClient, _ClientProjectMixin):
157157
:type namespace: str
158158
:param namespace: (optional) namespace to pass to proxied API methods.
159159
160-
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
161-
:class:`NoneType`
162-
:param credentials: The OAuth2 Credentials to use for the connection
163-
owned by this client. If not passed (and if no ``http``
164-
object is passed), falls back to the default inferred
165-
from the environment.
166-
167-
:type http: :class:`httplib2.Http` or class that defines ``request()``.
168-
:param http: An optional HTTP object to make requests. If not passed, an
160+
:type credentials: :class:`~google.auth.credentials.Credentials`
161+
:param credentials: (Optional) The OAuth2 Credentials to use for this
162+
client. If not passed (and if no ``http`` object is
163+
passed), falls back to the default inferred from the
164+
environment.
165+
166+
:type http: :class:`~httplib2.Http`
167+
:param http: (Optional) HTTP object to make requests. Can be any object
168+
that defines ``request()`` with the same interface as
169+
:meth:`~httplib2.Http.request`. If not passed, an
169170
``http`` object is created that is bound to the
170171
``credentials`` for the current object.
171172
"""
172-
_connection_class = Connection
173173

174174
def __init__(self, project=None, namespace=None,
175175
credentials=None, http=None):
176176
_ClientProjectMixin.__init__(self, project=project)
177+
_BaseClient.__init__(self, credentials=credentials, http=http)
178+
self._connection = Connection(
179+
credentials=self._credentials, http=self._http)
180+
177181
self.namespace = namespace
178182
self._batch_stack = _LocalStack()
179-
super(Client, self).__init__(credentials, http)
180183

181184
@staticmethod
182185
def _determine_default(project):

datastore/unit_tests/test_client.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,15 @@ class TestClient(unittest.TestCase):
118118
PROJECT = 'PROJECT'
119119

120120
def setUp(self):
121-
KLASS = self._get_target_class()
122-
self.original_cnxn_class = KLASS._connection_class
123-
KLASS._connection_class = _MockConnection
121+
from google.cloud.datastore import client as MUT
122+
123+
self.original_cnxn_class = MUT.Connection
124+
MUT.Connection = _MockConnection
124125

125126
def tearDown(self):
126-
KLASS = self._get_target_class()
127-
KLASS._connection_class = self.original_cnxn_class
127+
from google.cloud.datastore import client as MUT
128+
129+
MUT.Connection = self.original_cnxn_class
128130

129131
@staticmethod
130132
def _get_target_class():

dns/google/cloud/dns/client.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,25 @@ class Client(JSONClient):
2929
passed when creating a zone. If not passed,
3030
falls back to the default inferred from the environment.
3131
32-
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
33-
:class:`NoneType`
34-
:param credentials: The OAuth2 Credentials to use for the connection
35-
owned by this client. If not passed (and if no ``http``
36-
object is passed), falls back to the default inferred
37-
from the environment.
38-
39-
:type http: :class:`httplib2.Http` or class that defines ``request()``.
40-
:param http: An optional HTTP object to make requests. If not passed, an
32+
:type credentials: :class:`~google.auth.credentials.Credentials`
33+
:param credentials: (Optional) The OAuth2 Credentials to use for this
34+
client. If not passed (and if no ``http`` object is
35+
passed), falls back to the default inferred from the
36+
environment.
37+
38+
:type http: :class:`~httplib2.Http`
39+
:param http: (Optional) HTTP object to make requests. Can be any object
40+
that defines ``request()`` with the same interface as
41+
:meth:`~httplib2.Http.request`. If not passed, an
4142
``http`` object is created that is bound to the
4243
``credentials`` for the current object.
4344
"""
4445

45-
_connection_class = Connection
46+
def __init__(self, project=None, credentials=None, http=None):
47+
super(Client, self).__init__(
48+
project=project, credentials=credentials, http=http)
49+
self._connection = Connection(
50+
credentials=self._credentials, http=self._http)
4651

4752
def quotas(self):
4853
"""Return DNS quotas for the project associated with this client.

language/google/cloud/language/client.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,25 @@
2323
class Client(client_module.Client):
2424
"""Client to bundle configuration needed for API requests.
2525
26-
:type credentials: :class:`~oauth2client.client.OAuth2Credentials`
27-
:param credentials: (Optional) The OAuth2 Credentials to use for the
28-
connection owned by this client. If not passed (and
29-
if no ``http`` object is passed), falls back to the
30-
default inferred from the environment.
31-
32-
:type http: :class:`httplib2.Http` or class that defines ``request()``.
33-
:param http: An optional HTTP object to make requests. If not passed, an
26+
:type credentials: :class:`~google.auth.credentials.Credentials`
27+
:param credentials: (Optional) The OAuth2 Credentials to use for this
28+
client. If not passed (and if no ``http`` object is
29+
passed), falls back to the default inferred from the
30+
environment.
31+
32+
:type http: :class:`~httplib2.Http`
33+
:param http: (Optional) HTTP object to make requests. Can be any object
34+
that defines ``request()`` with the same interface as
35+
:meth:`~httplib2.Http.request`. If not passed, an
3436
``http`` object is created that is bound to the
3537
``credentials`` for the current object.
3638
"""
3739

38-
_connection_class = Connection
40+
def __init__(self, credentials=None, http=None):
41+
super(Client, self).__init__(
42+
credentials=credentials, http=http)
43+
self._connection = Connection(
44+
credentials=self._credentials, http=self._http)
3945

4046
def document_from_text(self, content, **kwargs):
4147
"""Create a plain text document bound to this client.

logging/google/cloud/logging/client.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,16 @@ class Client(JSONClient):
6767
If not passed, falls back to the default inferred
6868
from the environment.
6969
70-
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
71-
:class:`NoneType`
72-
:param credentials: The OAuth2 Credentials to use for the connection
73-
owned by this client. If not passed (and if no ``http``
74-
object is passed), falls back to the default inferred
75-
from the environment.
76-
77-
:type http: :class:`httplib2.Http` or class that defines ``request()``.
78-
:param http: An optional HTTP object to make requests. If not passed, an
70+
:type credentials: :class:`~google.auth.credentials.Credentials`
71+
:param credentials: (Optional) The OAuth2 Credentials to use for this
72+
client. If not passed (and if no ``http`` object is
73+
passed), falls back to the default inferred from the
74+
environment.
75+
76+
:type http: :class:`~httplib2.Http`
77+
:param http: (Optional) HTTP object to make requests. Can be any object
78+
that defines ``request()`` with the same interface as
79+
:meth:`~httplib2.Http.request`. If not passed, an
7980
``http`` object is created that is bound to the
8081
``credentials`` for the current object.
8182
@@ -86,12 +87,16 @@ class Client(JSONClient):
8687
variable
8788
"""
8889

89-
_connection_class = Connection
90-
_logging_api = _sinks_api = _metrics_api = None
90+
_logging_api = None
91+
_sinks_api = None
92+
_metrics_api = None
9193

9294
def __init__(self, project=None, credentials=None,
9395
http=None, use_gax=None):
94-
super(Client, self).__init__(project, credentials, http)
96+
super(Client, self).__init__(
97+
project=project, credentials=credentials, http=http)
98+
self._connection = Connection(
99+
credentials=self._credentials, http=self._http)
95100
if use_gax is None:
96101
self._use_gax = _USE_GAX
97102
else:

monitoring/google/cloud/monitoring/client.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,25 @@ class Client(JSONClient):
5454
:param project: The target project. If not passed, falls back to the
5555
default inferred from the environment.
5656
57-
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
58-
:class:`NoneType`
59-
:param credentials: The OAuth2 Credentials to use for the connection
60-
owned by this client. If not passed (and if no ``http``
61-
object is passed), falls back to the default inferred
62-
from the environment.
63-
64-
:type http: :class:`httplib2.Http` or class that defines ``request()``
65-
:param http: An optional HTTP object to make requests. If not passed, an
57+
:type credentials: :class:`~google.auth.credentials.Credentials`
58+
:param credentials: (Optional) The OAuth2 Credentials to use for this
59+
client. If not passed (and if no ``http`` object is
60+
passed), falls back to the default inferred from the
61+
environment.
62+
63+
:type http: :class:`~httplib2.Http`
64+
:param http: (Optional) HTTP object to make requests. Can be any object
65+
that defines ``request()`` with the same interface as
66+
:meth:`~httplib2.Http.request`. If not passed, an
6667
``http`` object is created that is bound to the
6768
``credentials`` for the current object.
6869
"""
6970

70-
_connection_class = Connection
71+
def __init__(self, project=None, credentials=None, http=None):
72+
super(Client, self).__init__(
73+
project=project, credentials=credentials, http=http)
74+
self._connection = Connection(
75+
credentials=self._credentials, http=self._http)
7176

7277
def query(self,
7378
metric_type=Query.DEFAULT_METRIC_TYPE,

pubsub/google/cloud/pubsub/client.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,16 @@ class Client(JSONClient):
5151
passed when creating a topic. If not passed,
5252
falls back to the default inferred from the environment.
5353
54-
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
55-
:class:`NoneType`
56-
:param credentials: The OAuth2 Credentials to use for the connection
57-
owned by this client. If not passed (and if no ``http``
58-
object is passed), falls back to the default inferred
59-
from the environment.
60-
61-
:type http: :class:`httplib2.Http` or class that defines ``request()``.
62-
:param http: An optional HTTP object to make requests. If not passed, an
54+
:type credentials: :class:`~google.auth.credentials.Credentials`
55+
:param credentials: (Optional) The OAuth2 Credentials to use for this
56+
client. If not passed (and if no ``http`` object is
57+
passed), falls back to the default inferred from the
58+
environment.
59+
60+
:type http: :class:`~httplib2.Http`
61+
:param http: (Optional) HTTP object to make requests. Can be any object
62+
that defines ``request()`` with the same interface as
63+
:meth:`~httplib2.Http.request`. If not passed, an
6364
``http`` object is created that is bound to the
6465
``credentials`` for the current object.
6566
@@ -69,17 +70,22 @@ class Client(JSONClient):
6970
falls back to the ``GOOGLE_CLOUD_DISABLE_GRPC`` environment
7071
variable
7172
"""
73+
74+
_publisher_api = None
75+
_subscriber_api = None
76+
_iam_policy_api = None
77+
7278
def __init__(self, project=None, credentials=None,
7379
http=None, use_gax=None):
74-
super(Client, self).__init__(project, credentials, http)
80+
super(Client, self).__init__(
81+
project=project, credentials=credentials, http=http)
82+
self._connection = Connection(
83+
credentials=self._credentials, http=self._http)
7584
if use_gax is None:
7685
self._use_gax = _USE_GAX
7786
else:
7887
self._use_gax = use_gax
7988

80-
_connection_class = Connection
81-
_publisher_api = _subscriber_api = _iam_policy_api = None
82-
8389
@property
8490
def publisher_api(self):
8591
"""Helper for publisher-related API calls."""

resource_manager/google/cloud/resource_manager/client.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,25 @@ class Client(BaseClient):
3333
>>> from google.cloud import resource_manager
3434
>>> client = resource_manager.Client()
3535
36-
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
37-
:class:`NoneType`
38-
:param credentials: The OAuth2 Credentials to use for the connection
39-
owned by this client. If not passed (and if no ``http``
40-
object is passed), falls back to the default inferred
41-
from the environment.
42-
43-
:type http: :class:`httplib2.Http` or class that defines ``request()``.
44-
:param http: An optional HTTP object to make requests. If not passed, an
36+
:type credentials: :class:`~google.auth.credentials.Credentials`
37+
:param credentials: (Optional) The OAuth2 Credentials to use for this
38+
client. If not passed (and if no ``http`` object is
39+
passed), falls back to the default inferred from the
40+
environment.
41+
42+
:type http: :class:`~httplib2.Http`
43+
:param http: (Optional) HTTP object to make requests. Can be any object
44+
that defines ``request()`` with the same interface as
45+
:meth:`~httplib2.Http.request`. If not passed, an
4546
``http`` object is created that is bound to the
4647
``credentials`` for the current object.
4748
"""
4849

49-
_connection_class = Connection
50+
def __init__(self, credentials=None, http=None):
51+
super(Client, self).__init__(
52+
credentials=credentials, http=http)
53+
self._connection = Connection(
54+
credentials=self._credentials, http=self._http)
5055

5156
def new_project(self, project_id, name=None, labels=None):
5257
"""Create a project bound to the current client.

0 commit comments

Comments
 (0)