Skip to content

Commit 1de0284

Browse files
fix(db_api): allow file path for credentials (#221)
* fix: credentials_uri parameter error * fix: combine credentials and credentials_uri * fix: nits
1 parent af23775 commit 1de0284

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

google/cloud/spanner_dbapi/connection.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,13 @@ def connect(
357357
instances, tables and data. If not provided, will
358358
attempt to determine from the environment.
359359
360-
:type credentials: :class:`~google.auth.credentials.Credentials`
360+
:type credentials: Union[:class:`~google.auth.credentials.Credentials`, str]
361361
:param credentials: (Optional) The authorization credentials to attach to
362362
requests. These credentials identify this application
363-
to the service. If none are specified, the client will
363+
to the service. These credentials may be specified as
364+
a file path indicating where to retrieve the service
365+
account JSON for the credentials to connect to
366+
Cloud Spanner. If none are specified, the client will
364367
attempt to ascertain the credentials from the
365368
environment.
366369
@@ -384,9 +387,14 @@ def connect(
384387
user_agent=user_agent or DEFAULT_USER_AGENT, python_version=PY_VERSION
385388
)
386389

387-
client = spanner.Client(
388-
project=project, credentials=credentials, client_info=client_info
389-
)
390+
if isinstance(credentials, str):
391+
client = spanner.Client.from_service_account_json(
392+
credentials, project=project, client_info=client_info
393+
)
394+
else:
395+
client = spanner.Client(
396+
project=project, credentials=credentials, client_info=client_info
397+
)
390398

391399
instance = client.instance(instance_id)
392400
if not instance.exists():

tests/unit/spanner_dbapi/test_connect.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,28 @@ def test_sessions_pool(self):
139139
):
140140
connect("test-instance", database_id, pool=pool)
141141
database_mock.assert_called_once_with(database_id, pool=pool)
142+
143+
def test_connect_w_credential_file_path(self):
144+
from google.cloud.spanner_dbapi import connect
145+
from google.cloud.spanner_dbapi import Connection
146+
147+
PROJECT = "test-project"
148+
USER_AGENT = "user-agent"
149+
credentials = "dummy/file/path.json"
150+
151+
with mock.patch(
152+
"google.cloud.spanner_v1.Client.from_service_account_json"
153+
) as client_mock:
154+
connection = connect(
155+
"test-instance",
156+
"test-database",
157+
PROJECT,
158+
credentials=credentials,
159+
user_agent=USER_AGENT,
160+
)
161+
162+
self.assertIsInstance(connection, Connection)
163+
164+
client_mock.assert_called_once_with(
165+
credentials, project=PROJECT, client_info=mock.ANY
166+
)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy