Sonar API Wrapper - a Sonarqube api wrapper
pip install sonar-api-wrapper
Executes an API call to SonarQube. This method wraps the requests.request
method.
-
method
(str
): HTTP method to use (e.g., GET, POST, etc.). -
route
(str
): API path that will be concatenated withbase_path
. For example,qualityprofiles/search
. -
parameters
(dict
|None
): Dictionary of parameters for the API call. Default isNone
. -
body
(dict
|None
): Body of the request. Default isNone
. -
files
(Any
): Files to be sent in the request. Default isNone
. -
headers
(dict
|None
): Headers of the request. Default isNone
. -
is_json
(bool
): If set toTrue
, the response will be parsed as JSON. Otherwise, it returns the decoded content. Default isTrue
. -
username
(str
|None
): Username used for authentication. Default is set via the environment variableSONAR_USERNAME
or "admin". Argument value has precedence, followed by environment variable value and lastly default value is used. -
password
(str
|None
): Password used for authentication. Default is set via the environment variableSONAR_PASSWORD
or "admin". Argument value has precedence, followed by environment variable value and lastly default value is used. -
token
(str
|None
): Token used for authentication. It overrides username and password if present. Default value is set via the environment variableSONAR_AUTH_TOKEN
or None. Argument value has precedence, followed by environment variable value and lastly default value is used. -
base_path
(str
|None
): The base endpoint used to build the API call. Default is set via the environment variableSONAR_HOST_URL
or "http://localhost:9000/api/". Argument value has precedence, followed by environment variable value and lastly default value is used.
Returns the API response as list[dict]
, dict
, or any other type based on the response content or raises an exception.
import os
from sonar_api_wrapper import api_call
# override default access config
os.environ['SONAR_PASSWORD'] = 'Username'
os.environ['SONAR_PASSWORD'] = 'YourPassword'
os.environ['SONAR_HOST_URL'] = 'https://yours.sonarqube/api/'
response = api_call('GET', 'qualityprofiles/search', parameters={
'defaults': 'true'
})
print(f'{response["projects"] = }')
Exceptions are raised based on HTTP errors or other request issues.
The dev install is only required if additional development is needed for this library
pip install -e '.[dev]'
Run the command:
python -m build -w
python -m twine upload dist/*
Run the test with command:
pytest
tox
Note: this command requires in the local machine python versions: 3.8
, 3.9
, 3.10
, 3.11
, and 3.12