1414
1515"""Client for interacting with the Google Cloud Vision API."""
1616
17+ import os
18+
1719from google .cloud .client import JSONClient
20+ from google .cloud .environment_vars import DISABLE_GRPC
21+
22+ from google .cloud .vision ._gax import _GAPICVisionAPI
1823from google .cloud .vision .connection import Connection
1924from google .cloud .vision .image import Image
2025from google .cloud .vision ._http import _HTTPVisionAPI
2126
2227
28+ _USE_GAX = not os .getenv (DISABLE_GRPC , False )
29+
30+
2331class Client (JSONClient ):
2432 """Client to bundle configuration needed for API requests.
2533
@@ -40,14 +48,25 @@ class Client(JSONClient):
4048 :meth:`~httplib2.Http.request`. If not passed, an
4149 ``http`` object is created that is bound to the
4250 ``credentials`` for the current object.
51+
52+ :type use_gax: bool
53+ :param use_gax: (Optional) Explicitly specifies whether
54+ to use the gRPC transport (via GAX) or HTTP. If unset,
55+ falls back to the ``GOOGLE_CLOUD_DISABLE_GRPC`` environment
56+ variable
4357 """
4458 _vision_api_internal = None
4559
46- def __init__ (self , project = None , credentials = None , http = None ):
60+ def __init__ (self , project = None , credentials = None , http = None ,
61+ use_gax = False ):
4762 super (Client , self ).__init__ (
4863 project = project , credentials = credentials , http = http )
4964 self ._connection = Connection (
5065 credentials = self ._credentials , http = self ._http )
66+ if use_gax is None :
67+ self ._use_gax = _USE_GAX
68+ else :
69+ self ._use_gax = use_gax
5170
5271 def image (self , content = None , filename = None , source_uri = None ):
5372 """Get instance of Image using current client.
@@ -71,9 +90,14 @@ def image(self, content=None, filename=None, source_uri=None):
7190 def _vision_api (self ):
7291 """Proxy method that handles which transport call Vision Annotate.
7392
74- :rtype: :class:`~google.cloud.vision._rest._HTTPVisionAPI`
75- :returns: Instance of ``_HTTPVisionAPI`` used to make requests.
93+ :rtype: :class:`~google.cloud.vision._http._HTTPVisionAPI`
94+ or :class:`~google.cloud.vision._gax._GAPICVisionAPI`
95+ :returns: Instance of ``_HTTPVisionAPI`` or ``_GAPICVisionAPI`` used to
96+ make requests.
7697 """
7798 if self ._vision_api_internal is None :
78- self ._vision_api_internal = _HTTPVisionAPI (self )
99+ if self ._use_gax :
100+ self ._vision_api_internal = _GAPICVisionAPI (self )
101+ else :
102+ self ._vision_api_internal = _HTTPVisionAPI (self )
79103 return self ._vision_api_internal
0 commit comments