From 277388725f38a8da44861641b0c79efc6e2a3155 Mon Sep 17 00:00:00 2001 From: Tuomas Suutari Date: Wed, 18 Dec 2024 14:47:05 +0200 Subject: [PATCH] WIP: Add DEBUG prints to MuseumPlus PUT request This request used to fail when the field name was not set correctly, but even after setting the correct(?) field name it still doesn't seem to save the data to MuseumPlus. The response to the PUT request was HTTP code 204 (with no content) so it should have succeeded, but still the field (`ObjPASLog01Clb`) seems to be empty on MuseumPlus. --- src/passari/museumplus/fields.py | 46 +++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/src/passari/museumplus/fields.py b/src/passari/museumplus/fields.py index d53b62c..ec28d2d 100644 --- a/src/passari/museumplus/fields.py +++ b/src/passari/museumplus/fields.py @@ -1,6 +1,8 @@ import datetime import json +import re +import aiohttp from lxml.etree import Element, fromstring, tostring from passari.config import CONFIG, MUSEUMPLUS_URL @@ -47,8 +49,12 @@ async def get_object_field(session, object_id: int, name: str): async def set_object_field( - session, object_id: int, name: str, field_type: str, - value: str): + session: aiohttp.ClientSession, + object_id: int, + name: str, + field_type: str, + value: str, +): """ Set the value of a single Object field. Value will be created if the field does not exist already. @@ -76,14 +82,46 @@ async def set_object_field( field_elem.append(value_elem) module_elem.append(field_elem) + url = f"{MUSEUMPLUS_URL}/module/Object/{object_id}/{name}" data = tostring(root, encoding="utf-8", xml_declaration=True) response = await session.put( - f"{MUSEUMPLUS_URL}/module/Object/{object_id}/{name}", + url, headers={"Content-Type": "application/xml"}, data=data ) - response.raise_for_status() + + try: + resp_content = await response.read() + except Exception: + resp_content = b"ERROR READING RESPONSE" + + try: + response.raise_for_status() + except Exception: + print() + print("FAILURE:") + print("response:", response) + print("Input URL:", url) + print("Input data:", bytes(data).decode("utf-8")) + print("Response body:") + print(response._body) + print("Response content:") + print(resp_content.decode("utf-8", errors="replace")) + print() + print() + raise + + print() + print() + print("SUCCESS=============================================") + print("Input URL:", url) + print("Input data:", bytes(data).decode("utf-8")) + print("response:", response) + print("Response content:") + print(resp_content.decode("utf-8", errors="replace")) + print() + print() return True