Skip to content

Commit 1fab756

Browse files
waprindhermes
authored andcommitted
Merge pull request #2553 from waprin/logging_gax_optional
Allows Explicitly Enabling/Disabling GAX for Logging/Pubsub
1 parent 491519d commit 1fab756

File tree

4 files changed

+70
-14
lines changed

4 files changed

+70
-14
lines changed

logging/google/cloud/logging/client.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,25 @@ class Client(JSONClient):
7171
:param http: An optional HTTP object to make requests. If not passed, an
7272
``http`` object is created that is bound to the
7373
``credentials`` for the current object.
74+
75+
:type use_gax: bool
76+
:param use_gax: (Optional) Explicitly specifies whether
77+
to use the gRPC transport (via GAX) or HTTP. If unset,
78+
falls back to the ``GOOGLE_CLOUD_DISABLE_GRPC`` environment
79+
variable
7480
"""
7581

7682
_connection_class = Connection
7783
_logging_api = _sinks_api = _metrics_api = None
7884

85+
def __init__(self, project=None, credentials=None,
86+
http=None, use_gax=None):
87+
super(Client, self).__init__(project, credentials, http)
88+
if use_gax is None:
89+
self._use_gax = _USE_GAX
90+
else:
91+
self._use_gax = use_gax
92+
7993
@property
8094
def logging_api(self):
8195
"""Helper for logging-related API calls.
@@ -85,7 +99,7 @@ def logging_api(self):
8599
https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.logs
86100
"""
87101
if self._logging_api is None:
88-
if _USE_GAX:
102+
if self._use_gax:
89103
generated = GeneratedLoggingAPI()
90104
self._logging_api = GAXLoggingAPI(generated)
91105
else:

logging/unit_tests/test_client.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ def test_ctor(self):
3939
self.assertEqual(client.project, self.PROJECT)
4040

4141
def test_logging_api_wo_gax(self):
42-
from google.cloud.logging.connection import _LoggingAPI
43-
from google.cloud.logging import client as MUT
4442
from google.cloud._testing import _Monkey
45-
client = self._makeOne(self.PROJECT, credentials=_Credentials())
46-
conn = client.connection = object()
43+
from google.cloud.logging import client as MUT
44+
from google.cloud.logging.connection import _LoggingAPI
4745

4846
with _Monkey(MUT, _USE_GAX=False):
49-
api = client.logging_api
47+
client = self._makeOne(self.PROJECT, credentials=_Credentials())
48+
conn = client.connection = object()
49+
api = client.logging_api
5050

5151
self.assertIsInstance(api, _LoggingAPI)
5252
self.assertIs(api._connection, conn)
@@ -85,6 +85,19 @@ def __init__(self, _wrapped):
8585
again = client.logging_api
8686
self.assertIs(again, api)
8787

88+
def test_no_gax_ctor(self):
89+
from google.cloud._testing import _Monkey
90+
from google.cloud.logging import client as MUT
91+
from google.cloud.logging.connection import _LoggingAPI
92+
93+
creds = _Credentials()
94+
with _Monkey(MUT, _USE_GAX=True):
95+
client = self._makeOne(project=self.PROJECT, credentials=creds,
96+
use_gax=False)
97+
98+
api = client.logging_api
99+
self.assertIsInstance(api, _LoggingAPI)
100+
88101
def test_sinks_api_wo_gax(self):
89102
from google.cloud.logging.connection import _SinksAPI
90103
from google.cloud.logging import client as MUT

pubsub/google/cloud/pubsub/client.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,20 @@ class Client(JSONClient):
6363
:param http: An optional HTTP object to make requests. If not passed, an
6464
``http`` object is created that is bound to the
6565
``credentials`` for the current object.
66+
67+
:type use_gax: bool
68+
:param use_gax: (Optional) Explicitly specifies whether
69+
to use the gRPC transport (via GAX) or HTTP. If unset,
70+
falls back to the ``GOOGLE_CLOUD_DISABLE_GRPC`` environment
71+
variable
6672
"""
73+
def __init__(self, project=None, credentials=None,
74+
http=None, use_gax=None):
75+
super(Client, self).__init__(project, credentials, http)
76+
if use_gax is None:
77+
self._use_gax = _USE_GAX
78+
else:
79+
self._use_gax = use_gax
6780

6881
_connection_class = Connection
6982
_publisher_api = _subscriber_api = _iam_policy_api = None
@@ -72,7 +85,7 @@ class Client(JSONClient):
7285
def publisher_api(self):
7386
"""Helper for publisher-related API calls."""
7487
if self._publisher_api is None:
75-
if _USE_GAX:
88+
if self._use_gax:
7689
generated = make_gax_publisher_api(self.connection)
7790
self._publisher_api = GAXPublisherAPI(generated)
7891
else:
@@ -83,7 +96,7 @@ def publisher_api(self):
8396
def subscriber_api(self):
8497
"""Helper for subscriber-related API calls."""
8598
if self._subscriber_api is None:
86-
if _USE_GAX:
99+
if self._use_gax:
87100
generated = make_gax_subscriber_api(self.connection)
88101
self._subscriber_api = GAXSubscriberAPI(generated)
89102
else:

pubsub/unit_tests/test_client.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,33 @@ def test_publisher_api_wo_gax(self):
3434
from google.cloud.pubsub import client as MUT
3535
from google.cloud._testing import _Monkey
3636
creds = _Credentials()
37-
client = self._makeOne(project=self.PROJECT, credentials=creds)
38-
conn = client.connection = object()
3937

4038
with _Monkey(MUT, _USE_GAX=False):
41-
api = client.publisher_api
39+
client = self._makeOne(project=self.PROJECT, credentials=creds)
40+
41+
conn = client.connection = object()
42+
api = client.publisher_api
4243

4344
self.assertIsInstance(api, _PublisherAPI)
4445
self.assertIs(api._connection, conn)
4546
# API instance is cached
4647
again = client.publisher_api
4748
self.assertIs(again, api)
4849

50+
def test_no_gax_ctor(self):
51+
from google.cloud._testing import _Monkey
52+
from google.cloud.pubsub.connection import _PublisherAPI
53+
from google.cloud.pubsub import client as MUT
54+
55+
creds = _Credentials()
56+
with _Monkey(MUT, _USE_GAX=True):
57+
client = self._makeOne(project=self.PROJECT, credentials=creds,
58+
use_gax=False)
59+
60+
self.assertFalse(client._use_gax)
61+
api = client.publisher_api
62+
self.assertIsInstance(api, _PublisherAPI)
63+
4964
def test_publisher_api_w_gax(self):
5065
from google.cloud.pubsub import client as MUT
5166
from google.cloud._testing import _Monkey
@@ -84,11 +99,12 @@ def test_subscriber_api_wo_gax(self):
8499
from google.cloud.pubsub import client as MUT
85100
from google.cloud._testing import _Monkey
86101
creds = _Credentials()
87-
client = self._makeOne(project=self.PROJECT, credentials=creds)
88-
conn = client.connection = object()
89102

90103
with _Monkey(MUT, _USE_GAX=False):
91-
api = client.subscriber_api
104+
client = self._makeOne(project=self.PROJECT, credentials=creds)
105+
106+
conn = client.connection = object()
107+
api = client.subscriber_api
92108

93109
self.assertIsInstance(api, _SubscriberAPI)
94110
self.assertIs(api._connection, conn)

0 commit comments

Comments
 (0)