FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Support forwarding arbitrary client kwargs to spanner.Client by oortega15 · Pull Request #196 · google/python-spanner-orm · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (4) .yaml  (1) All 2 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 9 additions & 3 deletions .github/workflows/test.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ on:
schedule:
- cron: '50 13 * * *'

permissions:
contents: read

jobs:
test:
strategy:
Expand All @@ -29,23 +32,26 @@ jobs:
- '3.11'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
- name: Install cloud-spanner-emulator
run: |
# https://github.com/GoogleCloudPlatform/cloud-spanner-emulator#via-pre-built-linux-binaries
VERSION=1.2.0
wget https://storage.googleapis.com/cloud-spanner-emulator/releases/${VERSION}/cloud-spanner-emulator_linux_amd64-${VERSION}.tar.gz
tar zxvf cloud-spanner-emulator_linux_amd64-${VERSION}.tar.gz
chmod u+x gateway_main emulator_main
- uses: actions/setup-python@v2
- uses: actions/setup-python@42375524e23c412d93fb67b49958b491fce71c38 # v5.4.0
with:
python-version: ${{ matrix.python-version }}
- name: Install python dependencies
run: |
pip install \
absl-py \
google-api-core \
'google-cloud-spanner >= 3, <4' \
'google-cloud-spanner >= 3, < 3.64.0; python_version < "3.9"' \
'google-cloud-spanner >= 3, < 4; python_version >= "3.9"' \
immutabledict \
portpicker \
pytest
Expand Down
3 changes: 2 additions & 1 deletion setup.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
include_package_data=True,
python_requires='~=3.8',
install_requires=[
'google-cloud-spanner >= 3, <4',
'google-cloud-spanner >= 3, < 3.64.0; python_version < "3.9"',
'google-cloud-spanner >= 3, < 4; python_version >= "3.9"',
'immutabledict',
],
tests_require=['absl-py', 'google-api-core', 'portpicker'],
Expand Down
13 changes: 7 additions & 6 deletions spanner_orm/admin/api.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
# limitations under the License.
"""Class that handles API calls to Spanner that deal with table metadata."""

from typing import Iterable, Optional
from typing import Any, Iterable, Optional
import warnings

from spanner_orm import api
from spanner_orm import error

from google.auth import credentials as auth_credentials
from google.cloud.spanner_v1 import database as spanner_database
from google.cloud.spanner_v1 import pool as spanner_pool
from spanner_orm import api
from spanner_orm import error


class SpannerAdminApi(api.SpannerReadApi, api.SpannerWriteApi):
Expand Down Expand Up @@ -58,7 +57,8 @@ def connect(instance: str,
project: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
pool: Optional[spanner_pool.AbstractSessionPool] = None,
create_ddl: Optional[Iterable[str]] = None) -> SpannerAdminApi:
create_ddl: Optional[Iterable[str]] = None,
**client_kwargs: Any) -> SpannerAdminApi:
"""Connects the global Spanner admin API to a Spanner database.

Deprecated in favor of from_connection().
Expand All @@ -72,7 +72,8 @@ def connect(instance: str,
project=project,
credentials=credentials,
pool=pool,
create_ddl=create_ddl)
create_ddl=create_ddl,
**client_kwargs)
return from_connection(connection)


Expand Down
22 changes: 15 additions & 7 deletions spanner_orm/api.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def __init__(
*,
client_options: Union[api_client_options.ClientOptions, Dict[Any, Any],
None] = None,
**client_kwargs: Any,
):
"""Connects to the specified Spanner database."""
self._instance = instance
Expand All @@ -126,6 +127,7 @@ def __init__(
self._pool = pool
self._create_ddl = create_ddl
self._client_options = client_options
self._client_kwargs = client_kwargs
self.connect()

def connect(self):
Expand All @@ -134,6 +136,7 @@ def connect(self):
project=self._project,
credentials=self._credentials,
client_options=self._client_options,
**self._client_kwargs,
)
instance = client.instance(self._instance)
self.database = instance.database(
Expand All @@ -159,12 +162,12 @@ def _connection(self):
_api = None # type: Optional[SpannerApi]


def connect(
instance: str,
database: str,
project: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
pool: Optional[spanner_pool.AbstractSessionPool] = None) -> SpannerApi:
def connect(instance: str,
database: str,
project: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
pool: Optional[spanner_pool.AbstractSessionPool] = None,
**client_kwargs: Any) -> SpannerApi:
"""Connects to the Spanner database and sets the global spanner_api.

Deprecated in favor of from_connection().
Expand All @@ -174,7 +177,12 @@ def connect(
'Please use '
'spanner_orm.from_connection(spanner_orm.SpannerConnection(...))'))
connection = SpannerConnection(
instance, database, project=project, credentials=credentials, pool=pool)
instance,
database,
project=project,
credentials=credentials,
pool=pool,
**client_kwargs)
return from_connection(connection)


Expand Down
35 changes: 35 additions & 0 deletions spanner_orm/tests/api_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,41 @@ def test_connection_args(self, client):
client.mock_calls,
)

@mock.patch.object(spanner, 'Client')
def test_connection_args_with_client_kwargs(self, client):
client.return_value.instance.return_value.database.return_value = (
'fake-database')
connection = api.SpannerConnection(
instance='some-instance',
database='some-database',
project='some-project',
credentials='fake-credentials',
pool='fake-pool',
create_ddl=('fake-ddl',),
client_options=dict(fake='options'),
disable_builtin_metrics=True,
route_to_leader_enabled=False,
)
self.assertEqual('fake-database', connection.database)
self.assertSequenceEqual(
(
mock.call(
project='some-project',
credentials='fake-credentials',
client_options=dict(fake='options'),
disable_builtin_metrics=True,
route_to_leader_enabled=False,
),
mock.call().instance('some-instance'),
mock.call().instance().database(
'some-database',
pool='fake-pool',
ddl_statements=('fake-ddl',),
),
),
client.mock_calls,
)

@mock.patch('google.cloud.spanner.Client')
def test_api_connection(self, client):
connection = self.mock_connection(client)
Expand Down
Loading

Back | FazBrowse Home | New Git URL