1919#
2020import typing
2121from dataclasses import dataclass
22- from typing import NoReturn , Optional , TypedDict
22+ from typing import Any , NoReturn , Optional
2323
2424import requests
2525import requests .auth
@@ -118,33 +118,35 @@ def from_dict(dict: dict) -> "JRE":
118118 )
119119
120120
121- ApiConfiguration = TypedDict (
122- "ApiConfiguration" ,
123- {SONAR_HOST_URL : str , SONAR_SCANNER_SONARCLOUD_URL : str , SONAR_SCANNER_API_BASE_URL : str , SONAR_REGION : str },
124- )
121+ @dataclass (frozen = True )
122+ class ApiConfiguration :
123+ sonar_host_url : str
124+ sonar_scanner_sonarcloud_url : str
125+ sonar_scanner_api_base_url : str
126+ sonar_region : str
125127
126128
127- def to_api_configuration (config_dict : dict [Key , any ]) -> ApiConfiguration :
128- return {
129- SONAR_HOST_URL : config_dict .get (SONAR_HOST_URL , "" ),
130- SONAR_SCANNER_SONARCLOUD_URL : config_dict .get (SONAR_SCANNER_SONARCLOUD_URL , "" ),
131- SONAR_SCANNER_API_BASE_URL : config_dict .get (SONAR_SCANNER_API_BASE_URL , "" ),
132- SONAR_REGION : config_dict .get (SONAR_REGION , "" ),
133- }
129+ def to_api_configuration (config_dict : dict [Key , Any ]) -> ApiConfiguration :
130+ return ApiConfiguration (
131+ sonar_host_url = config_dict .get (SONAR_HOST_URL , "" ),
132+ sonar_scanner_sonarcloud_url = config_dict .get (SONAR_SCANNER_SONARCLOUD_URL , "" ),
133+ sonar_scanner_api_base_url = config_dict .get (SONAR_SCANNER_API_BASE_URL , "" ),
134+ sonar_region = config_dict .get (SONAR_REGION , "" ),
135+ )
134136
135137
136- def get_base_urls (config_dict : dict [Key , any ]) -> BaseUrls :
138+ def get_base_urls (config_dict : dict [Key , Any ]) -> BaseUrls :
137139 def is_sq_cloud_url (api_config : ApiConfiguration , sonar_host_url : str ) -> bool :
138- sq_cloud_url = api_config [ SONAR_SCANNER_SONARCLOUD_URL ] or GLOBAL_SONARCLOUD_URL
140+ sq_cloud_url = api_config . sonar_scanner_sonarcloud_url or GLOBAL_SONARCLOUD_URL
139141 return remove_trailing_slash (sonar_host_url ) in [remove_trailing_slash (sq_cloud_url ), US_SONARCLOUD_URL ]
140142
141143 def is_blank (str ) -> bool :
142144 return str .strip () == ""
143145
144146 api_config : ApiConfiguration = to_api_configuration (config_dict )
145147
146- sonar_host_url = remove_trailing_slash (api_config [ SONAR_HOST_URL ] )
147- region = api_config [ SONAR_REGION ]
148+ sonar_host_url = remove_trailing_slash (api_config . sonar_host_url )
149+ region = api_config . sonar_region
148150 if region and region != "us" :
149151 raise InconsistentConfiguration (
150152 f"Invalid region '{ region } '. Valid regions are: 'us'. "
@@ -158,11 +160,11 @@ def is_blank(str) -> bool:
158160 if is_blank (sonar_host_url ) or is_sq_cloud_url (api_config , sonar_host_url ):
159161 default_url = US_SONARCLOUD_URL if region == "us" else GLOBAL_SONARCLOUD_URL
160162 default_api_base_url = "https://api.sonarqube.us" if region == "us" else "https://api.sonarcloud.io"
161- sonar_host_url = api_config [ SONAR_SCANNER_SONARCLOUD_URL ] or default_url
162- api_base_url = api_config [ SONAR_SCANNER_API_BASE_URL ] or default_api_base_url
163+ sonar_host_url = api_config . sonar_scanner_sonarcloud_url or default_url
164+ api_base_url = api_config . sonar_scanner_api_base_url or default_api_base_url
163165 return BaseUrls (base_url = sonar_host_url , api_base_url = api_base_url , is_sonar_qube_cloud = True )
164166 else :
165- api_base_url = api_config [ SONAR_SCANNER_API_BASE_URL ] or f"{ sonar_host_url } /api/v2"
167+ api_base_url = api_config . sonar_scanner_api_base_url or f"{ sonar_host_url } /api/v2"
166168 return BaseUrls (base_url = sonar_host_url , api_base_url = api_base_url , is_sonar_qube_cloud = False )
167169
168170
0 commit comments