File tree Expand file tree Collapse file tree 5 files changed +42
-6
lines changed
Expand file tree Collapse file tree 5 files changed +42
-6
lines changed Original file line number Diff line number Diff line change @@ -127,4 +127,11 @@ datacommons.RCheck
127127.vscode /
128128
129129# # JetBrains
130- .idea /
130+ .idea /
131+
132+ # Gemini
133+ GEMINI.md
134+ .gemini /
135+
136+ # Temp files
137+ tmp /
Original file line number Diff line number Diff line change @@ -45,7 +45,7 @@ def __init__(
4545
4646 if url is not None :
4747 # Use the given URL directly (strip trailing slash)
48- self .base_url = check_instance_is_valid (url .rstrip ("/" ))
48+ self .base_url = check_instance_is_valid (url .rstrip ("/" ), api_key = api_key )
4949 else :
5050 # Resolve from dc_instance
5151 self .base_url = resolve_instance_url (dc_instance )
Original file line number Diff line number Diff line change @@ -38,7 +38,8 @@ def test_api_initialization_with_url(mock_check_instance):
3838 "Content-Type" : "application/json" ,
3939 "x-surface" : "clientlib-python"
4040 }
41- mock_check_instance .assert_called_once_with ("https://custom_instance.api/v2" )
41+ mock_check_instance .assert_called_once_with ("https://custom_instance.api/v2" ,
42+ api_key = None )
4243
4344
4445@patch (
Original file line number Diff line number Diff line change @@ -69,7 +69,27 @@ def test_check_instance_is_valid_valid(mock_get):
6969 # Assert that the instance URL is returned if it is valid
7070 assert check_instance_is_valid (instance_url ) == instance_url
7171 mock_get .assert_called_once_with (
72- f"{ instance_url } /node?nodes=country%2FGTM&property=->name" )
72+ f"{ instance_url } /node?nodes=country%2FGTM&property=->name" , headers = {})
73+
74+
75+ @patch ("requests.get" )
76+ def test_check_instance_is_valid_with_key_valid (mock_get ):
77+ """Tests that a valid instance URL is correctly validated."""
78+
79+ # Create a mock response object with the expected JSON data and status code
80+ mock_response = MagicMock ()
81+ mock_response .json .return_value = {"data" : {"country/GTM" : {}}}
82+ mock_response .status_code = 200
83+ mock_get .return_value = mock_response
84+
85+ # Mock the instance URL to test
86+ instance_url = "https://valid-instance"
87+ api_key = "test-api-key"
88+ # Assert that the instance URL is returned if it is valid
89+ assert check_instance_is_valid (instance_url , api_key = api_key ) == instance_url
90+ mock_get .assert_called_once_with (
91+ f"{ instance_url } /node?nodes=country%2FGTM&property=->name" ,
92+ headers = {"X-API-Key" : api_key })
7393
7494
7595@patch ("requests.get" )
Original file line number Diff line number Diff line change 1414CUSTOM_DC_V2 : str = "/core/api/v2"
1515
1616
17- def check_instance_is_valid (instance_url : str ) -> str :
17+ def check_instance_is_valid (instance_url : str ,
18+ api_key : str | None = None ) -> str :
1819 """Check that the given instance URL points to a valid Data Commons instance.
1920
2021 This function attempts a GET request against a known node in Data Commons to
2122 validate the given instance URL. If the node is found and the response has the
2223 expected data, the URL is considered valid.
24+
25+ If an api_key is provided, it will be included in the request headers.
2326
2427 Args:
2528 instance_url: The Data Commons instance URL to validate.
29+ api_key: Optional API key for authentication.
2630
2731 Returns:
2832 The validated instance URL.
@@ -34,8 +38,12 @@ def check_instance_is_valid(instance_url: str) -> str:
3438 # Test URL for a known node in Data Commons
3539 test_url = f"{ instance_url } /node?nodes=country%2FGTM&property=->name"
3640
41+ headers = {}
42+ if api_key :
43+ headers ["X-API-Key" ] = api_key
44+
3745 try :
38- response = requests .get (test_url )
46+ response = requests .get (test_url , headers = headers )
3947 response .raise_for_status ()
4048 except requests .exceptions .RequestException as exc :
4149 raise InvalidDCInstanceError (exc .response ) from exc
You can’t perform that action at this time.
0 commit comments