GeezerVison is showing github.com — Allows Explicitly Enabling/Disabling GAX for Logging/Pubsub by waprin · Pull Request #2553 · googleapis/google-cloud-python
Allows Explicitly Enabling/Disabling GAX for Logging/Pubsub
#2553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.py
(4)
All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -71,11 +71,25 @@ class Client(JSONClient):
:param http: An optional HTTP object to make requests. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.
:type use_gax: bool
:param use_gax: (Optional) Explicitly specifies whether
to use the gRPC transport (via GAX) or HTTP. If unset,
falls back to the ``GOOGLE_CLOUD_DISABLE_GRPC`` environment
variable
"""
_connection_class = Connection
_logging_api = _sinks_api = _metrics_api = None
def __init__(self, project=None, credentials=None,
http=None, use_gax=None):
super(Client, self).__init__(project, credentials, http)
if use_gax is None:
self._use_gax = _USE_GAX
else:
self._use_gax = use_gax
@property
def logging_api(self):
"""Helper for logging-related API calls.
Expand All
@@ -85,7 +99,7 @@ def logging_api(self):
https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.logs
"""
if self._logging_api is None:
if _USE_GAX :
if self._use_gax :
generated = GeneratedLoggingAPI()
self._logging_api = GAXLoggingAPI(generated)
else:
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -39,14 +39,14 @@ def test_ctor(self):
self.assertEqual(client.project, self.PROJECT)
def test_logging_api_wo_gax(self):
from google.cloud.logging.connection import _LoggingAPI
from google.cloud.logging import client as MUT
from google.cloud._testing import _Monkey
client = self._makeOne(self.PROJECT, credentials=_Credentials())
conn = client. connection = object()
from google.cloud.logging import client as MUT
from google.cloud.logging. connection import _LoggingAPI
with _Monkey(MUT, _USE_GAX=False):
api = client.logging_api
client = self._makeOne(self.PROJECT, credentials=_Credentials())
conn = client.connection = object()
api = client.logging_api
self.assertIsInstance(api, _LoggingAPI)
self.assertIs(api._connection, conn)
Expand Down
Expand Up
@@ -85,6 +85,19 @@ def __init__(self, _wrapped):
again = client.logging_api
self.assertIs(again, api)
def test_no_gax_ctor(self):
from google.cloud._testing import _Monkey
Comment thread
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
from google.cloud.logging import client as MUT
from google.cloud.logging.connection import _LoggingAPI
creds = _Credentials()
with _Monkey(MUT, _USE_GAX=True):
client = self._makeOne(project=self.PROJECT, credentials=creds,
use_gax=False)
api = client.logging_api
self.assertIsInstance(api, _LoggingAPI)
def test_sinks_api_wo_gax(self):
from google.cloud.logging.connection import _SinksAPI
from google.cloud.logging import client as MUT
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -63,7 +63,20 @@ class Client(JSONClient):
:param http: An optional HTTP object to make requests. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.
:type use_gax: bool
:param use_gax: (Optional) Explicitly specifies whether
to use the gRPC transport (via GAX) or HTTP. If unset,
falls back to the ``GOOGLE_CLOUD_DISABLE_GRPC`` environment
variable
"""
def __init__(self, project=None, credentials=None,
http=None, use_gax=None):
super(Client, self).__init__(project, credentials, http)
if use_gax is None:
self._use_gax = _USE_GAX
else:
self._use_gax = use_gax
_connection_class = Connection
_publisher_api = _subscriber_api = _iam_policy_api = None
Expand All
@@ -72,7 +85,7 @@ class Client(JSONClient):
def publisher_api(self):
"""Helper for publisher-related API calls."""
if self._publisher_api is None:
if _USE_GAX :
if self._use_gax :
generated = make_gax_publisher_api(self.connection)
self._publisher_api = GAXPublisherAPI(generated)
else:
Expand All
@@ -83,7 +96,7 @@ def publisher_api(self):
def subscriber_api(self):
"""Helper for subscriber-related API calls."""
if self._subscriber_api is None:
if _USE_GAX :
if self._use_gax :
generated = make_gax_subscriber_api(self.connection)
self._subscriber_api = GAXSubscriberAPI(generated)
else:
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -34,18 +34,33 @@ def test_publisher_api_wo_gax(self):
from google.cloud.pubsub import client as MUT
from google.cloud._testing import _Monkey
creds = _Credentials()
client = self._makeOne(project=self.PROJECT, credentials=creds)
conn = client.connection = object()
with _Monkey(MUT, _USE_GAX=False):
api = client.publisher_api
client = self._makeOne(project=self.PROJECT, credentials=creds)
Comment thread
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
conn = client.connection = object()
api = client.publisher_api
self.assertIsInstance(api, _PublisherAPI)
self.assertIs(api._connection, conn)
# API instance is cached
again = client.publisher_api
self.assertIs(again, api)
def test_no_gax_ctor(self):
from google.cloud._testing import _Monkey
from google.cloud.pubsub.connection import _PublisherAPI
from google.cloud.pubsub import client as MUT
creds = _Credentials()
with _Monkey(MUT, _USE_GAX=True):
client = self._makeOne(project=self.PROJECT, credentials=creds,
use_gax=False)
self.assertFalse(client._use_gax)
api = client.publisher_api
self.assertIsInstance(api, _PublisherAPI)
def test_publisher_api_w_gax(self):
from google.cloud.pubsub import client as MUT
from google.cloud._testing import _Monkey
Expand Down
Expand Up
@@ -84,11 +99,12 @@ def test_subscriber_api_wo_gax(self):
from google.cloud.pubsub import client as MUT
from google.cloud._testing import _Monkey
creds = _Credentials()
client = self._makeOne(project=self.PROJECT, credentials=creds)
conn = client.connection = object()
with _Monkey(MUT, _USE_GAX=False):
api = client.subscriber_api
client = self._makeOne(project=self.PROJECT, credentials=creds)
Comment thread
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
conn = client.connection = object()
api = client.subscriber_api
self.assertIsInstance(api, _SubscriberAPI)
self.assertIs(api._connection, conn)
Expand Down
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.