@@ -0,0 +1,72 @@
|
||||
import json
|
||||
from typing import Any, Dict, Optional, Tuple
|
||||
|
||||
from flask import Request
|
||||
|
||||
|
||||
def parse_json_request(req: Request, allow_gzip: bool = False) -> Tuple[Optional[Dict[str, Any]], Optional[str]]:
|
||||
"""Парсинг JSON-тела запроса с опциональной поддержкой gzip."""
|
||||
if allow_gzip and req.headers.get("Content-Encoding", "") == "gzip":
|
||||
import gzip
|
||||
|
||||
try:
|
||||
payload = gzip.decompress(req.data)
|
||||
data = json.loads(payload.decode("utf-8"))
|
||||
except (gzip.BadGzipFile, json.JSONDecodeError, UnicodeDecodeError):
|
||||
return None, "Ошибка распаковки сжатых данных"
|
||||
if not isinstance(data, dict):
|
||||
return None, "Тело запроса должно быть JSON-объектом"
|
||||
return data, None
|
||||
|
||||
if not req.is_json:
|
||||
return None, "Content-Type должен быть application/json"
|
||||
|
||||
data = req.get_json() or {}
|
||||
if not isinstance(data, dict):
|
||||
return None, "Тело запроса должно быть JSON-объектом"
|
||||
return data, None
|
||||
|
||||
|
||||
def parse_pull_payload(
|
||||
data: Dict[str, Any],
|
||||
) -> Tuple[Optional[str], Optional[int], Optional[str], Optional[str]]:
|
||||
"""(client_id, limit, client_name, error). client_name — опционально для реестра на сервере."""
|
||||
client_id = data.get("client_id")
|
||||
if not client_id:
|
||||
return None, None, None, "client_id обязателен"
|
||||
|
||||
limit = data.get("limit")
|
||||
lim: Optional[int] = None
|
||||
if limit is not None:
|
||||
try:
|
||||
lim = int(limit)
|
||||
except (TypeError, ValueError):
|
||||
return None, None, None, "limit должен быть числом"
|
||||
|
||||
cn_raw = data.get("client_name")
|
||||
client_name: Optional[str] = None
|
||||
if cn_raw is not None:
|
||||
s = str(cn_raw).strip()
|
||||
if s:
|
||||
client_name = s[:100]
|
||||
|
||||
return str(client_id).strip(), lim, client_name, None
|
||||
|
||||
|
||||
def parse_confirm_payload(data: Dict[str, Any]) -> Tuple[Optional[str], Optional[list], Optional[str]]:
|
||||
client_id = data.get("client_id")
|
||||
task_ids = data.get("task_ids") or []
|
||||
if not client_id:
|
||||
return None, None, "client_id обязателен"
|
||||
if not isinstance(task_ids, list) or not task_ids:
|
||||
return None, None, "task_ids обязателен и должен быть списком"
|
||||
return client_id, task_ids, None
|
||||
|
||||
|
||||
def parse_push_payload(data: Dict[str, Any]) -> Tuple[Optional[str], Any, Optional[str]]:
|
||||
client_id = data.get("client_id")
|
||||
if not client_id:
|
||||
return None, None, "client_id обязателен"
|
||||
changes = data.get("changes", [])
|
||||
return client_id, changes, None
|
||||
|
||||
Reference in New Issue
Block a user