|
| 1 | +# Copyright 2015 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""gcloud client base class for interacting with API.""" |
| 16 | + |
| 17 | + |
| 18 | +from gcloud._helpers import _get_production_project |
| 19 | +from gcloud.connection import Connection |
| 20 | +from gcloud.credentials import get_credentials |
| 21 | +from gcloud.credentials import get_for_service_account_json |
| 22 | +from gcloud.credentials import get_for_service_account_p12 |
| 23 | + |
| 24 | + |
| 25 | +class Client(object): |
| 26 | + """Client to bundle configuration needed for API requests. |
| 27 | +
|
| 28 | + Assumes that the associated ``_connection_class`` only accepts |
| 29 | + ``http`` and ``credentials`` in its constructor. |
| 30 | +
|
| 31 | + :type credentials: :class:`oauth2client.client.OAuth2Credentials` or |
| 32 | + :class:`NoneType` |
| 33 | + :param credentials: The OAuth2 Credentials to use for the connection |
| 34 | + owned by this client. If not passed (and if no ``http`` |
| 35 | + object is passed), falls back to the default inferred |
| 36 | + from the environment. |
| 37 | +
|
| 38 | + :type http: :class:`httplib2.Http` or class that defines ``request()``. |
| 39 | + :param http: An optional HTTP object to make requests. If not passed, an |
| 40 | + ``http`` object is created that is bound to the |
| 41 | + ``credentials`` for the current object. |
| 42 | + """ |
| 43 | + |
| 44 | + _connection_class = Connection |
| 45 | + |
| 46 | + def __init__(self, credentials=None, http=None): |
| 47 | + if credentials is None and http is None: |
| 48 | + credentials = get_credentials() |
| 49 | + self.connection = self._connection_class( |
| 50 | + credentials=credentials, http=http) |
| 51 | + |
| 52 | + @classmethod |
| 53 | + def from_service_account_json(cls, json_credentials_path, *args, **kwargs): |
| 54 | + """Factory to retrieve JSON credentials while creating client. |
| 55 | +
|
| 56 | + :type json_credentials_path: string |
| 57 | + :param json_credentials_path: The path to a private key file (this file |
| 58 | + was given to you when you created the |
| 59 | + service account). This file must contain |
| 60 | + a JSON object with a private key and |
| 61 | + other credentials information (downloaded |
| 62 | + from the Google APIs console). |
| 63 | +
|
| 64 | + :type args: tuple |
| 65 | + :param args: Remaining positional arguments to pass to constructor. |
| 66 | +
|
| 67 | + :type kwargs: dictionary |
| 68 | + :param kwargs: Remaining keyword arguments to pass to constructor. |
| 69 | +
|
| 70 | + :rtype: :class:`gcloud.pubsub.client.Client` |
| 71 | + :returns: The client created with the retrieved JSON credentials. |
| 72 | + :raises: class:`TypeError` if there is a conflict with the kwargs |
| 73 | + and the credentials created by the factory. |
| 74 | + """ |
| 75 | + if 'credentials' in kwargs: |
| 76 | + raise TypeError('credentials must not be in keyword arguments') |
| 77 | + credentials = get_for_service_account_json(json_credentials_path) |
| 78 | + kwargs['credentials'] = credentials |
| 79 | + return cls(*args, **kwargs) |
| 80 | + |
| 81 | + @classmethod |
| 82 | + def from_service_account_p12(cls, client_email, private_key_path, |
| 83 | + *args, **kwargs): |
| 84 | + """Factory to retrieve P12 credentials while creating client. |
| 85 | +
|
| 86 | + .. note:: |
| 87 | + Unless you have an explicit reason to use a PKCS12 key for your |
| 88 | + service account, we recommend using a JSON key. |
| 89 | +
|
| 90 | + :type client_email: string |
| 91 | + :param client_email: The e-mail attached to the service account. |
| 92 | +
|
| 93 | + :type private_key_path: string |
| 94 | + :param private_key_path: The path to a private key file (this file was |
| 95 | + given to you when you created the service |
| 96 | + account). This file must be in P12 format. |
| 97 | +
|
| 98 | + :type args: tuple |
| 99 | + :param args: Remaining positional arguments to pass to constructor. |
| 100 | +
|
| 101 | + :type kwargs: dictionary |
| 102 | + :param kwargs: Remaining keyword arguments to pass to constructor. |
| 103 | +
|
| 104 | + :rtype: :class:`gcloud.client.Client` |
| 105 | + :returns: The client created with the retrieved P12 credentials. |
| 106 | + :raises: class:`TypeError` if there is a conflict with the kwargs |
| 107 | + and the credentials created by the factory. |
| 108 | + """ |
| 109 | + if 'credentials' in kwargs: |
| 110 | + raise TypeError('credentials must not be in keyword arguments') |
| 111 | + credentials = get_for_service_account_p12(client_email, |
| 112 | + private_key_path) |
| 113 | + kwargs['credentials'] = credentials |
| 114 | + return cls(*args, **kwargs) |
| 115 | + |
| 116 | + |
| 117 | +class JSONClient(Client): |
| 118 | + """Client to for Google JSON-based API. |
| 119 | +
|
| 120 | + Assumes such APIs use the `project` and the client needs to store this |
| 121 | + value. |
| 122 | +
|
| 123 | + :type project: string |
| 124 | + :param project: the project which the client acts on behalf of. If not |
| 125 | + passed falls back to the default inferred from the |
| 126 | + environment. |
| 127 | +
|
| 128 | + :type credentials: :class:`oauth2client.client.OAuth2Credentials` or |
| 129 | + :class:`NoneType` |
| 130 | + :param credentials: The OAuth2 Credentials to use for the connection |
| 131 | + owned by this client. If not passed (and if no ``http`` |
| 132 | + object is passed), falls back to the default inferred |
| 133 | + from the environment. |
| 134 | +
|
| 135 | + :type http: :class:`httplib2.Http` or class that defines ``request()``. |
| 136 | + :param http: An optional HTTP object to make requests. If not passed, an |
| 137 | + ``http`` object is created that is bound to the |
| 138 | + ``credentials`` for the current object. |
| 139 | +
|
| 140 | + :raises: :class:`ValueError` if the project is neither passed in nor |
| 141 | + set in the environment. |
| 142 | + """ |
| 143 | + |
| 144 | + def __init__(self, project=None, credentials=None, http=None): |
| 145 | + if project is None: |
| 146 | + project = _get_production_project() |
| 147 | + if project is None: |
| 148 | + raise ValueError('Project was not passed and could not be ' |
| 149 | + 'determined from the environment.') |
| 150 | + self.project = project |
| 151 | + |
| 152 | + super(JSONClient, self).__init__(credentials=credentials, http=http) |
0 commit comments