Handshake first attempt

This commit is contained in:
Jonas Linter
2025-09-25 13:13:22 +02:00
parent b432e5e5cc
commit 03556df10e
2 changed files with 82 additions and 5 deletions

View File

@@ -257,13 +257,57 @@ class PingAction(AlpineBitsAction):
except Exception as e: except Exception as e:
return AlpineBitsResponse(f"Error: Invalid XML request - {str(e)}", HttpStatusCode.BAD_REQUEST) 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'''<?xml version="1.0" encoding="UTF-8"?>
<OTA_PingRS xmlns="http://www.opentravel.org/OTA/2003/05" Version="8.000">
<Success/>
<Warnings>
<Warning Type="11" Status="AlpineBitsHandshake">{capabilities_json}</Warning>
</Warnings>
<EchoData>{capabilities_json}</EchoData>
</OTA_PingRS>'''
@@ -400,6 +444,10 @@ class AlpineBitsServer:
# Handle the request # Handle the request
try: try:
# 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) return await action_instance.handle(request_action_name, request_xml, version_enum)
except Exception as e: except Exception as e:
return AlpineBitsResponse( return AlpineBitsResponse(
@@ -487,7 +535,7 @@ async def main():
test_cases = [ test_cases = [
("OTA_Ping:Handshaking", "2024-10"), ("OTA_Ping:Handshaking", "2024-10"),
("OTA_Read:GuestRequests", "2024-10"), ("OTA_Read:GuestRequests", "2024-10"),
("OTA_Read:GuestRequest", "2022-10"), ("OTA_Read:GuestRequests", "2022-10"),
("OTA_HotelAvailNotif", "2024-10"), ("OTA_HotelAvailNotif", "2024-10"),
("UnknownAction", "2024-10"), ("UnknownAction", "2024-10"),
("OTA_Ping:Handshaking", "unsupported-version") ("OTA_Ping:Handshaking", "unsupported-version")

29
test_handshake.py Normal file
View File

@@ -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())