2323from gcloud .credentials import get_for_service_account_p12
2424
2525
26- class Client (object ):
27- """Client to bundle configuration needed for API requests .
26+ class _ClientFactoryMixin (object ):
27+ """Mixin to allow factories that create credentials .
2828
29- Assumes that the associated ``_connection_class`` only accepts
30- ``http`` and ``credentials`` in its constructor.
31-
32- If ``_connection_class`` is :data:`None`, stores the ``credentials``
33- on the client instead of the newly created ``connection``.
29+ .. note::
3430
35- :type credentials: :class:`oauth2client.client.OAuth2Credentials` or
36- :class:`NoneType`
37- :param credentials: The OAuth2 Credentials to use for the connection
38- owned by this client. If not passed (and if no ``http``
39- object is passed), falls back to the default inferred
40- from the environment.
41-
42- :type http: :class:`httplib2.Http` or class that defines ``request()``.
43- :param http: An optional HTTP object to make requests. If not passed, an
44- ``http`` object is created that is bound to the
45- ``credentials`` for the current object.
31+ This class is virtual.
4632 """
4733
48- _connection_class = Connection
49-
50- def __init__ (self , credentials = None , http = None ):
51- if credentials is None and http is None :
52- credentials = get_credentials ()
53- self .connection = None
54- if self ._connection_class is not None :
55- self .connection = self ._connection_class (
56- credentials = credentials , http = http )
57- else :
58- self .credentials = credentials
59- self .http = http
34+ def __init__ (self , * args , ** kwargs ):
35+ raise NotImplementedError ('_ClientFactoryMixin is a virtual class' )
6036
6137 @classmethod
6238 def from_service_account_json (cls , json_credentials_path , * args , ** kwargs ):
@@ -123,16 +99,14 @@ def from_service_account_p12(cls, client_email, private_key_path,
12399 return cls (* args , ** kwargs )
124100
125101
126- class JSONClient ( Client ):
127- """Client to for Google JSON-based API.
102+ class Client ( _ClientFactoryMixin ):
103+ """Client to bundle configuration needed for API requests .
128104
129- Assumes such APIs use the `project` and the client needs to store this
130- value .
105+ Assumes that the associated ``_connection_class`` only accepts
106+ ``http`` and ``credentials`` in its constructor .
131107
132- :type project: string
133- :param project: the project which the client acts on behalf of. If not
134- passed falls back to the default inferred from the
135- environment.
108+ If ``_connection_class`` is :data:`None`, stores the ``credentials``
109+ on the client instead of the newly created ``connection``.
136110
137111 :type credentials: :class:`oauth2client.client.OAuth2Credentials` or
138112 :class:`NoneType`
@@ -145,12 +119,30 @@ class JSONClient(Client):
145119 :param http: An optional HTTP object to make requests. If not passed, an
146120 ``http`` object is created that is bound to the
147121 ``credentials`` for the current object.
122+ """
123+
124+ _connection_class = Connection
125+
126+ def __init__ (self , credentials = None , http = None ):
127+ if credentials is None and http is None :
128+ credentials = get_credentials ()
129+ self .connection = self ._connection_class (
130+ credentials = credentials , http = http )
131+
132+
133+ class _ClientProjectMixin (object ):
134+ """Mixin to allow setting the project on the client.
135+
136+ :type project: string
137+ :param project: the project which the client acts on behalf of. If not
138+ passed falls back to the default inferred from the
139+ environment.
148140
149141 :raises: :class:`ValueError` if the project is neither passed in nor
150142 set in the environment.
151143 """
152144
153- def __init__ (self , project = None , credentials = None , http = None ):
145+ def __init__ (self , project = None ):
154146 if project is None :
155147 project = _get_production_project ()
156148 if project is None :
@@ -160,4 +152,34 @@ def __init__(self, project=None, credentials=None, http=None):
160152 raise ValueError ('Project must be a string.' )
161153 self .project = project
162154
163- super (JSONClient , self ).__init__ (credentials = credentials , http = http )
155+
156+ class JSONClient (Client , _ClientProjectMixin ):
157+ """Client to for Google JSON-based API.
158+
159+ Assumes such APIs use the ``project`` and the client needs to store this
160+ value.
161+
162+ :type project: string
163+ :param project: the project which the client acts on behalf of. If not
164+ passed falls back to the default inferred from the
165+ environment.
166+
167+ :type credentials: :class:`oauth2client.client.OAuth2Credentials` or
168+ :class:`NoneType`
169+ :param credentials: The OAuth2 Credentials to use for the connection
170+ owned by this client. If not passed (and if no ``http``
171+ object is passed), falls back to the default inferred
172+ from the environment.
173+
174+ :type http: :class:`httplib2.Http` or class that defines ``request()``.
175+ :param http: An optional HTTP object to make requests. If not passed, an
176+ ``http`` object is created that is bound to the
177+ ``credentials`` for the current object.
178+
179+ :raises: :class:`ValueError` if the project is neither passed in nor
180+ set in the environment.
181+ """
182+
183+ def __init__ (self , project = None , credentials = None , http = None ):
184+ _ClientProjectMixin .__init__ (self , project = project )
185+ Client .__init__ (self , credentials = credentials , http = http )
0 commit comments