|
21 | 21 |
|
22 | 22 | import httplib2 |
23 | 23 |
|
24 | | -from gcloud.credentials import get_credentials |
25 | 24 | from gcloud.exceptions import make_exception |
26 | 25 |
|
27 | 26 |
|
@@ -94,6 +93,25 @@ def http(self): |
94 | 93 | self._http = self._credentials.authorize(self._http) |
95 | 94 | return self._http |
96 | 95 |
|
| 96 | + @staticmethod |
| 97 | + def _create_scoped_credentials(credentials, scope): |
| 98 | + """Create a scoped set of credentials if it is required. |
| 99 | +
|
| 100 | + :type credentials: :class:`oauth2client.client.OAuth2Credentials` or |
| 101 | + :class:`NoneType` |
| 102 | + :param credentials: The OAuth2 Credentials to add a scope to. |
| 103 | +
|
| 104 | + :type scope: list of URLs |
| 105 | + :param scope: the effective service auth scopes for the connection. |
| 106 | +
|
| 107 | + :rtype: :class:`oauth2client.client.OAuth2Credentials` or |
| 108 | + :class:`NoneType` |
| 109 | + :returns: A new credentials object that has a scope added (if needed). |
| 110 | + """ |
| 111 | + if credentials and credentials.create_scoped_required(): |
| 112 | + credentials = credentials.create_scoped(scope) |
| 113 | + return credentials |
| 114 | + |
97 | 115 |
|
98 | 116 | class JSONConnection(Connection): |
99 | 117 | """A connection to a Google JSON-based API. |
@@ -320,20 +338,3 @@ def api_request(self, method, path, query_params=None, |
320 | 338 | return json.loads(content) |
321 | 339 |
|
322 | 340 | return content |
323 | | - |
324 | | - |
325 | | -def get_scoped_connection(klass, scopes): |
326 | | - """Create a scoped connection to GCloud. |
327 | | -
|
328 | | - :type klass: subclass of :class:`gcloud.connection.Connection` |
329 | | - :param klass: the specific ``Connection`` class to instantiate. |
330 | | -
|
331 | | - :type scopes: list of URLs |
332 | | - :param scopes: the effective service auth scopes for the connection. |
333 | | -
|
334 | | - :rtype: instance of ``klass`` |
335 | | - :returns: A connection defined with the proper credentials. |
336 | | - """ |
337 | | - implicit_credentials = get_credentials() |
338 | | - scoped_credentials = implicit_credentials.create_scoped(scopes) |
339 | | - return klass(credentials=scoped_credentials) |
|
0 commit comments