Model/infrastructure/abri/abri_client.py
Daniel Roth 79ce9dc4b1 Abri log_job sends the spec's recorded LogJob envelope to the relay endpoint 🟩
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 14:24:57 +00:00

45 lines
1.7 KiB
Python

import xml.etree.ElementTree as ET
import requests
from domain.abri.models import JobLogged, LogJobRequest, LogJobResult
from infrastructure.abri.config import AbriConfig
from infrastructure.abri.envelope import serialise_relay_request
STD_JOB_CODE = "SCSEXT"
CLIENT_CODE = "HSG"
RESOURCE_GROUP = "Surveyors"
class AbriClient:
def __init__(self, config: AbriConfig) -> None:
self._config = config
self._session = requests.Session()
def log_job(self, request: LogJobRequest) -> LogJobResult:
body = serialise_relay_request(
request_type="logjob",
parameters=[
("place_ref", request.place_ref),
("std_job_code", STD_JOB_CODE),
("client", CLIENT_CODE),
("short_description", request.short_description),
("long_description", request.long_description),
("client_ref", request.client_ref),
("appointment_date", request.appointment_date.strftime("%d/%m/%Y")),
("appointment_time", request.appointment_time),
("resource", self._config.default_resource),
("resource_group", RESOURCE_GROUP),
],
username=self._config.username,
password=self._config.password,
)
response = self._session.post(self._config.endpoint_url, data=body)
root = ET.fromstring(response.content)
job_logged = root.find("Jobs/Job_logged")
if job_logged is None:
raise ValueError("Job_logged element missing from relay response")
return JobLogged(
job_no=job_logged.attrib["job_no"],
logged_info=job_logged.attrib["logged_info"],
)