75 lines
2.6 KiB
Python
75 lines
2.6 KiB
Python
"""Notify pusat about cycle close request / cancel."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
import httpx
|
|
from django.conf import settings
|
|
|
|
from apps.farms.models import Cycle
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class PusatCloseError(Exception):
|
|
def __init__(self, message: str):
|
|
self.message = message
|
|
super().__init__(message)
|
|
|
|
|
|
def _configured() -> bool:
|
|
return bool(
|
|
settings.PUSAT_BASE_URL
|
|
and settings.PUSAT_API_KEY
|
|
and settings.PUSAT_CITY_SITE_ID
|
|
)
|
|
|
|
|
|
def notify_pusat_close_request(cycle: Cycle, *, cancelled: bool = False) -> None:
|
|
"""POST ingest/cancel to pusat. Raises PusatCloseError on failure when configured."""
|
|
if not _configured():
|
|
logger.info("Pusat close channel not configured; skipping notify for cycle %s", cycle.pk)
|
|
return
|
|
|
|
base = settings.PUSAT_BASE_URL.rstrip("/")
|
|
headers = {"X-API-Key": settings.PUSAT_API_KEY, "Content-Type": "application/json"}
|
|
city_site_id = int(settings.PUSAT_CITY_SITE_ID)
|
|
|
|
try:
|
|
if cancelled:
|
|
url = f"{base}/cycle-close/cancel/"
|
|
payload: dict[str, Any] = {
|
|
"city_site_id": city_site_id,
|
|
"source_cycle_id": cycle.pk,
|
|
}
|
|
else:
|
|
url = f"{base}/cycle-close/ingest/"
|
|
payload = {
|
|
"city_site_id": city_site_id,
|
|
"source_cycle_id": cycle.pk,
|
|
"kandang_name": cycle.kandang.kandang_name,
|
|
"site_name": cycle.kandang.site.site_name,
|
|
"start_date": cycle.start_date.isoformat(),
|
|
"proposed_end_date": cycle.proposed_end_date.isoformat()
|
|
if cycle.proposed_end_date
|
|
else None,
|
|
"doc_in_weight": cycle.doc_in_weight,
|
|
"doc_in_count": cycle.doc_in_count,
|
|
"total_days": cycle.total_days,
|
|
"requested_at": cycle.close_requested_at.isoformat()
|
|
if cycle.close_requested_at
|
|
else None,
|
|
}
|
|
with httpx.Client(timeout=30) as client:
|
|
response = client.post(url, json=payload, headers=headers)
|
|
if response.status_code >= 400:
|
|
detail = response.text[:300]
|
|
raise PusatCloseError(f"gagal kirim ke pusat — coba lagi ({response.status_code}: {detail})")
|
|
except PusatCloseError:
|
|
raise
|
|
except httpx.HTTPError as exc:
|
|
logger.exception("Pusat notify failed for cycle %s", cycle.pk)
|
|
raise PusatCloseError("gagal kirim ke pusat — coba lagi") from exc
|