Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1efdb00

Browse files
committed
Moving _http and _credentials default values from base connection to client.
Still leaving proxy properties on Connection to point to the http and credentials objects owned by the client.
1 parent 9e6f2df commit 1efdb00

File tree

4 files changed

+145
-169
lines changed

4 files changed

+145
-169
lines changed

core/google/cloud/_http.py

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
from six.moves.urllib.parse import urlencode
2121

2222
import google.auth.credentials
23-
import google_auth_httplib2
24-
import httplib2
2523

2624
from google.cloud.exceptions import make_exception
2725

@@ -37,50 +35,14 @@
3735
class Connection(object):
3836
"""A generic connection to Google Cloud Platform.
3937
40-
Subclasses should understand only the basic types in method arguments,
41-
however they should be capable of returning advanced types.
42-
43-
If no value is passed in for ``http``, a :class:`httplib2.Http` object
44-
will be created and authorized with the ``credentials``. If not, the
45-
``credentials`` and ``http`` need not be related.
46-
47-
Subclasses may seek to use the private key from ``credentials`` to sign
48-
data.
49-
50-
A custom (non-``httplib2``) HTTP object must have a ``request`` method
51-
which accepts the following arguments:
52-
53-
* ``uri``
54-
* ``method``
55-
* ``body``
56-
* ``headers``
57-
58-
In addition, ``redirections`` and ``connection_type`` may be used.
59-
60-
Without the use of ``credentials.authorize(http)``, a custom ``http``
61-
object will also need to be able to add a bearer token to API
62-
requests and handle token refresh on 401 errors.
63-
64-
:type credentials: :class:`google.auth.credentials.Credentials` or
65-
:class:`NoneType`
66-
:param credentials: The credentials to use for this connection.
67-
68-
:type http: :class:`httplib2.Http` or class that defines ``request()``.
69-
:param http: An optional HTTP object to make requests.
38+
:type client: :class:`~google.cloud.client.Client`
39+
:param client: The client that owns the credentials.
7040
"""
7141

7242
USER_AGENT = DEFAULT_USER_AGENT
7343

74-
SCOPE = None
75-
"""The scopes required for authenticating with a service.
76-
77-
Needs to be set by subclasses.
78-
"""
79-
80-
def __init__(self, credentials=None, http=None):
81-
self._http = http
82-
self._credentials = google.auth.credentials.with_scopes_if_required(
83-
credentials, self.SCOPE)
44+
def __init__(self, client):
45+
self._client = client
8446

8547
@property
8648
def credentials(self):
@@ -90,7 +52,7 @@ def credentials(self):
9052
:class:`NoneType`
9153
:returns: The credentials object associated with this connection.
9254
"""
93-
return self._credentials
55+
return self._client._credentials
9456

9557
@property
9658
def http(self):
@@ -99,13 +61,7 @@ def http(self):
9961
:rtype: :class:`httplib2.Http`
10062
:returns: A Http object used to transport data.
10163
"""
102-
if self._http is None:
103-
if self._credentials:
104-
self._http = google_auth_httplib2.AuthorizedHttp(
105-
self._credentials)
106-
else:
107-
self._http = httplib2.Http()
108-
return self._http
64+
return self._client._http
10965

11066

11167
class JSONConnection(Connection):

core/google/cloud/client.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import google.auth.credentials
1818
from google.oauth2 import service_account
19+
import google_auth_httplib2
1920
import six
2021

2122
from google.cloud._helpers import _determine_default_project
@@ -74,6 +75,26 @@ class Client(_ClientFactoryMixin):
7475
Stores ``credentials`` and ``http`` object so that subclasses
7576
can pass them along to a connection class.
7677
78+
If no value is passed in for ``http``, a :class:`httplib2.Http` object
79+
will be created and authorized with the ``credentials``. If not, the
80+
``credentials`` and ``http`` need not be related.
81+
82+
Callers and subclasses may seek to use the private key from
83+
``credentials`` to sign data.
84+
85+
A custom (non-``httplib2``) HTTP object must have a ``request`` method
86+
which accepts the following arguments:
87+
88+
* ``uri``
89+
* ``method``
90+
* ``body``
91+
* ``headers``
92+
93+
In addition, ``redirections`` and ``connection_type`` may be used.
94+
95+
A custom ``http`` object will also need to be able to add a bearer token
96+
to API requests and handle token refresh on 401 errors.
97+
7798
:type credentials: :class:`~google.auth.credentials.Credentials`
7899
:param credentials: (Optional) The OAuth2 Credentials to use for this
79100
client. If not passed (and if no ``http`` object is
@@ -88,15 +109,34 @@ class Client(_ClientFactoryMixin):
88109
``credentials`` for the current object.
89110
"""
90111

112+
_SCOPE = None
113+
"""The scopes required for authenticating with a service.
114+
115+
Needs to be set by subclasses.
116+
"""
117+
91118
def __init__(self, credentials=None, http=None):
92119
if (credentials is not None and
93120
not isinstance(
94121
credentials, google.auth.credentials.Credentials)):
95122
raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP)
96123
if credentials is None and http is None:
97124
credentials = get_credentials()
98-
self._credentials = credentials
99-
self._http = http
125+
self._credentials = google.auth.credentials.with_scopes_if_required(
126+
credentials, self._SCOPE)
127+
self._http_internal = http
128+
129+
@property
130+
def _http(self):
131+
"""Getter for object used for HTTP transport.
132+
133+
:rtype: :class:`~httplib2.Http`
134+
:returns: An HTTP object.
135+
"""
136+
if self._http_internal is None:
137+
self._http_internal = google_auth_httplib2.AuthorizedHttp(
138+
self._credentials)
139+
return self._http_internal
100140

101141

102142
class _ClientProjectMixin(object):

0 commit comments

Comments
 (0)