|
21 | 21 |
|
22 | 22 | import httplib2 |
23 | 23 |
|
| 24 | +from gcloud.credentials import get_credentials |
| 25 | +from gcloud.credentials import get_for_service_account_json |
| 26 | +from gcloud.credentials import get_for_service_account_p12 |
24 | 27 | from gcloud.exceptions import make_exception |
25 | 28 |
|
26 | 29 |
|
@@ -112,6 +115,68 @@ def _create_scoped_credentials(credentials, scope): |
112 | 115 | credentials = credentials.create_scoped(scope) |
113 | 116 | return credentials |
114 | 117 |
|
| 118 | + @classmethod |
| 119 | + def from_service_account_json(cls, json_credentials_path, *args, **kwargs): |
| 120 | + """Factory to retrieve JSON credentials while creating connection. |
| 121 | +
|
| 122 | + :type json_credentials_path: string |
| 123 | + :param json_credentials_path: The path to a private key file (this file |
| 124 | + was given to you when you created the |
| 125 | + service account). This file must contain |
| 126 | + a JSON object with a private key and |
| 127 | + other credentials information (downloaded |
| 128 | + from the Google APIs console). |
| 129 | +
|
| 130 | + :rtype: :class:`gcloud.connection.Connection` |
| 131 | + :returns: The connection created with the retrieved JSON credentials. |
| 132 | + """ |
| 133 | + credentials = get_for_service_account_json(json_credentials_path) |
| 134 | + if 'credentials' in kwargs: |
| 135 | + raise TypeError('credentials must not be in keyword arguments') |
| 136 | + kwargs['credentials'] = credentials |
| 137 | + return cls(*args, **kwargs) |
| 138 | + |
| 139 | + @classmethod |
| 140 | + def from_service_account_p12(cls, client_email, private_key_path, |
| 141 | + *args, **kwargs): |
| 142 | + """Factory to retrieve P12 credentials while creating connection. |
| 143 | +
|
| 144 | + .. note:: |
| 145 | + Unless you have an explicit reason to use a PKCS12 key for your |
| 146 | + service account, we recommend using a JSON key. |
| 147 | +
|
| 148 | + :type client_email: string |
| 149 | + :param client_email: The e-mail attached to the service account. |
| 150 | +
|
| 151 | + :type private_key_path: string |
| 152 | + :param private_key_path: The path to a private key file (this file was |
| 153 | + given to you when you created the service |
| 154 | + account). This file must be in P12 format. |
| 155 | +
|
| 156 | + :rtype: :class:`gcloud.connection.Connection` |
| 157 | + :returns: The connection created with the retrieved P12 credentials. |
| 158 | + """ |
| 159 | + credentials = get_for_service_account_p12(client_email, |
| 160 | + private_key_path) |
| 161 | + if 'credentials' in kwargs: |
| 162 | + raise TypeError('credentials must not be in keyword arguments') |
| 163 | + kwargs['credentials'] = credentials |
| 164 | + return cls(*args, **kwargs) |
| 165 | + |
| 166 | + @classmethod |
| 167 | + def from_environment(cls, *args, **kwargs): |
| 168 | + """Factory to retrieve implicit credentials while creating connection. |
| 169 | +
|
| 170 | + :rtype: :class:`gcloud.connection.Connection` |
| 171 | + :returns: The connection created with the retrieved implicit |
| 172 | + credentials. |
| 173 | + """ |
| 174 | + credentials = get_credentials() |
| 175 | + if 'credentials' in kwargs: |
| 176 | + raise TypeError('credentials must not be in keyword arguments') |
| 177 | + kwargs['credentials'] = credentials |
| 178 | + return cls(*args, **kwargs) |
| 179 | + |
115 | 180 |
|
116 | 181 | class JSONConnection(Connection): |
117 | 182 | """A connection to a Google JSON-based API. |
|
0 commit comments