From 1bb4a79a94ca747bd476b3e1dfa611f51469154e Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Wed, 7 Aug 2024 13:24:08 +0700 Subject: [PATCH] Create xcmp_connector.py --- .../interoperability/xcmp/xcmp_connector.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 projects/DAPIO/interoperability/xcmp/xcmp_connector.py diff --git a/projects/DAPIO/interoperability/xcmp/xcmp_connector.py b/projects/DAPIO/interoperability/xcmp/xcmp_connector.py new file mode 100644 index 000000000..4addbf65e --- /dev/null +++ b/projects/DAPIO/interoperability/xcmp/xcmp_connector.py @@ -0,0 +1,23 @@ +import http.client + +class XCMPConnector: + def __init__(self, url: str, namespace: str): + self.url = url + self.namespace = namespace + self.adapter = XCMPAdapter(namespace) + + def send_request(self, request: dict) -> dict: + xcmp = self.adapter.convert_to_xcmp(request) + headers = {"Content-Type": "application/xcmp+xml"} + conn = http.client.HTTPConnection(self.url) + conn.request("POST", "/", ET.tostring(xcmp, encoding="unicode"), headers) + response = conn.getresponse() + xcmp_response = ET.fromstring(response.read()) + return self.adapter.convert_from_xcmp(xcmp_response) + + def receive_request(self) -> dict: + conn = http.client.HTTPConnection(self.url) + conn.request("GET", "/") + response = conn.getresponse() + xcmp = ET.fromstring(response.read()) + return self.adapter.convert_from_xcmp(xcmp)