Skip to content

Commit c869e15

Browse files
authored
Feat: Add api_key argument to Client constructor (#1441)
1 parent b58d319 commit c869e15

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

google/cloud/storage/client.py

+19
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ class Client(ClientWithProject):
108108
:param extra_headers:
109109
(Optional) Custom headers to be sent with the requests attached to the client.
110110
For example, you can add custom audit logging headers.
111+
112+
:type api_key: string
113+
:param api_key:
114+
(Optional) An API key. Mutually exclusive with any other credentials.
115+
This parameter is an alias for setting `client_options.api_key` and
116+
will supercede any api key set in the `client_options` parameter.
111117
"""
112118

113119
SCOPE = (
@@ -126,6 +132,8 @@ def __init__(
126132
client_options=None,
127133
use_auth_w_custom_endpoint=True,
128134
extra_headers={},
135+
*,
136+
api_key=None,
129137
):
130138
self._base_connection = None
131139

@@ -146,6 +154,17 @@ def __init__(
146154

147155
connection_kw_args = {"client_info": client_info}
148156

157+
# api_key should set client_options.api_key. Set it here whether
158+
# client_options was specified as a dict, as a ClientOptions object, or
159+
# None.
160+
if api_key:
161+
if client_options and not isinstance(client_options, dict):
162+
client_options.api_key = api_key
163+
else:
164+
if not client_options:
165+
client_options = {}
166+
client_options["api_key"] = api_key
167+
149168
if client_options:
150169
if isinstance(client_options, dict):
151170
client_options = google.api_core.client_options.from_dict(

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
dependencies = [
3131
"google-auth >= 2.26.1, < 3.0dev",
3232
"google-api-core >= 2.15.0, <3.0.0dev",
33-
"google-cloud-core >= 2.3.0, < 3.0dev",
33+
"google-cloud-core >= 2.4.2, < 3.0dev",
3434
# The dependency "google-resumable-media" is no longer used. However, the
3535
# dependency is still included here to accommodate users who may be
3636
# importing exception classes from the google-resumable-media without

tests/unit/test_client.py

+47
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,53 @@ def test_ctor_w_client_options_object(self):
191191
self.assertEqual(client._connection.API_BASE_URL, api_endpoint)
192192
self.assertEqual(client.api_endpoint, api_endpoint)
193193

194+
def test_ctor_w_api_key(self):
195+
from google.auth.api_key import Credentials
196+
197+
PROJECT = "PROJECT"
198+
api_key = "my_api_key"
199+
200+
client = self._make_one(project=PROJECT, api_key=api_key)
201+
202+
self.assertEqual(
203+
client._connection.API_BASE_URL, client._connection.DEFAULT_API_ENDPOINT
204+
)
205+
self.assertIsInstance(client._credentials, Credentials)
206+
self.assertEqual(client._credentials.token, api_key)
207+
208+
def test_ctor_w_api_key_and_client_options(self):
209+
from google.auth.api_key import Credentials
210+
from google.api_core.client_options import ClientOptions
211+
212+
PROJECT = "PROJECT"
213+
api_key = "my_api_key"
214+
api_endpoint = "https://www.foo-googleapis.com"
215+
client_options = ClientOptions(api_endpoint=api_endpoint)
216+
217+
client = self._make_one(
218+
project=PROJECT, client_options=client_options, api_key=api_key
219+
)
220+
221+
self.assertEqual(client._connection.API_BASE_URL, api_endpoint)
222+
self.assertIsInstance(client._credentials, Credentials)
223+
self.assertEqual(client._credentials.token, api_key)
224+
225+
def test_ctor_w_api_key_and_client_dict(self):
226+
from google.auth.api_key import Credentials
227+
228+
PROJECT = "PROJECT"
229+
api_key = "my_api_key"
230+
api_endpoint = "https://www.foo-googleapis.com"
231+
client_options = {"api_endpoint": api_endpoint}
232+
233+
client = self._make_one(
234+
project=PROJECT, client_options=client_options, api_key=api_key
235+
)
236+
237+
self.assertEqual(client._connection.API_BASE_URL, api_endpoint)
238+
self.assertIsInstance(client._credentials, Credentials)
239+
self.assertEqual(client._credentials.token, api_key)
240+
194241
def test_ctor_w_universe_domain_and_matched_credentials(self):
195242
PROJECT = "PROJECT"
196243
universe_domain = "example.com"

0 commit comments

Comments
 (0)