api_utils.py
47 lines
| 1.9 KiB
| text/x-python
|
PythonLexer
r5325 | # Copyright (C) 2010-2023 RhodeCode GmbH | |||
# | ||||
# This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU Affero General Public License, version 3 | ||||
# (only), as published by the Free Software Foundation. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU Affero General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
# | ||||
# This program is dual-licensed. If you wish to learn more about the | ||||
# RhodeCode Enterprise Edition, including its added features, Support services, | ||||
# and proprietary license terms, please see https://rhodecode.com/licenses/ | ||||
import urllib.parse | ||||
from rhodecode.lib.vcs import CurlSession | ||||
from rhodecode.lib.ext_json import json | ||||
r5326 | from rhodecode.lib.vcs.exceptions import ImproperlyConfiguredError | |||
r5325 | ||||
r5326 | def call_service_api(settings, payload): | |||
try: | ||||
api_host = settings['app.service_api.host'] | ||||
api_token = settings['app.service_api.token'] | ||||
api_url = settings['rhodecode.api.url'] | ||||
except KeyError as exc: | ||||
raise ImproperlyConfiguredError( | ||||
f"{str(exc)} is missing. " | ||||
"Please ensure that app.service_api.host, app.service_api.token and rhodecode.api.url are " | ||||
"defined inside of .ini configuration file." | ||||
) | ||||
r5325 | payload.update({ | |||
'id': 'service', | ||||
r5326 | 'auth_token': api_token | |||
r5325 | }) | |||
r5326 | service_api_url = urllib.parse.urljoin(api_host, api_url) | |||
r5325 | response = CurlSession().post(service_api_url, json.dumps(payload)) | |||
if response.status_code != 200: | ||||
raise Exception(f"Service API at {service_api_url} responded with error: {response.status_code}") | ||||
return json.loads(response.content)['result'] | ||||