// bring in a helper-library called axios that makes API request code cleaner and easier.
const axios = require('axios');
// Your API token is brought in from a system environment variable. Use the token provided to you by the workshop coordinator. The one-time only setup step is adding the key-value pair: https://runkit.com/settings/environment
const mytoken = process.env.TOKEN;
const myAppId = process.env.APPID;
// .airportCode and .checkIn and .checkOut change where hotel properties are found in the API request
const searchCriteria = {
location: {
airportCode: 'LAS',
stateCode: 'NV',
countryCode: 'US',
},
date: {
checkIn: '2019-09-01',
checkOut: '2019-09-05',
},
};
// sets up the request search parameters - note how it uses searchCriteria several important attributes
const requestPayload = {
GetHotelAvailRQ: {
SearchCriteria: {
OffSet: 1,
SortBy: 'SabreRating',
SortOrder: 'DESC',
PageSize: 100,
AvailRatesOnly: true,
GeoSearch: {
GeoRef: {
Radius: 50,
UOM: 'MI',
RefPoint: {
Value: searchCriteria.location.airportCode,
ValueContext: 'CODE',
RefPointType: '6',
StateProv: searchCriteria.location.stateCode,
CountryCode: searchCriteria.location.countryCode,
},
},
},
RateInfoRef: {
ConvertedRateInfoOnly: false,
CurrencyCode: 'USD',
BestOnly: '2',
PrepaidQualifier: 'IncludePrepaid',
StayDateRange: {
StartDate: searchCriteria.date.checkIn,
EndDate: searchCriteria.date.checkOut,
},
Rooms: {
Room: [
{
Index: 1,
Adults: 2,
},
],
},
InfoSource: '100,110,112,113',
},
HotelPref: {
AmenityCodes: {
Inclusive: true,
AmenityCode: [
'15',
],
},
SabreRating: {
Min: '3',
Max: '5',
},
},
ImageRef: {
Type: 'MEDIUM',
LanguageCode: 'EN',
},
},
}
};
// make the API request
axios({
method: 'post',
url: 'https://api-crt.cert.havail.sabre.com/v2.0.0/get/hotelavail',
data: JSON.stringify(requestPayload),
headers: {
'content-type': 'application/json',
accept: 'application/json',
authorization: `Bearer ${mytoken}`,
'Application-ID': myAppId
}
})
.then(function (response) {
// HTTP 200
console.log(response.data);
DisplayNames(response.data);
})
.catch(function (error) {
// HTTP 400 or 500
console.log('Something went wrong');
console.log(`${error.response.status} ... ${error.response.statusText}`);
console.log(error.response.data);
});
// loop through the collection of Hotel Availability Information to display the property name
function DisplayNames(data) {
const hotelInfo = data.GetHotelAvailRS.HotelAvailInfos.HotelAvailInfo;
hotelInfo.forEach((hotel) => {
console.log(`${hotel.HotelInfo.HotelName}`);
});
}