Files
Touchh/pms_integration/plugins/bnovo_pms.py
2024-12-09 16:36:11 +09:00

72 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
from .base_plugin import BasePMSPlugin
class BnovoPMS(BasePMSPlugin):
"""
Плагин для интеграции с Bnovo.
"""
json_schema = {
"type": "object",
"properties": {
"id": {"type": "integer"},
"number": {"type": "integer"},
"roomTypeName": {"type": "string"},
"checkInStatus": {"type": "string"},
"guests": {"type": "array"},
},
"required": ["id", "number", "roomTypeName", "checkInStatus", "guests"]
}
def get_default_parser_settings(self):
"""
Возвращает настройки парсера по умолчанию.
"""
return {
"field_mapping": {
"room_name": "roomNumber",
"check_in": "from",
"check_out": "until",
},
"date_format": "%Y-%m-%dT%H:%M:%S"
}
def fetch_data(self):
response = requests.get(self.pms_config.url, headers={"Authorization": f"Bearer {self.pms_config.token}"})
response.raise_for_status()
data = response.json()
# Проверка структуры
expected_fields = self.pms_config.parser_settings.get("fields_mapping", {})
for field in expected_fields.values():
if field not in data[0]: # Проверяем первую запись
raise ValueError(f"Поле {field} отсутствует в ответе API.")
return data
def fetch_and_parse(self):
response = requests.get(
self.pms_config.url,
headers={"Authorization": f"Bearer {self.pms_config.token}"}
)
self.validate_response(response) # Проверка соответствия структуры
if response.status_code != 200:
raise ValueError(f"Ошибка запроса к PMS Bnovo: {response.text}")
data = response.json()
parsed_data = self.parse_data(data)
return parsed_data
def parse_data(self, data):
# Пример разбора данных на основе JSON-маски
reservations = []
for item in data["reservations"]:
reservation = {
"id": item["id"],
"room_number": item["roomNumber"],
"check_in": item["checkIn"],
"check_out": item["checkOut"],
"status": item["status"],
}
reservations.append(reservation)
return reservations