-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_client.py
More file actions
337 lines (303 loc) · 11.4 KB
/
Copy pathapi_client.py
File metadata and controls
337 lines (303 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# api_client.py
from __future__ import annotations
import logging
import os
import socket
import time
from pathlib import Path
from typing import Dict, List, Optional, Tuple
from urllib.parse import urlparse
import requests
import yaml
from dotenv import load_dotenv
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
# -----------------------------------------------------------------------------
# Pfade & .env
# -----------------------------------------------------------------------------
BASE_DIR = Path(__file__).resolve().parent
load_dotenv(BASE_DIR / ".env") # .env im Skriptordner laden
# -----------------------------------------------------------------------------
# Konfiguration
# -----------------------------------------------------------------------------
CONFIG_PATH = Path(os.getenv("CONFIG_PATH") or (BASE_DIR / "config.yml"))
try:
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
CONFIG = yaml.safe_load(f) or {}
except FileNotFoundError:
CONFIG = {}
API_BASE_URL = (os.getenv("API_BASE_URL") or "").rstrip("/")
API_TOKEN = os.getenv("API_TOKEN")
if not API_BASE_URL:
raise ValueError("API_BASE_URL is not set in .env")
if not API_TOKEN:
raise ValueError("API_TOKEN is not set in .env")
HEADERS = {
"Authorization": f"Bearer {API_TOKEN}",
"Content-Type": "application/json",
}
# -----------------------------------------------------------------------------
# Logging / HTTP Session mit Retries
# -----------------------------------------------------------------------------
logger = logging.getLogger("api_client")
# Level wird durch Root-Logger aus logging_setup.py bestimmt
TIMEOUT = int(CONFIG.get("api", {}).get("timeout_seconds", 20))
RETRIES_CFG = CONFIG.get("api", {}).get("retries", {}) or {}
_session = requests.Session()
retry = Retry(
total=int(RETRIES_CFG.get("total", 0)),
read=int(RETRIES_CFG.get("total", 0)),
connect=int(RETRIES_CFG.get("total", 0)),
backoff_factor=float(RETRIES_CFG.get("backoff_factor", 0)),
status_forcelist=(429, 500, 502, 503, 504),
allowed_methods=frozenset({"GET", "POST"}),
)
_adapter = HTTPAdapter(max_retries=retry)
_session.mount("http://", _adapter)
_session.mount("https://", _adapter)
_LAST_ERROR: Optional[str] = None
# -----------------------------------------------------------------------------
# Low-level HTTP
# -----------------------------------------------------------------------------
def _request(method: str, url: str, *, json: Optional[dict] = None, params: Optional[dict] = None) -> requests.Response:
global _LAST_ERROR
t0 = time.perf_counter()
try:
r = _session.request(method, url, headers=HEADERS, json=json, params=params, timeout=TIMEOUT)
except Exception as e:
_LAST_ERROR = f"request-error {method} {url}: {e}"
logger.error(_LAST_ERROR)
raise
dt_ms = int((time.perf_counter() - t0) * 1000)
body_preview = (r.text or "")[:400]
logger.debug("HTTP %s %s → %s in %d ms | resp-bytes=%s | preview=%r",
method, url, r.status_code, dt_ms, r.headers.get("Content-Length"), body_preview)
if r.status_code >= 400:
_LAST_ERROR = f"HTTP {r.status_code} for {url}; body={(r.text or '')[:500]}"
else:
_LAST_ERROR = None
return r
def _get(url: str, params: Optional[dict] = None) -> dict:
r = _request("GET", url, params=params)
r.raise_for_status()
return r.json()
def _post(url: str, json: dict) -> dict:
r = _request("POST", url, json=json)
r.raise_for_status()
try:
return r.json()
except Exception:
return {"ok": True}
def get_last_error() -> Optional[str]:
return _LAST_ERROR
# -----------------------------------------------------------------------------
# API Wrapper
# -----------------------------------------------------------------------------
def get_players() -> List[Dict]:
try:
data = _get(f"{API_BASE_URL}/api/get_players")
except Exception as e:
logger.warning("get_players() fehlgeschlagen: %s", e)
return []
result: List[Dict] = []
for p in data.get("result", []):
result.append(
{
"name": p.get("name"),
"is_vip": bool(p.get("is_vip")),
"steam_id_64": p.get("player_id") or p.get("steam_id_64") or p.get("steam_id"),
"team": p.get("team"),
}
)
logger.debug("get_players(): %d Spieler", len(result))
return result
def get_detailed_players() -> List[Dict]:
try:
raw = _get(f"{API_BASE_URL}/api/get_detailed_players")
except Exception as e:
logger.warning("get_detailed_players() fehlgeschlagen: %s", e)
return []
payload = raw.get("result") or raw.get("data", {}).get("result")
out: List[Dict] = []
if not payload:
return out
mapping = payload.get("players")
if isinstance(mapping, dict):
for steam_id, p in mapping.items():
out.append({
"name": p.get("name"),
"steam_id_64": p.get("player_id") or steam_id,
"is_vip": bool(p.get("is_vip")),
"team": (p.get("team") or "").lower() or None,
})
return out
if isinstance(payload, list):
for p in payload:
out.append({
"name": p.get("name"),
"steam_id_64": p.get("player_id"),
"is_vip": bool(p.get("is_vip")),
"team": (p.get("team") or "").lower() or None,
})
return out
def message_player(player_name: str, steam_id_64: str, message: str, save_message: bool = True) -> dict:
payload = {
"player_name": player_name,
"player_id": steam_id_64,
"message": message,
"save_message": save_message,
}
return _post(f"{API_BASE_URL}/api/message_player", json=payload)
def message_all(msg: str) -> dict:
try:
players = get_players()
sent = 0
errors = 0
for p in players:
try:
message_player(p.get("name"), p.get("steam_id_64"), msg)
sent += 1
time.sleep(0.2)
except Exception:
errors += 1
continue
return {"ok": True, "count": sent, "errors": errors}
except Exception as e:
return {"ok": False, "error": str(e)}
def message_side(side: str, msg: str) -> dict:
try:
side = (side or "").strip().lower()
if side not in {"allies", "axis"}:
return {"ok": False, "error": f"invalid side '{side}'"}
from_players = get_detailed_players()
target = [p for p in from_players if (str(p.get("team") or "").lower() == side)]
if not target:
return {"ok": False, "error": f"no players on side '{side}'", "count": 0}
sent = 0
errors = 0
for p in target:
try:
message_player(p.get("name"), p.get("steam_id_64"), msg)
sent += 1
time.sleep(0.2)
except Exception:
errors += 1
continue
return {"ok": True, "count": sent, "errors": errors}
except Exception as e:
return {"ok": False, "error": str(e)}
def set_map(map_name: str) -> dict:
url = f"{API_BASE_URL}/api/set_map"
payload = {"map_name": map_name}
try:
resp = _session.post(url, json=payload, timeout=TIMEOUT, headers=HEADERS)
resp.raise_for_status()
ctype = (resp.headers.get("content-type") or "").lower()
if ctype.startswith("application/json"):
data = resp.json()
if isinstance(data, dict) and "ok" not in data:
data["ok"] = True
return data if isinstance(data, dict) else {"ok": True, "data": data}
return {"ok": True}
except requests.RequestException as e:
msg = str(e)
resp = getattr(e, "response", None)
if resp is not None:
msg = f"{e} | status={resp.status_code} | body={(resp.text or '')[:800]} | payload={payload}"
global _LAST_ERROR
_LAST_ERROR = msg
logger.error("set_map() Fehler: %s", msg)
return {"ok": False, "error": msg}
def do_kick_player(player: str, steam_id_64: str, reason: str) -> dict:
url = f"{API_BASE_URL}/api/kick"
data = {
"player_name": player,
"player_id": steam_id_64,
"reason": reason,
"by": "Admin",
}
r = _session.post(url, json=data, headers=HEADERS, timeout=TIMEOUT)
r.raise_for_status()
return r.json()
def do_punish_player(player_name: str, reason: str, by: str) -> dict:
url = f"{API_BASE_URL}/api/punish"
data = {"player_name": player_name, "reason": reason, "by": by}
r = _session.post(url, json=data, headers=HEADERS, timeout=TIMEOUT)
r.raise_for_status()
return r.json()
def do_switch_player_now(player_name: str) -> bool:
"""POST /api/switch_player_now → bool"""
url = f"{API_BASE_URL}/api/switch_player_now"
data = {"player_name": player_name}
r = _session.post(url, json=data, headers=HEADERS, timeout=TIMEOUT)
r.raise_for_status()
try:
js = r.json()
if isinstance(js, dict) and "result" in js:
return bool(js["result"])
except Exception:
pass
return True
# -----------------------------------------------------------------------------
# Diagnose / Health
# -----------------------------------------------------------------------------
def _tcp_connectivity_check() -> dict:
try:
parts = urlparse(API_BASE_URL)
host = parts.hostname
port = parts.port or (443 if parts.scheme == "https" else 80)
if not host:
return {"ok": False, "error": "API_BASE_URL hat keinen Host."}
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2.0)
t0 = time.perf_counter()
s.connect((host, port))
s.close()
return {"ok": True, "host": host, "port": port, "rtt_ms": int((time.perf_counter() - t0) * 1000)}
except Exception as e:
return {"ok": False, "error": str(e)}
def try_endpoints(candidates: List[str]) -> dict:
last = None
for path in candidates:
url = f"{API_BASE_URL}{path}"
try:
r = _request("GET", url)
code = r.status_code
try:
data = r.json()
except Exception:
data = {"text": (r.text or "")[:200]}
return {"url": url, "status": code, "data": data}
except Exception as e:
last = str(e)
return {"url": None, "status": None, "error": last or "unbekannt"}
def diagnose() -> dict:
net = _tcp_connectivity_check()
ver = try_endpoints(["/api/get_version", "/api/version", "/api/about", "/version", "/health"])
try:
data = _get(f"{API_BASE_URL}/api/get_players")
players = {"status": 200, "count": len(data.get("result", [])), "api_version": data.get("version")}
except Exception as e:
players = {"status": "error", "error": str(e)}
return {
"api_base_url": API_BASE_URL,
"timeout_s": TIMEOUT,
"debug": logger.isEnabledFor(logging.DEBUG),
"network": net,
"version": ver,
"get_players": players,
"last_error": get_last_error(),
}
__all__ = [
"get_players",
"get_detailed_players",
"message_player",
"message_all",
"message_side",
"set_map",
"do_kick_player",
"do_punish_player",
"do_switch_player_now",
"get_last_error",
"diagnose",
]