|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Expedia, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.expediagroup.sdk.xap.examples.scenarios.flight; |
| 18 | + |
| 19 | +import com.expediagroup.sdk.xap.client.XapClient; |
| 20 | +import com.expediagroup.sdk.xap.examples.scenarios.XapScenario; |
| 21 | +import com.expediagroup.sdk.xap.models.FlightSearchResponse; |
| 22 | +import com.expediagroup.sdk.xap.operations.GetFlightListingsOperation; |
| 23 | +import com.expediagroup.sdk.xap.operations.GetFlightListingsOperationParams; |
| 24 | +import java.time.LocalDate; |
| 25 | +import java.util.Arrays; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | + |
| 30 | +/** |
| 31 | + * This example demonstrates how to search for flight listings for a one-way, |
| 32 | + * round-trip & mutli-stop with adults, seniors, and childrens. |
| 33 | + */ |
| 34 | + |
| 35 | +public class FlightListingExample implements XapScenario { |
| 36 | + |
| 37 | + private static final Logger LOGGER = |
| 38 | + LoggerFactory.getLogger(FlightListingExample.class); |
| 39 | + |
| 40 | + |
| 41 | + public static void main(String[] args) { |
| 42 | + new FlightListingExample().run(); |
| 43 | + System.exit(0); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void run() { |
| 48 | + LOGGER.info( |
| 49 | + "========= Running FlightListingsQuickStartScenario ============="); |
| 50 | + |
| 51 | + LOGGER.info( |
| 52 | + "============= Executing GetFlightListingsOperation (One-Way) ========="); |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + GetFlightListingsOperationParams getFlightListingsOperationParams = |
| 57 | + GetFlightListingsOperationParams.builder() |
| 58 | + .partnerTransactionID("txn-123-4") |
| 59 | + .segment1Origin("LAS") |
| 60 | + .segment1Destination("FLL") |
| 61 | + .segment1DepartureDate(LocalDate.of(2025, 5, 1)) |
| 62 | + .adult(1) |
| 63 | + .senior(1) |
| 64 | + .childrenAges(Arrays.asList(4, 5)) |
| 65 | + .build(); |
| 66 | + |
| 67 | + XapClient xapClient = createClient(); |
| 68 | + |
| 69 | + FlightSearchResponse flightListingsResponse = |
| 70 | + xapClient.execute(new GetFlightListingsOperation(getFlightListingsOperationParams)) |
| 71 | + .getData(); |
| 72 | + |
| 73 | + LOGGER.info("========= GetFlightListingsOperation (One-Way) Executed ========"); |
| 74 | + |
| 75 | + if (flightListingsResponse == null || flightListingsResponse.getSegments() == null |
| 76 | + || flightListingsResponse.getSegments().isEmpty()) { |
| 77 | + throw new IllegalStateException("No flight found."); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + LOGGER.info("Transaction ID: {}", flightListingsResponse.getTransactionId()); |
| 82 | + |
| 83 | + if (flightListingsResponse.getSegments() != null) { |
| 84 | + flightListingsResponse.getSegments().forEach(segment -> { |
| 85 | + LOGGER.info("======================== Flight Segment Start ========================"); |
| 86 | + LOGGER.info("Segment ID: {}", segment.getSegmentId()); |
| 87 | + LOGGER.info("Flight Duration: {}", segment.getFlightDuration()); |
| 88 | + LOGGER.info("Total Stops: {}", segment.getTotalStops()); |
| 89 | + LOGGER.info("Departure Arrival Day Difference: {}", |
| 90 | + segment.getDepartureArrivalDayDifference()); |
| 91 | + LOGGER.info("Seats Left: {}", segment.getSeatsLeft()); |
| 92 | + LOGGER.info("Fare Type: {}", segment.getFareType()); |
| 93 | + |
| 94 | + if (segment.getLegs() != null) { |
| 95 | + segment.getLegs().forEach(leg -> { |
| 96 | + LOGGER.info("-------------------- Leg Start --------------------"); |
| 97 | + |
| 98 | + if (leg.getDepartureAirport() != null) { |
| 99 | + LOGGER.info("Departure Airport Code: {}", |
| 100 | + leg.getDepartureAirport().getCode()); |
| 101 | + LOGGER.info("Departure Airport Name: {}", |
| 102 | + leg.getDepartureAirport().getName()); |
| 103 | + LOGGER.info("Departure City: {}", |
| 104 | + leg.getDepartureAirport().getCity()); |
| 105 | + LOGGER.info("Departure Province: {}", |
| 106 | + leg.getDepartureAirport().getProvince()); |
| 107 | + LOGGER.info("Departure Country: {}", |
| 108 | + leg.getDepartureAirport().getCountry()); |
| 109 | + LOGGER.info("Departure Latitude: {}", |
| 110 | + leg.getDepartureAirport().getLatitude()); |
| 111 | + LOGGER.info("Departure Longitude: {}", |
| 112 | + leg.getDepartureAirport().getLongitude()); |
| 113 | + } |
| 114 | + |
| 115 | + if (leg.getArrivalAirport() != null) { |
| 116 | + LOGGER.info("Arrival Airport Code: {}", |
| 117 | + leg.getArrivalAirport().getCode()); |
| 118 | + LOGGER.info("Arrival Airport Name: {}", |
| 119 | + leg.getArrivalAirport().getName()); |
| 120 | + LOGGER.info("Arrival City: {}", |
| 121 | + leg.getArrivalAirport().getCity()); |
| 122 | + LOGGER.info("Arrival Province: {}", |
| 123 | + leg.getArrivalAirport().getProvince()); |
| 124 | + LOGGER.info("Arrival Country: {}", |
| 125 | + leg.getArrivalAirport().getCountry()); |
| 126 | + LOGGER.info("Arrival Latitude: {}", |
| 127 | + leg.getArrivalAirport().getLatitude()); |
| 128 | + LOGGER.info("Arrival Longitude: {}", |
| 129 | + leg.getArrivalAirport().getLongitude()); |
| 130 | + } |
| 131 | + LOGGER.info("Departure Date Time: {}", |
| 132 | + leg.getDepartureDateTime()); |
| 133 | + LOGGER.info("Arrival Date Time: {}", |
| 134 | + leg.getArrivalDateTime()); |
| 135 | + LOGGER.info("Flight Number: {}", |
| 136 | + leg.getFlightNumber()); |
| 137 | + LOGGER.info("Marketing Airline Code: {}", |
| 138 | + leg.getMarketingAirlineCode()); |
| 139 | + LOGGER.info("Marketing Airline Name: {}", |
| 140 | + leg.getMarketingAirlineName()); |
| 141 | + LOGGER.info("Equipment Code: {}", |
| 142 | + leg.getEquipmentCode()); |
| 143 | + LOGGER.info("Flight On Time Percentage: {}", |
| 144 | + leg.getFlightOnTimePercentage()); |
| 145 | + LOGGER.info("Equipment Name: {}", |
| 146 | + leg.getEquipmentName()); |
| 147 | + LOGGER.info("Flight Duration (Leg): {}", |
| 148 | + leg.getFlightDuration()); |
| 149 | + |
| 150 | + if (leg.getFlightDistance() != null) { |
| 151 | + LOGGER.info("Flight Distance Value: {}", |
| 152 | + leg.getFlightDistance().getValue()); |
| 153 | + LOGGER.info("Flight Distance Unit: {}", |
| 154 | + leg.getFlightDistance().getUnit()); |
| 155 | + } |
| 156 | + LOGGER.info("Booking Code: {}", |
| 157 | + leg.getBookingCode()); |
| 158 | + LOGGER.info("Cabin Class: {}", |
| 159 | + leg.getCabinClass()); |
| 160 | + LOGGER.info("Fare Basis Code: {}", |
| 161 | + leg.getFareBasisCode()); |
| 162 | + LOGGER.info("Meal Options: {}", |
| 163 | + leg.getMealOptions()); |
| 164 | + LOGGER.info("Amenities: {}", |
| 165 | + leg.getAmenities()); |
| 166 | + LOGGER.info("---------- Leg End ----------"); |
| 167 | + }); |
| 168 | + } |
| 169 | + LOGGER.info("=========== Flight Segment End ============"); |
| 170 | + }); |
| 171 | + } |
| 172 | + |
| 173 | + if (flightListingsResponse.getOffers() != null) { |
| 174 | + flightListingsResponse.getOffers().forEach(offer -> { |
| 175 | + LOGGER.info("======================== Flight Offer Start ========================"); |
| 176 | + |
| 177 | + if (offer.getLinks() != null) { |
| 178 | + offer.getLinks().forEach((key, value) -> |
| 179 | + LOGGER.info("Link [{}]: {}", key, value)); |
| 180 | + } |
| 181 | + |
| 182 | + LOGGER.info("Segment IDs: {}", offer.getSegmentIds()); |
| 183 | + |
| 184 | + if (offer.getOfferPrice() != null) { |
| 185 | + |
| 186 | + if (offer.getOfferPrice().getTotalPrice() != null) { |
| 187 | + LOGGER.info("Total Price Value: {}", |
| 188 | + offer.getOfferPrice().getTotalPrice().getValue()); |
| 189 | + LOGGER.info("Total Price Currency: {}", |
| 190 | + offer.getOfferPrice().getTotalPrice().getCurrency()); |
| 191 | + } |
| 192 | + |
| 193 | + if (offer.getOfferPrice().getBasePrice() != null) { |
| 194 | + LOGGER.info("Base Price Value: {}", |
| 195 | + offer.getOfferPrice().getBasePrice().getValue()); |
| 196 | + LOGGER.info("Base Price Currency: {}", |
| 197 | + offer.getOfferPrice().getBasePrice().getCurrency()); |
| 198 | + } |
| 199 | + |
| 200 | + if (offer.getOfferPrice().getTotalTaxes() != null) { |
| 201 | + LOGGER.info("Total Taxes Value: {}", |
| 202 | + offer.getOfferPrice().getTotalTaxes().getValue()); |
| 203 | + LOGGER.info("Total Taxes Currency: {}", |
| 204 | + offer.getOfferPrice().getTotalTaxes().getCurrency()); |
| 205 | + } |
| 206 | + |
| 207 | + if (offer.getOfferPrice().getTotalTaxesAndFees() != null) { |
| 208 | + LOGGER.info("Total Taxes and Fees Value: {}", |
| 209 | + offer.getOfferPrice().getTotalTaxesAndFees().getValue()); |
| 210 | + LOGGER.info("Total Taxes and Fees Currency: {}", |
| 211 | + offer.getOfferPrice().getTotalTaxesAndFees().getCurrency()); |
| 212 | + } |
| 213 | + |
| 214 | + if (offer.getOfferPrice().getAveragePricePerTicket() != null) { |
| 215 | + LOGGER.info("Average Price Per Ticket Value: {}", |
| 216 | + offer.getOfferPrice().getAveragePricePerTicket().getValue()); |
| 217 | + LOGGER.info("Average Price Per Ticket Currency: {}", |
| 218 | + offer.getOfferPrice().getAveragePricePerTicket().getCurrency()); |
| 219 | + LOGGER.info("Average Price Per Ticket Count: {}", |
| 220 | + offer.getOfferPrice().getAveragePricePerTicket().getCount()); |
| 221 | + } |
| 222 | + |
| 223 | + if (offer.getOfferPrice().getPricePerPassengerCategory() != null) { |
| 224 | + offer.getOfferPrice().getPricePerPassengerCategory() |
| 225 | + .forEach(categoryPrice -> { |
| 226 | + LOGGER.info("---- Passenger Category Price Start ----"); |
| 227 | + LOGGER.info("Category: {}", categoryPrice.getCategory()); |
| 228 | + LOGGER.info("Count: {}", categoryPrice.getCount()); |
| 229 | + |
| 230 | + if (categoryPrice.getTotalPrice() != null) { |
| 231 | + LOGGER.info("Total Price Value: {}", |
| 232 | + categoryPrice.getTotalPrice().getValue()); |
| 233 | + LOGGER.info("Total Price Currency: {}", |
| 234 | + categoryPrice.getTotalPrice().getCurrency()); |
| 235 | + } |
| 236 | + |
| 237 | + if (categoryPrice.getBasePrice() != null) { |
| 238 | + LOGGER.info("Base Price Value: {}", |
| 239 | + categoryPrice.getBasePrice().getValue()); |
| 240 | + LOGGER.info("Base Price Currency: {}", |
| 241 | + categoryPrice.getBasePrice().getCurrency()); |
| 242 | + } |
| 243 | + |
| 244 | + if (categoryPrice.getTotalTaxes() != null) { |
| 245 | + LOGGER.info("Total Taxes Value: {}", |
| 246 | + categoryPrice.getTotalTaxes().getValue()); |
| 247 | + LOGGER.info("Total Taxes Currency: {}", |
| 248 | + categoryPrice.getTotalTaxes().getCurrency()); |
| 249 | + } |
| 250 | + LOGGER.info("---- Passenger Category Price End ----"); |
| 251 | + }); |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + |
| 256 | + LOGGER.info("Ticket Type: {}", offer.getTicketType()); |
| 257 | + LOGGER.info("======================== Flight Offer End ========================"); |
| 258 | + }); |
| 259 | + } |
| 260 | + |
| 261 | + if (flightListingsResponse.getSearchCities() != null) { |
| 262 | + flightListingsResponse.getSearchCities().forEach(city -> { |
| 263 | + LOGGER.info("======================== Search City Start ========================"); |
| 264 | + LOGGER.info("City Code: {}", city.getCode()); |
| 265 | + LOGGER.info("City Name: {}", city.getCity()); |
| 266 | + LOGGER.info("Province: {}", city.getProvince()); |
| 267 | + LOGGER.info("Country: {}", city.getCountry()); |
| 268 | + LOGGER.info("======================== Search City End ========================"); |
| 269 | + }); |
| 270 | + } |
| 271 | + |
| 272 | + if (flightListingsResponse.getValidFormsOfPayment() != null) { |
| 273 | + flightListingsResponse.getValidFormsOfPayment().forEach((key, value) -> { |
| 274 | + LOGGER.info("================= Valid Forms of Payment Start [{}] ============", key); |
| 275 | + value.forEach(payment -> { |
| 276 | + LOGGER.info("---- Payment Method Start ----"); |
| 277 | + LOGGER.info("Payment Method: {}", payment.getPaymentMethod()); |
| 278 | + LOGGER.info("Name: {}", payment.getName()); |
| 279 | + LOGGER.info("Fee: {}", payment.getFee()); |
| 280 | + LOGGER.info("Currency: {}", payment.getCurrency()); |
| 281 | + LOGGER.info("---- Payment Method End ----"); |
| 282 | + }); |
| 283 | + LOGGER.info("========== Valid Forms of Payment End [{}] ============", key); |
| 284 | + }); |
| 285 | + } |
| 286 | + |
| 287 | + LOGGER.info( |
| 288 | + "===================== End FlightListingsQuickStartScenario ===================="); |
| 289 | + |
| 290 | + // --- Example for a Round Trip Search --- |
| 291 | + LOGGER.info( |
| 292 | + "\n======== Executing GetFlightListingsOperation (Round Trip) ========"); |
| 293 | + |
| 294 | + GetFlightListingsOperationParams roundTripParams = |
| 295 | + GetFlightListingsOperationParams.builder() |
| 296 | + .partnerTransactionID("txn-123-4") |
| 297 | + .segment1Origin("EWR") |
| 298 | + .segment1Destination("LAX") |
| 299 | + .segment1DepartureDate(LocalDate.of(2025, 5, 1)) |
| 300 | + .segment2Origin("LAX") |
| 301 | + .segment2Destination("EWR") |
| 302 | + .segment2DepartureDate(LocalDate.of(2025, 5, 5)) |
| 303 | + .childrenAges(Arrays.asList(4, 5)) |
| 304 | + .adult(1) |
| 305 | + .senior(1) |
| 306 | + .build(); |
| 307 | + |
| 308 | + FlightSearchResponse roundTripResponse = |
| 309 | + xapClient.execute(new GetFlightListingsOperation(roundTripParams)).getData(); |
| 310 | + |
| 311 | + LOGGER.info( |
| 312 | + "========= GetFlightListingsOperation (Round Trip) Executed =============="); |
| 313 | + |
| 314 | + if (roundTripResponse != null && roundTripResponse.getSegments() != null |
| 315 | + && roundTripResponse.getSegments().size() == 2) { |
| 316 | + LOGGER.info("Round trip search returned {} segments.", |
| 317 | + roundTripResponse.getSegments().size()); |
| 318 | + } |
| 319 | + |
| 320 | + // --- Example for a Multi-Stop Search --- |
| 321 | + LOGGER.info( |
| 322 | + "\n========= Executing GetFlightListingsOperation (Multi-Stop) =========="); |
| 323 | + |
| 324 | + GetFlightListingsOperationParams multiStopParams = |
| 325 | + GetFlightListingsOperationParams.builder() |
| 326 | + .partnerTransactionID("txn-123-4") |
| 327 | + .segment1Origin("LAS") |
| 328 | + .segment1Destination("ATL") |
| 329 | + .segment1DepartureDate(LocalDate.of(2025, 5, 1)) |
| 330 | + .segment2Origin("ORD") |
| 331 | + .segment2Destination("SEA") |
| 332 | + .segment2DepartureDate(LocalDate.of(2025, 5, 5)) |
| 333 | + .adult(1) |
| 334 | + .senior(1) |
| 335 | + .childrenAges(Arrays.asList(4, 5)) |
| 336 | + .build(); |
| 337 | + |
| 338 | + FlightSearchResponse multiStopResponse = |
| 339 | + xapClient.execute(new GetFlightListingsOperation(multiStopParams)).getData(); |
| 340 | + |
| 341 | + LOGGER.info( |
| 342 | + "============ GetFlightListingsOperation (Multi-Stop) Executed ==========="); |
| 343 | + |
| 344 | + if (multiStopResponse != null && multiStopResponse.getSegments() != null |
| 345 | + && multiStopResponse.getSegments().size() == 3) { |
| 346 | + LOGGER.info("Multi-stop search returned {} segments.", |
| 347 | + multiStopResponse.getSegments().size()); |
| 348 | + } |
| 349 | + } |
| 350 | +} |
| 351 | + |
0 commit comments