Skip to content

Commit

Permalink
chore: add car code examples (#12)
Browse files Browse the repository at this point in the history
Co-authored-by: johnli <[email protected]>
  • Loading branch information
johnli111 and johnli authored Nov 27, 2024
1 parent 47e4848 commit 76bfd5a
Show file tree
Hide file tree
Showing 5 changed files with 408 additions and 2 deletions.
10 changes: 10 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ Currently, the following scenarios are included:

This example demonstrates how to search for availability calendars with property IDs in Lodging Availability Calendar API.

### Car

- [`CarDetailsQuickStartScenario.java`](src/main/java/com/expediagroup/sdk/xap/examples/scenarios/car/CarListingsQuickStartScenario.java):

This example demonstrates how to search for cars using an airport keyword with filters applied in the Car Listings API.

- [`CarDetailsQuickStartScenario.java`](src/main/java/com/expediagroup/sdk/xap/examples/scenarios/car/CarDetailsQuickStartScenario.java):

This example demonstrates how to search for car details using the offerToken obtained from the car listing in the Car Details API.

We are continuously adding more scenarios to demonstrate the usage of other XAP APIs.

## Requirements
Expand Down
4 changes: 2 additions & 2 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<xap-java-sdk.sdk.version>0.0.15-SNAPSHOT</xap-java-sdk.sdk.version>
<xap-java-sdk.sdk.version>0.0.17-SNAPSHOT</xap-java-sdk.sdk.version>
</properties>

<repositories>
Expand Down Expand Up @@ -88,4 +88,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.expediagroup.sdk.xap.examples;

import com.expediagroup.sdk.xap.examples.scenarios.car.CarDetailsQuickStartScenario;
import com.expediagroup.sdk.xap.examples.scenarios.car.CarListingsQuickStartScenario;
import com.expediagroup.sdk.xap.examples.scenarios.lodging.AvailabilityCalendarsQuickStartScenario;
import com.expediagroup.sdk.xap.examples.scenarios.lodging.ListingsQuickStartScenario;
import com.expediagroup.sdk.xap.examples.scenarios.lodging.QuotesQuickStartScenario;
Expand Down Expand Up @@ -50,6 +52,16 @@ public static void main(String[] args) {
logger.info(
"=============================== End of Lodging Scenarios ==============================");

logger.info(
"============================== Running Car Scenarios =============================");
CarListingsQuickStartScenario carListingsQuickStartScenario = new CarListingsQuickStartScenario();
carListingsQuickStartScenario.run();

CarDetailsQuickStartScenario carDetailsQuickStartScenario = new CarDetailsQuickStartScenario();
carDetailsQuickStartScenario.run();
logger.info(
"=============================== End of Car Scenarios ==============================");

System.exit(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package com.expediagroup.sdk.xap.examples.scenarios.car;

import com.expediagroup.sdk.core.model.Response;
import com.expediagroup.sdk.xap.client.XapClient;
import com.expediagroup.sdk.xap.examples.scenarios.XapScenario;
import com.expediagroup.sdk.xap.models.CarDetails;
import com.expediagroup.sdk.xap.models.CarDetailsResponse;
import com.expediagroup.sdk.xap.models.CarListingsResponse;
import com.expediagroup.sdk.xap.models.VehicleDetails;
import com.expediagroup.sdk.xap.operations.GetCarDetailsOperation;
import com.expediagroup.sdk.xap.operations.GetCarDetailsOperationParams;
import com.expediagroup.sdk.xap.operations.GetCarsListingsOperation;
import com.expediagroup.sdk.xap.operations.GetCarsListingsOperationParams;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This example demonstrates how to retrieve CarDetails information using the Car Details DeepLink obtained from the car listing.
*/
public class CarDetailsQuickStartScenario implements XapScenario {

private static final Logger LOGGER = LoggerFactory.getLogger(CarDetailsQuickStartScenario.class);

/**
* Summary: main function.
*/
public static void main(String[] args) {
new CarListingsQuickStartScenario().run();
new CarDetailsQuickStartScenario().run();
System.exit(0);
}

@Override
public void run() {
LOGGER.info("========== Start QuickStartScenario ==========");

LOGGER.info("========== Car Listing Start ==========");

// This example demonstrates how to obtain the Car Details Deep Link from the CarListing.
// For information on using car search, refer to car/shopping/listings/ListingsQuickStartExample.
List<GetCarsListingsOperationParams.Links> linksList = new ArrayList<>();
linksList.add(GetCarsListingsOperationParams.Links.AD);
linksList.add(GetCarsListingsOperationParams.Links.WS);
linksList.add(GetCarsListingsOperationParams.Links.WD);
GetCarsListingsOperationParams getCarsListingsOperationParams = GetCarsListingsOperationParams.builder()
.partnerTransactionId("EWSCar_Automation")
.dropOffAirport("MCO")
.pickupAirport("MCO")
.pickupTime(LocalDateTime.now().plusDays(5))
.dropOffTime(LocalDateTime.now().plusDays(8))
.limit(1)
.links(linksList)
.build();

XapClient xapClient = createClient();
GetCarsListingsOperation getCarsListingsOperation = new GetCarsListingsOperation(getCarsListingsOperationParams);
Response<CarListingsResponse> carListingsResponse = xapClient.execute(getCarsListingsOperation);

LOGGER.info("========== Car Listing Property End ==========");

// Iterate through the car listings and retrieve the Car Details Deep Link.
LOGGER.info("========== Car Details Start ==========");
Objects.requireNonNull(carListingsResponse.getData().getCars()).forEach(car -> {
if (!car.getLinks().get("ApiDetails").getHref().isEmpty()) {
// Retrieve the Car Details Deep Link from the car listing.
LOGGER.info("Car Details Deep Link: " + car.getLinks().get("ApiDetails").getHref());
String[] strings = splitUrl(car.getLinks().get("ApiDetails").getHref());

// Retrieve the Car Details information using the Car Details Deep Link,which include(offerToken, price, currency)
GetCarDetailsOperationParams getCarDetailsOperationParams = GetCarDetailsOperationParams.builder().partnerTransactionId("EWSCar_Automation")
.offerToken(strings[0]).price(strings[1]).currency(strings[2]).build();

// Execute the operation and get the CarDetailsResponse
LOGGER.info("========== Executing GetCarDetailsOperation ==========");
CarDetailsResponse carDetailsResponse = xapClient.execute(
new GetCarDetailsOperation(getCarDetailsOperationParams)).getData();
LOGGER.info("========== GetCarDetailsOperation Executed ==========");

if (carDetailsResponse == null || carDetailsResponse.getLinks() == null) {
throw new IllegalStateException("No car found.");
}

LOGGER.info("========== Car Properties Start ==========");

// The CarDetailsResponse contains a transaction ID for troubleshooting
LOGGER.info("Transaction ID: {}", carDetailsResponse.getTransactionId());

// List Container for warning messages
if (carDetailsResponse.getWarnings() != null) {
LOGGER.info("Warnings: {}", carDetailsResponse.getWarnings());
}

// Details of requested car.
// Details refer to the CarDetails Section table below.
if (carDetailsResponse.getValidFormsOfPayment() != null) {
LOGGER.info("Valid Forms Of Payment: {}", carDetailsResponse.getValidFormsOfPayment());
}

// A map of links to other Car APIs.
if (carDetailsResponse.getLinks() != null) {
LOGGER.info("Links: {}", carDetailsResponse.getLinks());
}

// Specific information for a car.
CarDetails carDetails = carDetailsResponse.getCarDetails();
VehicleDetails vehicleDetails = carDetails.getVehicleDetails();
if (vehicleDetails.getMake() != null ) {
//Car manufacturer and model.
LOGGER.info("Make: {}", vehicleDetails.getMake());
}

// Car category and type.
LOGGER.info("Car Class: {}", vehicleDetails.getCarClass());

// Minimal car door count.
if (vehicleDetails.getMinDoors() != null) {
LOGGER.info("Min Doors: {}", vehicleDetails.getMinDoors());
}

// Maximum car door count.
if (vehicleDetails.getMaxDoors() != null) {
LOGGER.info("Max Doors: {}", vehicleDetails.getMaxDoors());
}

// Car fuel information.
if (vehicleDetails.getFuelLevel() != null) {
// Fuel level of the car.
LOGGER.info("Fuel Level: {}", vehicleDetails.getFuelLevel());
}

// Car category.
LOGGER.info("Car Category: {}", vehicleDetails.getCarCategory());

// Car type.
LOGGER.info("Car Type: {}", vehicleDetails.getCarType());

// Car transmission and drive.
LOGGER.info("Transmission Drive: {}", vehicleDetails.getTransmissionDrive());

// Car fuel type and whether Air Conditioning is included.
LOGGER.info("Fuel AC: {}", vehicleDetails.getFuelAC());

// Capacity for car's properties, which include AdultCount, ChildCount, SmallLuggageCount and LargeLuggageCount.
if (vehicleDetails.getCapacity() != null) {
LOGGER.info("Capacity: {}", vehicleDetails.getCapacity());
}

// Car rental supplier.
LOGGER.info(" : {}", carDetails.getSupplier());

// Pickup information
LOGGER.info("Pickup Details: {}", carDetails.getPickupDetails());

// Drop off information, include drop off date time and drop off location information.
LOGGER.info("Drop Off Details: {}", carDetails.getDropOffDetails());

// The rate information for a car product.
LOGGER.info("Rate Details: {}", carDetails.getRateDetails());

// Base price per rate period.
LOGGER.info("Price: {}", carDetails.getPrice());

// List of TaxesAndFees Details.
if (carDetails.getTaxesAndFeesDetails() != null) {
LOGGER.info("Taxes And Fees Details: {}", carDetails.getTaxesAndFeesDetails());
}

// List of ExtraFeesDetails
if (carDetails.getExtraFeesDetails() != null) {
LOGGER.info("Extra Fees Details: {}", carDetails.getExtraFeesDetails());
}

// ReferencePrice is the totalPrice for the comparable standalone car, when there is a discounted car or need to show strike through pricing.
if (carDetails.getReferencePrice() != null) {
LOGGER.info("Reference Price: {}", carDetails.getReferencePrice());
}

// List of additional fees including both mandatory and optional fees such as young driver fee/drop off fee /CollisionDamageWaiver.
if (carDetails.getAdditionalFees() != null) {
LOGGER.info("Additional Fees: {}", carDetails.getAdditionalFees());
}

// Description and costs of any optional special equipment that may be rented with the car.
if (carDetails.getSpecialEquipments() != null) {
LOGGER.info("Special Equipments: {}", carDetails.getSpecialEquipments());
}

// Limitations that are part of this rental agreement.
if (carDetails.getRentalLimits() != null) {
LOGGER.info("Rental Limits: {}", carDetails.getRentalLimits());
}

// Cancellation Policy Container.
LOGGER.info("Cancellation Policy: {}", carDetails.getCancellationPolicy());

// Container for no show penalty
if (carDetails.getNoShowPenalty() != null) {
LOGGER.info("No Show Penalty: {}", carDetails.getNoShowPenalty());
}

// A list of policies that apply to this car rental.
if (carDetails.getCarPolicies() != null) {
LOGGER.info("Policies: {}", carDetails.getCarPolicies());
}

// List of image resources of the car product.
if (carDetails.getImages() != null) {
LOGGER.info("Images: {}", carDetails.getImages());
}

LOGGER.info("========== Property End ==========");
}

});

LOGGER.info("========== End QuickStartExample ==========");
}

public static String[] splitUrl(String url) {
String[] parts = url.split("\\?");
String base = parts[0];
String query = parts[1];

String offerToken = base.substring(base.lastIndexOf("/") + 1);
String[] queryParams = query.split("&");
String price = queryParams[0].split("=")[1];
String currency = queryParams[1].split("=")[1];

return new String[]{offerToken, price, currency};
}

}
Loading

0 comments on commit 76bfd5a

Please sign in to comment.