Skip to content

Commit 3ec1d55

Browse files
committed
fix typing error in api.py
1 parent e122dd2 commit 3ec1d55

2 files changed

Lines changed: 34 additions & 26 deletions

File tree

src/pysonar_scanner/api.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#
2020
import typing
2121
from dataclasses import dataclass
22-
from typing import Any, Optional, TypedDict
22+
from typing import Any, Optional
2323

2424
import requests
2525
import requests.auth
@@ -112,33 +112,35 @@ def from_dict(dict: dict) -> "JRE":
112112
raise MissingKeyException(f"Missing key in dictionary {dict}") from e
113113

114114

115-
ApiConfiguration = TypedDict(
116-
"ApiConfiguration",
117-
{SONAR_HOST_URL: str, SONAR_SCANNER_SONARCLOUD_URL: str, SONAR_SCANNER_API_BASE_URL: str, SONAR_REGION: str},
118-
)
115+
@dataclass(frozen=True)
116+
class ApiConfiguration:
117+
sonar_host_url: str
118+
sonar_scanner_sonarcloud_url: str
119+
sonar_scanner_api_base_url: str
120+
sonar_region: str
119121

120122

121123
def to_api_configuration(config_dict: dict[Key, Any]) -> ApiConfiguration:
122-
return {
123-
SONAR_HOST_URL: config_dict.get(SONAR_HOST_URL, ""),
124-
SONAR_SCANNER_SONARCLOUD_URL: config_dict.get(SONAR_SCANNER_SONARCLOUD_URL, ""),
125-
SONAR_SCANNER_API_BASE_URL: config_dict.get(SONAR_SCANNER_API_BASE_URL, ""),
126-
SONAR_REGION: config_dict.get(SONAR_REGION, ""),
127-
}
124+
return ApiConfiguration(
125+
sonar_host_url=config_dict.get(SONAR_HOST_URL, ""),
126+
sonar_scanner_sonarcloud_url=config_dict.get(SONAR_SCANNER_SONARCLOUD_URL, ""),
127+
sonar_scanner_api_base_url=config_dict.get(SONAR_SCANNER_API_BASE_URL, ""),
128+
sonar_region=config_dict.get(SONAR_REGION, ""),
129+
)
128130

129131

130132
def get_base_urls(config_dict: dict[Key, Any]) -> BaseUrls:
131133
def is_sq_cloud_url(api_config: ApiConfiguration, sonar_host_url: str) -> bool:
132-
sq_cloud_url = api_config[SONAR_SCANNER_SONARCLOUD_URL] or GLOBAL_SONARCLOUD_URL
134+
sq_cloud_url = api_config.sonar_scanner_sonarcloud_url or GLOBAL_SONARCLOUD_URL
133135
return remove_trailing_slash(sonar_host_url) in [remove_trailing_slash(sq_cloud_url), US_SONARCLOUD_URL]
134136

135137
def is_blank(str) -> bool:
136138
return str.strip() == ""
137139

138140
api_config: ApiConfiguration = to_api_configuration(config_dict)
139141

140-
sonar_host_url = remove_trailing_slash(api_config[SONAR_HOST_URL])
141-
region = api_config[SONAR_REGION]
142+
sonar_host_url = remove_trailing_slash(api_config.sonar_host_url)
143+
region = api_config.sonar_region
142144
if region and region != "us":
143145
raise InconsistentConfiguration(
144146
f"Invalid region '{region}'. Valid regions are: 'us'. "
@@ -152,11 +154,11 @@ def is_blank(str) -> bool:
152154
if is_blank(sonar_host_url) or is_sq_cloud_url(api_config, sonar_host_url):
153155
default_url = US_SONARCLOUD_URL if region == "us" else GLOBAL_SONARCLOUD_URL
154156
default_api_base_url = "https://api.sonarqube.us" if region == "us" else "https://api.sonarcloud.io"
155-
sonar_host_url = api_config[SONAR_SCANNER_SONARCLOUD_URL] or default_url
156-
api_base_url = api_config[SONAR_SCANNER_API_BASE_URL] or default_api_base_url
157+
sonar_host_url = api_config.sonar_scanner_sonarcloud_url or default_url
158+
api_base_url = api_config.sonar_scanner_api_base_url or default_api_base_url
157159
return BaseUrls(base_url=sonar_host_url, api_base_url=api_base_url, is_sonar_qube_cloud=True)
158160
else:
159-
api_base_url = api_config[SONAR_SCANNER_API_BASE_URL] or f"{sonar_host_url}/api/v2"
161+
api_base_url = api_config.sonar_scanner_api_base_url or f"{sonar_host_url}/api/v2"
160162
return BaseUrls(base_url=sonar_host_url, api_base_url=api_base_url, is_sonar_qube_cloud=False)
161163

162164

tests/unit/test_api.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
SONAR_SCANNER_API_BASE_URL,
3131
SONAR_SCANNER_SONARCLOUD_URL,
3232
)
33-
from pysonar_scanner.api import SQVersion
33+
from pysonar_scanner.api import SQVersion, ApiConfiguration
3434
from pysonar_scanner.exceptions import InconsistentConfiguration
3535
from tests.unit import sq_api_utils
3636
from tests.unit.sq_api_utils import sq_api_mocker
@@ -433,19 +433,25 @@ def test_download_analysis_jre(self):
433433

434434
def test_to_api_configuration(self):
435435
with self.subTest("Missing keys"):
436-
expected = {
437-
SONAR_HOST_URL: "",
438-
SONAR_SCANNER_SONARCLOUD_URL: "",
439-
SONAR_SCANNER_API_BASE_URL: "",
440-
SONAR_REGION: "",
441-
}
436+
expected = ApiConfiguration(
437+
sonar_host_url="",
438+
sonar_scanner_sonarcloud_url="",
439+
sonar_scanner_api_base_url="",
440+
sonar_region="",
441+
)
442442
self.assertEqual(expected, api.to_api_configuration({}))
443443

444444
with self.subTest("All keys"):
445-
expected = {
445+
input_config = {
446446
SONAR_HOST_URL: "https://sonarcloud.io",
447447
SONAR_SCANNER_SONARCLOUD_URL: "https://sonarcloud.io",
448448
SONAR_SCANNER_API_BASE_URL: "https://api.sonarcloud.io",
449449
SONAR_REGION: "us",
450450
}
451-
self.assertEqual(expected, api.to_api_configuration(expected))
451+
expected = ApiConfiguration(
452+
sonar_host_url="https://sonarcloud.io",
453+
sonar_scanner_sonarcloud_url="https://sonarcloud.io",
454+
sonar_scanner_api_base_url="https://api.sonarcloud.io",
455+
sonar_region="us",
456+
)
457+
self.assertEqual(expected, api.to_api_configuration(input_config))

0 commit comments

Comments
 (0)