2024-06-21 14:04:58 +02:00
|
|
|
import typing as _typing
|
|
|
|
import json as _json
|
|
|
|
import requests as _requests
|
|
|
|
|
|
|
|
from helpers import *
|
|
|
|
from conf import *
|
|
|
|
from log import *
|
|
|
|
|
|
|
|
|
2024-06-21 14:26:57 +02:00
|
|
|
_session_key = None
|
|
|
|
|
|
|
|
|
2024-06-21 14:04:58 +02:00
|
|
|
def backend_api_call_generic(
|
|
|
|
http_method,
|
|
|
|
action_path,
|
|
|
|
data
|
|
|
|
):
|
2024-06-21 14:26:57 +02:00
|
|
|
global _session_key
|
2024-06-21 14:04:58 +02:00
|
|
|
log_info(
|
|
|
|
"backend_api_call",
|
|
|
|
{
|
2024-06-21 14:26:57 +02:00
|
|
|
"with_session_key": (not (_session_key is None)),
|
2024-06-21 14:04:58 +02:00
|
|
|
"http_method": http_method,
|
|
|
|
"path": action_path,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
target = string_coin(
|
|
|
|
"{{scheme}}://{{host}}:{{port}}{{path_base}}{{path_action}}",
|
|
|
|
{
|
|
|
|
"scheme": conf_get()["api"]["scheme"],
|
|
|
|
"host": conf_get()["api"]["host"],
|
|
|
|
"port": ("%u" % conf_get()["api"]["port"]),
|
|
|
|
"path_base": conf_get()["api"]["path"],
|
|
|
|
"path_action": action_path,
|
|
|
|
}
|
|
|
|
)
|
2024-06-21 14:26:57 +02:00
|
|
|
headers_common = (
|
|
|
|
{}
|
|
|
|
if (_session_key is None) else
|
|
|
|
{"X-Session-Key": _session_key}
|
|
|
|
)
|
2024-06-21 14:04:58 +02:00
|
|
|
if (http_method == "GET"):
|
|
|
|
response_raw = _requests.get(
|
|
|
|
target,
|
2024-06-21 14:26:57 +02:00
|
|
|
headers = (
|
|
|
|
headers_common
|
|
|
|
)
|
2024-06-21 14:04:58 +02:00
|
|
|
)
|
|
|
|
elif (http_method == "POST"):
|
|
|
|
response_raw = _requests.post(
|
|
|
|
target,
|
2024-06-21 14:26:57 +02:00
|
|
|
headers = (
|
|
|
|
headers_common
|
|
|
|
|
|
|
|
|
{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
}
|
|
|
|
),
|
2024-06-21 14:04:58 +02:00
|
|
|
json = data
|
|
|
|
)
|
|
|
|
elif (http_method == "DELETE"):
|
|
|
|
response_raw = _requests.delete(
|
|
|
|
target,
|
2024-06-21 14:26:57 +02:00
|
|
|
headers = (
|
|
|
|
headers_common
|
|
|
|
),
|
2024-06-21 14:04:58 +02:00
|
|
|
json = data
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError("unhandled HTTP method: %s" % http_method)
|
2024-06-21 14:26:57 +02:00
|
|
|
if ((response_raw.status_code < 200) or (response_raw.status_code >= 300)):
|
|
|
|
raise ValueError("irregular HTTP response status code: %u" % response_raw.status_code)
|
|
|
|
else:
|
|
|
|
return _json.loads(response_raw.text)
|
2024-06-21 14:04:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
def backend_api_call_session_begin(
|
2024-06-21 14:26:57 +02:00
|
|
|
):
|
|
|
|
global _session_key
|
|
|
|
session_key = backend_api_call_generic(
|
2024-06-21 14:04:58 +02:00
|
|
|
"POST",
|
|
|
|
"/session/begin",
|
|
|
|
{
|
|
|
|
"name": conf_get()["account"]["name"],
|
|
|
|
"password": conf_get()["account"]["password"],
|
|
|
|
}
|
|
|
|
)
|
2024-06-21 14:26:57 +02:00
|
|
|
_session_key = session_key
|
|
|
|
return None
|
2024-06-21 14:04:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
def backend_api_call_session_end(
|
|
|
|
):
|
|
|
|
return backend_api_call_generic(
|
|
|
|
"DELETE",
|
|
|
|
"/session/end",
|
|
|
|
None
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def backend_api_call_member_list(
|
|
|
|
):
|
|
|
|
return backend_api_call_generic(
|
|
|
|
"GET",
|
|
|
|
"/member/list",
|
|
|
|
None
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def backend_api_call_member_project(
|
|
|
|
membership_number : _typing.Optional[str],
|
|
|
|
name_real_value : str,
|
|
|
|
email_address_private : _typing.Optional[str],
|
|
|
|
notification_target_url_template : _typing.Optional[str]
|
|
|
|
):
|
|
|
|
return backend_api_call_generic(
|
|
|
|
"POST",
|
|
|
|
"/member/project",
|
|
|
|
{
|
|
|
|
"membership_number": membership_number,
|
|
|
|
"name_real_value": name_real_value,
|
|
|
|
"email_address_private": email_address_private,
|
|
|
|
"notification_target_url_template": notification_target_url_template,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|