Skip to content

Commit 1cbe301

Browse files
Strip 'https' prefix from alloydb_api_endpoint
1 parent a3800ba commit 1cbe301

File tree

6 files changed

+84
-3
lines changed

6 files changed

+84
-3
lines changed

google/cloud/alloydb/connector/async_connector.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from google.cloud.alloydb.connector.lazy import LazyRefreshCache
3232
from google.cloud.alloydb.connector.types import CacheTypes
3333
from google.cloud.alloydb.connector.utils import generate_keys
34+
from google.cloud.alloydb.connector.utils import strip_http_prefix
3435

3536
if TYPE_CHECKING:
3637
from google.auth.credentials import Credentials
@@ -51,7 +52,7 @@ class AsyncConnector:
5152
billing purposes.
5253
Defaults to None, picking up project from environment.
5354
alloydb_api_endpoint (str): Base URL to use when calling
54-
the AlloyDB API endpoint. Defaults to "https://alloydb.googleapis.com".
55+
the AlloyDB API endpoint. Defaults to "alloydb.googleapis.com".
5556
enable_iam_auth (bool): Enables automatic IAM database authentication.
5657
ip_type (str | IPTypes): Default IP type for all AlloyDB connections.
5758
Defaults to IPTypes.PRIVATE ("PRIVATE") for private IP connections.
@@ -75,7 +76,7 @@ def __init__(
7576
self._cache: dict[str, CacheTypes] = {}
7677
# initialize default params
7778
self._quota_project = quota_project
78-
self._alloydb_api_endpoint = alloydb_api_endpoint
79+
self._alloydb_api_endpoint = strip_http_prefix(alloydb_api_endpoint)
7980
self._enable_iam_auth = enable_iam_auth
8081
# if ip_type is str, convert to IPTypes enum
8182
if isinstance(ip_type, str):

google/cloud/alloydb/connector/connector.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import google.cloud.alloydb.connector.pg8000 as pg8000
3838
from google.cloud.alloydb.connector.types import CacheTypes
3939
from google.cloud.alloydb.connector.utils import generate_keys
40+
from google.cloud.alloydb.connector.utils import strip_http_prefix
4041
import google.cloud.alloydb_connectors_v1.proto.resources_pb2 as connectorspb
4142

4243
if TYPE_CHECKING:
@@ -99,7 +100,7 @@ def __init__(
99100
self._cache: dict[str, CacheTypes] = {}
100101
# initialize default params
101102
self._quota_project = quota_project
102-
self._alloydb_api_endpoint = alloydb_api_endpoint
103+
self._alloydb_api_endpoint = strip_http_prefix(alloydb_api_endpoint)
103104
self._enable_iam_auth = enable_iam_auth
104105
# if ip_type is str, convert to IPTypes enum
105106
if isinstance(ip_type, str):

google/cloud/alloydb/connector/utils.py

+12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
from __future__ import annotations
1616

17+
import re
18+
1719
import aiofiles
1820
from cryptography.hazmat.primitives import serialization
1921
from cryptography.hazmat.primitives.asymmetric import rsa
@@ -58,3 +60,13 @@ async def generate_keys() -> tuple[rsa.RSAPrivateKey, str]:
5860
.decode("UTF-8")
5961
)
6062
return (priv_key, pub_key)
63+
64+
65+
def strip_http_prefix(url: str) -> str:
66+
"""
67+
Returns a new URL with 'http://' or 'https://' prefix removed.
68+
"""
69+
if url == "":
70+
return ""
71+
m = re.search(r"^(https?://)?(.+)", url)
72+
return m.group(2)

tests/unit/test_async_connector.py

+20
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ async def test_AsyncConnector_init_bad_ip_type(credentials: FakeCredentials) ->
109109
)
110110

111111

112+
def test_AsyncConnector_init_alloydb_api_endpoint_with_http_prefix():
113+
"""
114+
Test to check whether the __init__ method of AsyncConnector properly sets
115+
alloydb_api_endpoint when its URL has an 'http://' prefix.
116+
"""
117+
connector = AsyncConnector(alloydb_api_endpoint="http://alloydb.googleapis.com")
118+
assert connector._alloydb_api_endpoint == "alloydb.googleapis.com"
119+
connector.close()
120+
121+
122+
def test_AsyncConnector_init_alloydb_api_endpoint_with_https_prefix():
123+
"""
124+
Test to check whether the __init__ method of AsyncConnector properly sets
125+
alloydb_api_endpoint when its URL has an 'https://' prefix.
126+
"""
127+
connector = AsyncConnector(alloydb_api_endpoint="https://alloydb.googleapis.com")
128+
assert connector._alloydb_api_endpoint == "alloydb.googleapis.com"
129+
connector.close()
130+
131+
112132
@pytest.mark.asyncio
113133
async def test_AsyncConnector_context_manager(
114134
credentials: FakeCredentials,

tests/unit/test_connector.py

+20
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,26 @@ def test_Connector_init_ip_type(
107107
connector.close()
108108

109109

110+
def test_Connector_init_alloydb_api_endpoint_with_http_prefix():
111+
"""
112+
Test to check whether the __init__ method of Connector properly sets
113+
alloydb_api_endpoint when its URL has an 'http://' prefix.
114+
"""
115+
connector = Connector(alloydb_api_endpoint="http://alloydb.googleapis.com")
116+
assert connector._alloydb_api_endpoint == "alloydb.googleapis.com"
117+
connector.close()
118+
119+
120+
def test_Connector_init_alloydb_api_endpoint_with_https_prefix():
121+
"""
122+
Test to check whether the __init__ method of Connector properly sets
123+
alloydb_api_endpoint when its URL has an 'https://' prefix.
124+
"""
125+
connector = Connector(alloydb_api_endpoint="https://alloydb.googleapis.com")
126+
assert connector._alloydb_api_endpoint == "alloydb.googleapis.com"
127+
connector.close()
128+
129+
110130
def test_Connector_context_manager(credentials: FakeCredentials) -> None:
111131
"""
112132
Test to check whether the __init__ method of Connector

tests/unit/test_utils.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2025 Google LLC
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+
from google.cloud.alloydb.connector.utils import strip_http_prefix
16+
17+
18+
def test_strip_http_prefix_with_empty_url():
19+
assert strip_http_prefix("") == ""
20+
21+
22+
def test_strip_http_prefix_with_url_having_http_prefix():
23+
assert strip_http_prefix("http://google.com") == "google.com"
24+
25+
26+
def test_strip_http_prefix_with_url_having_https_prefix():
27+
assert strip_http_prefix("https://google.com") == "google.com"

0 commit comments

Comments
 (0)