在SabreRedWorkspace的Web RedApp中校验PNR存在性的技术求助
I get it—finding docs for Web RedApps can be tricky since most resources focus on the newer Native APIs or REST APIs. Let’s walk through how to validate an active PNR before adding a passive segment in your Angular-based Web RedApp, using the Sabre Red App SDK for Web.
Approach Overview
Web RedApps run within the Sabre Red Workspace context, so we can leverage the SDK’s built-in services to either check the current session state directly or execute low-level Sabre commands to verify PNR existence. Here’s a step-by-step breakdown:
Step 1: Inject Required SDK Services
First, you’ll need to inject the core services from the Sabre Web SDK into your Angular component or service. These handle session context and command execution:
import { Component, OnInit } from '@angular/core'; // Adjust imports based on your SDK version (names might vary slightly) import { SabredContextService, CommandExecutorService } from 'sabre-red-app-sdk-web'; @Component({ selector: 'app-passive-segment', templateUrl: './passive-segment.component.html', styleUrls: ['./passive-segment.component.css'] }) export class PassiveSegmentComponent implements OnInit { constructor( private sabreContext: SabredContextService, private cmdExecutor: CommandExecutorService ) {} ngOnInit(): void {} }
Step 2: Check for Active PNR
There are two reliable ways to validate an active PNR—pick the one that works best with your SDK version:
Option 1: Use Session Context (Simplest)
Many Web RedApp SDK versions expose session state details that include whether an active PNR is open. Use the context service to fetch this state:
async hasActivePNR(): Promise<boolean> { try { const sessionState = await this.sabreContext.getSessionState(); // The exact property name might differ—check your SDK's type definitions return sessionState?.activePNR?.isOpen || false; } catch (err) { console.error('Failed to fetch session state:', err); return false; } }
Option 2: Execute a Sabre Command (Fallback)
If the context service doesn’t expose PNR status, execute the *PNR command (a standard Sabre command to retrieve PNR details) and parse the response to check if a PNR exists:
async checkPNRWithCommand(): Promise<boolean> { try { const commandResponse = await this.cmdExecutor.executeCommand('*PNR'); // Look for keywords in the response to determine if PNR exists if (commandResponse.includes('PNR NOT FOUND') || commandResponse.includes('NO ACTIVE PNR')) { return false; } return true; } catch (err) { console.error('Error executing *PNR command:', err); return false; } }
Step 3: Validate Before Adding Passive Segment
Integrate the validation into your passive segment addition workflow. If no active PNR exists, alert the user and stop execution:
async addPassiveSegment(): Promise<void> { // Use either validation method from above const isPNRActive = await this.hasActivePNR(); if (!isPNRActive) { alert('No valid active PNR found. Please open or retrieve a PNR first.'); return; } // Proceed with adding your passive segment here // Example: Execute the Sabre command for passive segment creation await this.cmdExecutor.executeCommand('PAX PASSIVE...'); // Replace with your actual command syntax }
Important Tips
- SDK Version Compatibility: Double-check your SDK’s documentation (even if sparse) for exact service names and method signatures—older versions might use
SessionServiceinstead ofSabredContextService. - Error Handling: Always wrap SDK calls in try/catch blocks to handle session timeouts or network issues gracefully.
- Testing: Test this logic in the Sabre Red Workspace development environment to confirm the session state properties or command responses match your expectations.
内容的提问来源于stack exchange,提问作者lboisset

