From 03556df10ecfbeed2bed4c6f4337bc950cb49524 Mon Sep 17 00:00:00 2001 From: Jonas Linter Date: Thu, 25 Sep 2025 13:13:22 +0200 Subject: [PATCH] Handshake first attempt --- src/alpine_bits_python/alpinebits_server.py | 58 +++++++++++++++++++-- test_handshake.py | 29 +++++++++++ 2 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 test_handshake.py diff --git a/src/alpine_bits_python/alpinebits_server.py b/src/alpine_bits_python/alpinebits_server.py index b3b2a52..0bcc388 100644 --- a/src/alpine_bits_python/alpinebits_server.py +++ b/src/alpine_bits_python/alpinebits_server.py @@ -257,13 +257,57 @@ class PingAction(AlpineBitsAction): except Exception as e: return AlpineBitsResponse(f"Error: Invalid XML request - {str(e)}", HttpStatusCode.BAD_REQUEST) + # compare echo data with capabilities, create a dictionary containing the matching capabilities + capabilities_dict = server_capabilities.get_capabilities_dict() + matching_capabilities = {"versions": []} + # Iterate through client's requested versions + for client_version in echo_data.get("versions", []): + client_version_str = client_version.get("version", "") + + # Find matching server version + for server_version in capabilities_dict["versions"]: + if server_version["version"] == client_version_str: + # Found a matching version, now find common actions + matching_version = { + "version": client_version_str, + "actions": [] + } + + # Get client's requested actions for this version + client_actions = {action.get("action", ""): action for action in client_version.get("actions", [])} + server_actions = {action.get("action", ""): action for action in server_version.get("actions", [])} + + # Find common actions + for action_name in client_actions: + if action_name in server_actions: + # Use server's action definition (includes our supports) + matching_version["actions"].append(server_actions[action_name]) + + # Only add version if there are common actions + if matching_version["actions"]: + matching_capabilities["versions"].append(matching_version) + break + # Debug print to see what we matched + print("šŸ” Client requested capabilities:") + print(json.dumps(echo_data, indent=2)) + print("\nšŸ  Server capabilities:") + print(json.dumps(capabilities_dict, indent=2)) + print("\nšŸ¤ Matching capabilities:") + print(json.dumps(matching_capabilities, indent=2)) + # Create successful ping response with matched capabilities + capabilities_json = json.dumps(matching_capabilities, indent=2) - - - + response_xml = f''' + + + + {capabilities_json} + + {capabilities_json} +''' @@ -400,7 +444,11 @@ class AlpineBitsServer: # Handle the request try: - return await action_instance.handle(request_action_name, request_xml, version_enum) + # Special case for ping action - pass server capabilities + if capability_name == "action_OTA_Ping": + return await action_instance.handle(request_action_name, request_xml, version_enum, self.capabilities) + else: + return await action_instance.handle(request_action_name, request_xml, version_enum) except Exception as e: return AlpineBitsResponse( f"Error: Internal server error while processing {request_action_name}: {str(e)}", @@ -487,7 +535,7 @@ async def main(): test_cases = [ ("OTA_Ping:Handshaking", "2024-10"), ("OTA_Read:GuestRequests", "2024-10"), - ("OTA_Read:GuestRequest", "2022-10"), + ("OTA_Read:GuestRequests", "2022-10"), ("OTA_HotelAvailNotif", "2024-10"), ("UnknownAction", "2024-10"), ("OTA_Ping:Handshaking", "unsupported-version") diff --git a/test_handshake.py b/test_handshake.py new file mode 100644 index 0000000..47ff199 --- /dev/null +++ b/test_handshake.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +""" +Test the handshake functionality with the real AlpineBits sample file. +""" + +import asyncio +from alpine_bits_python.alpinebits_server import AlpineBitsServer + +async def main(): + print("šŸ”„ Testing AlpineBits Handshake with Sample File") + print("=" * 60) + + # Create server instance + server = AlpineBitsServer() + + # Read the sample handshake request + with open("AlpineBits-HotelData-2024-10/files/samples/Handshake/Handshake-OTA_PingRQ.xml", "r") as f: + ping_request_xml = f.read() + + print("šŸ“¤ Sending handshake request...") + + # Handle the ping request + response = await server.handle_request("OTA_Ping:Handshaking", ping_request_xml, "2024-10") + + print(f"\nšŸ“„ Response Status: {response.status_code}") + print(f"šŸ“„ Response XML:\n{response.xml_content}") + +if __name__ == "__main__": + asyncio.run(main()) \ No newline at end of file