1919#
2020import typing
2121from dataclasses import dataclass
22- from typing import Any , Optional , TypedDict
22+ from typing import Any , Optional
2323
2424import requests
2525import 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
121123def 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
130132def 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
0 commit comments