Initial commit
This commit is contained in:
46
lib/kanboard_api.py
Executable file
46
lib/kanboard_api.py
Executable file
@@ -0,0 +1,46 @@
|
||||
import json
|
||||
import requests
|
||||
from typing import Any, Dict, Tuple
|
||||
|
||||
class KanboardAPI:
|
||||
"""Базовый класс для работы с Kanboard через JSON-RPC."""
|
||||
|
||||
def __init__(self, url: str, user: str, token: str):
|
||||
self.url: str = url
|
||||
self.auth: Tuple[str, str] = (user, token)
|
||||
self.request_id: int = 0
|
||||
|
||||
def call(self, method: str, params: Any = None) -> Any:
|
||||
"""Выполнить JSON-RPC запрос с кодировкой UTF-8."""
|
||||
self.request_id += 1
|
||||
payload: Dict[str, Any] = {
|
||||
"jsonrpc": "2.0",
|
||||
"method": method,
|
||||
"id": self.request_id,
|
||||
"params": params or {}
|
||||
}
|
||||
|
||||
try:
|
||||
json_data: bytes = json.dumps(payload, ensure_ascii=False).encode('utf-8')
|
||||
except Exception as e:
|
||||
raise ValueError(f"Ошибка кодирования JSON: {e}")
|
||||
|
||||
headers = {'Content-Type': 'application/json; charset=utf-8'}
|
||||
|
||||
try:
|
||||
response = requests.post(self.url, data=json_data, auth=self.auth, headers=headers)
|
||||
response.raise_for_status()
|
||||
response.encoding = 'utf-8'
|
||||
except requests.exceptions.RequestException as e:
|
||||
raise ConnectionError(f"Сетевая ошибка при вызове API {method}: {e}")
|
||||
|
||||
try:
|
||||
result = response.json()
|
||||
except json.JSONDecodeError as e:
|
||||
raise ValueError(f"Ошибка декодирования JSON: {e}. Ответ: {response.text[:100]}...")
|
||||
|
||||
if 'error' in result:
|
||||
raise Exception(f"API Error: {result['error']}")
|
||||
|
||||
return result.get('result')
|
||||
|
||||
Reference in New Issue
Block a user