Skip to content

Commit ce60ff5

Browse files
authored
chore: add code examples (#8)
1 parent c4dfa02 commit ce60ff5

File tree

8 files changed

+634
-0
lines changed

8 files changed

+634
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ You can read more about the XAP API on [Developer Hub for XAP](https://developer
1212
The XAP SDK makes integrating simple, saving development time so you can focus more on getting your product to market
1313
and less on the technical details of the API.
1414

15+
### Code Examples
16+
17+
You can find code examples for multiple use cases in the [examples](examples) directory.
18+
19+
---
1520

1621
## Support
1722

examples/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# XAP Java SDK Examples
2+
3+
This repository contains examples of how to use the Expedia Group XAP Java SDK. The examples are
4+
written in Java and use Maven for dependency management.
5+
6+
## Examples
7+
8+
The example implementation provided in the examples package demonstrates different scenarios
9+
utilizing various XAP APIs through the SDK.
10+
11+
Currently, the following scenarios are included:
12+
13+
### Lodging
14+
15+
#### Shopping
16+
17+
##### Listings
18+
19+
- [
20+
`QuickStartExample.java`](src/main/java/com/expediagroup/sdk/xap/examples/scenarios/lodging/shopping/listings/QuickStartExample.java):
21+
This example demonstrates how to search for properties with a location keyword with filters
22+
applied.
23+
24+
We are continuously adding more scenarios to demonstrate the usage of other XAP APIs.
25+
26+
## Requirements
27+
28+
- Ensure you have a valid API key and secret from Expedia Group.
29+
Check [Getting started with XAP](https://developers.expediagroup.com/xap/products/xap/set-up/getting-started)
30+
for more info.
31+
- Java 1.8 or higher
32+
- Maven
33+
34+
## Setup
35+
36+
1. Clone the repository.
37+
2. Navigate to the project directory `examples`.
38+
3. Set your API key and secret in the environment variables `XAP_API_KEY` and `XAP_API_PASSWORD`.
39+
> **Note:** You can also replace the value of `xapApiKey` and `xapApiPassword` in the
40+
> [`Constants.java`](src/main/java/com/expediagroup/sdk/xap/examples/Constants.java) file, but we
41+
> recommend using environment variables out of security concerns.
42+
4. Run `mvn clean install` to build the project and install the dependencies including the XAP SDK.
43+
44+
## Running the Examples
45+
46+
To run the examples, simply navigate to the example class you want to run and execute the `main`
47+
method.
48+
49+
## License
50+
51+
This project is licensed under the Apache License v2.0 - see the [LICENSE](../LICENSE) for details.

examples/pom.xml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright (C) 2024 Expedia, Inc.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<groupId>com.expediagroup</groupId>
24+
<artifactId>xap-java-sdk-examples</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
<name>XAP Java SDK Examples</name>
27+
<description>Expedia Group XAP Java SDK Examples</description>
28+
29+
<properties>
30+
<java.version>1.8</java.version>
31+
<maven.compiler.source>1.8</maven.compiler.source>
32+
<maven.compiler.target>1.8</maven.compiler.target>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
<xap-java-sdk.sdk.version>0.0.8-SNAPSHOT</xap-java-sdk.sdk.version>
35+
</properties>
36+
37+
<repositories>
38+
<repository>
39+
<id>oss.sonatype.org-snapshot</id>
40+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
41+
<releases>
42+
<enabled>false</enabled>
43+
</releases>
44+
<snapshots>
45+
<enabled>true</enabled>
46+
</snapshots>
47+
</repository>
48+
</repositories>
49+
50+
<dependencies>
51+
<dependency>
52+
<groupId>com.expediagroup</groupId>
53+
<artifactId>xap-sdk</artifactId>
54+
<version>${xap-java-sdk.sdk.version}</version>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>org.apache.logging.log4j</groupId>
59+
<artifactId>log4j-api</artifactId>
60+
<version>2.23.1</version>
61+
</dependency>
62+
63+
<dependency>
64+
<groupId>org.apache.logging.log4j</groupId>
65+
<artifactId>log4j-slf4j2-impl</artifactId>
66+
<version>2.23.1</version>
67+
</dependency>
68+
</dependencies>
69+
70+
<build>
71+
<plugins>
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-checkstyle-plugin</artifactId>
75+
<version>3.5.0</version>
76+
<configuration>
77+
<configLocation>google_checks.xml</configLocation>
78+
<consoleOutput>true</consoleOutput>
79+
<violationSeverity>warning</violationSeverity>
80+
</configuration>
81+
<executions>
82+
<execution>
83+
<goals>
84+
<goal>check</goal>
85+
</goals>
86+
</execution>
87+
</executions>
88+
</plugin>
89+
</plugins>
90+
</build>
91+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (C) 2024 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;
18+
19+
import static com.expediagroup.sdk.dependencies.org.hibernate.validator.internal.util.Contracts.assertNotEmpty;
20+
21+
import com.expediagroup.sdk.xap.client.XapClient;
22+
23+
/**
24+
Constants.
25+
**/
26+
public class Constants {
27+
28+
public static final XapClient XAP_CLIENT;
29+
public static final String PARTNER_TRANSACTION_ID = "xap-java-sdk-examples";
30+
31+
static {
32+
String xapApiKey = System.getenv("XAP_API_KEY");
33+
String xapApiPassword = System.getenv("XAP_API_PASSWORD");
34+
assertNotEmpty(xapApiKey, "XAP_API_KEY is missing, please set it in your environment");
35+
assertNotEmpty(xapApiPassword,
36+
"XAP_API_PASSWORD is missing, please set it in your environment");
37+
XAP_CLIENT = XapClient.builder()
38+
.key(System.getenv("XAP_API_KEY"))
39+
.secret(System.getenv("XAP_API_PASSWORD"))
40+
.build();
41+
}
42+
43+
private Constants() {
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.expediagroup.sdk.xap.examples.scenarios.lodging.shopping.availabilitycalendars;
2+
3+
import com.expediagroup.sdk.xap.examples.Constants;
4+
import com.expediagroup.sdk.xap.models.AvailabilityCalendarResponse;
5+
import com.expediagroup.sdk.xap.operations.GetLodgingAvailabilityCalendarsOperation;
6+
import com.expediagroup.sdk.xap.operations.GetLodgingAvailabilityCalendarsOperationParams;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
/**
11+
* This example demonstrates how to use Availability Calendar api with simple search.
12+
*/
13+
public class AvailabilityCalendarsQuickStartExample {
14+
15+
private static final Logger LOGGER =
16+
LoggerFactory.getLogger(AvailabilityCalendarsQuickStartExample.class);
17+
18+
/**
19+
* Summary: main function.
20+
*/
21+
public static void main(String[] args) {
22+
LOGGER.info("========== Start QuickStartExample ==========");
23+
24+
// This example Returns the availability of each day for a range of dates for given Expedia
25+
// lodging properties.
26+
27+
// Build the query parameters with GetLodgingAvailabilityCalendarsOperationParams
28+
GetLodgingAvailabilityCalendarsOperationParams availabilityCalendarsOperationParams =
29+
GetLodgingAvailabilityCalendarsOperationParams.builder()
30+
.partnerTransactionId(Constants.PARTNER_TRANSACTION_ID)
31+
//Comma-separated list of Expedia Property IDs.
32+
.propertyIds("87704892,36238803")
33+
.build();
34+
35+
// Execute the operation and get the AvailabilityCalendarsResponse
36+
LOGGER.info("========== Executing GetLodgingAvailabilityCalendarsOperation ==========");
37+
38+
AvailabilityCalendarResponse availCalendarResponse = Constants.XAP_CLIENT.execute(
39+
new GetLodgingAvailabilityCalendarsOperation(availabilityCalendarsOperationParams))
40+
.getData();
41+
// If you want to use the async method, you can use the following code:
42+
// ---------------------------------------------------------------
43+
// CompletableFuture<Response<AvailabilityCalendarResponse>> completableFuture =
44+
// Constants.XAP_CLIENT.executeAsync(
45+
// new GetLodgingAvailabilityCalendarsOperation(availabilityCalendarsOperationParams));
46+
// completableFuture.thenAccept(availCalendarResponse -> {
47+
// // Your code here
48+
// });
49+
// ---------------------------------------------------------------
50+
51+
LOGGER.info("========== GetLodgingAvailabilityCalendarsOperation Executed ==========");
52+
53+
if (availCalendarResponse == null || availCalendarResponse.getAvailabilityCalendars() == null
54+
|| availCalendarResponse.getAvailabilityCalendars().isEmpty()) {
55+
throw new IllegalStateException("No properties found.");
56+
}
57+
58+
// The AvailabilityCalendarsResponse contains a transaction ID for troubleshooting
59+
LOGGER.info("Transaction ID: {}", availCalendarResponse.getTransactionId());
60+
61+
// To access the properties, iterate through the list of properties
62+
availCalendarResponse.getAvailabilityCalendars().forEach(availCalendar -> {
63+
LOGGER.info("========== Property:{} Start ==========", availCalendar.getPropertyId());
64+
65+
// Availability of property: A string of codes that shows property availability, one for every
66+
// day in the specified date range.
67+
// Valid values include Y (available) and N (unavailable).
68+
LOGGER.info("Availability: {}", availCalendar.getAvailability());
69+
70+
LOGGER.info("========== Property End ==========");
71+
});
72+
}
73+
}

0 commit comments

Comments
 (0)