Skip to content

Commit 3768aa6

Browse files
authored
Fix the implementation of Flight booking API (#72)
* Fix the implementation fo the Flight Offers Price and Flight Create Orders helper methods * Fix typo in comment
1 parent 51f7571 commit 3768aa6

File tree

6 files changed

+290
-111
lines changed

6 files changed

+290
-111
lines changed

src/examples/flight/createorders/FlightCreateOrders.java

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
public class FlightCreateOrders {
1313
/**
1414
* <p>
15-
* An example to call the Flight Create Orders API
16-
* <code>/v1/booking/flight-orders</code> endpoints.
15+
* An example to call the Flight Create Orders API
16+
* <code>/v1/booking/flight-orders</code> endpoints.
1717
* </p>
1818
*
1919
* <p>
20-
* Access via the Amadeus client object.
20+
* Access via the Amadeus client object.
2121
* </p>
2222
*
2323
* <pre>
@@ -26,6 +26,10 @@ public class FlightCreateOrders {
2626
*/
2727
public static void main(String[] args) throws ResponseException {
2828

29+
Amadeus amadeus = Amadeus
30+
.builder("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET")
31+
.build();
32+
2933
Traveler traveler = new Traveler();
3034

3135
traveler.setId("1");
@@ -56,26 +60,28 @@ public static void main(String[] args) throws ResponseException {
5660
travelerArray[0] = traveler;
5761
System.out.println(travelerArray[0]);
5862

59-
Amadeus amadeus = Amadeus
60-
.builder("YOUR_CLIENT_ID","YOUR_CLIENT_SECRET")
61-
.build();
62-
6363
FlightOfferSearch[] flightOffersSearches = amadeus.shopping.flightOffersSearch.get(
64-
Params.with("originLocationCode", "SYD")
65-
.and("destinationLocationCode", "BKK")
66-
.and("departureDate", "2020-11-01")
67-
.and("returnDate", "2020-11-08")
68-
.and("adults", 1)
69-
.and("max", 1));
64+
Params.with("originLocationCode", "PAR")
65+
.and("destinationLocationCode", "NYC")
66+
.and("departureDate", "2020-11-01")
67+
.and("returnDate", "2020-11-08")
68+
.and("adults", 1)
69+
.and("max", 3));
70+
71+
// We price the 2nd flight of the list to confirm the price and the availability
72+
FlightPrice flightPricing = amadeus.shopping.flightOffersSearch.pricing.post(
73+
flightOffersSearches[1]);
74+
7075

71-
FlightOrder order = amadeus.booking.flightOrders.post(flightOffersSearches, travelerArray);
76+
// We book the flight previously priced
77+
FlightOrder order = amadeus.booking.flightOrders.post(flightPricing, travelerArray);
78+
System.out.println(order.getResponse());
7279

73-
// Return CO2 Emission of a given flight
74-
String weight = order.getFlightOffers()[0].getItineraries(
80+
// Return CO2 Emission of the previously booked flight
81+
int weight = order.getFlightOffers()[0].getItineraries(
7582
)[0].getSegments()[0].getCo2Emissions()[0].getWeight();
7683
String unit = order.getFlightOffers()[0].getItineraries(
7784
)[0].getSegments()[0].getCo2Emissions()[0].getWeightUnit();
7885

79-
System.out.println(weight + unit);
8086
}
8187
}

src/main/java/examples/flight/offers/FlightOffersPrice.java renamed to src/examples/flight/offers/FlightOffersPrice.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class FlightOffersPrice {
1010
/**
1111
* <p>
12-
* An example to call hotel Booking API
12+
* An example to call Flight Offers Price API
1313
* <code>/v1/shopping/flight-offers/pricing</code> endpoints.
1414
* </p>
1515
*
@@ -33,13 +33,15 @@ public static void main(String[] args) throws ResponseException {
3333
.and("departureDate", "2020-11-01")
3434
.and("returnDate", "2020-11-08")
3535
.and("adults", 1)
36-
.and("max", 1));
36+
.and("max", 2));
3737

38+
// We price the 2nd flight of the list to confirm the price and the availability
3839
FlightPrice flightPricing = amadeus.shopping.flightOffersSearch.pricing.post(
39-
flightOffersSearches,
40+
flightOffersSearches[1],
4041
Params.with("include", "detailed-fare-rules")
4142
.and("forceClass", "false")
42-
);
43+
);
44+
4345
System.out.println(flightPricing.getResponse());
4446
}
4547
}

src/main/java/com/amadeus/booking/FlightOrders.java

Lines changed: 107 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
import com.amadeus.exceptions.ResponseException;
66
import com.amadeus.resources.FlightOfferSearch;
77
import com.amadeus.resources.FlightOrder;
8+
import com.amadeus.resources.FlightPrice;
89
import com.amadeus.resources.Resource;
910
import com.amadeus.resources.Traveler;
11+
import com.google.gson.Gson;
12+
import com.google.gson.GsonBuilder;
1013
import com.google.gson.JsonArray;
14+
import com.google.gson.JsonElement;
1115
import com.google.gson.JsonObject;
1216

17+
1318
/**
1419
* <p>
1520
* A namespaced client for the
@@ -36,6 +41,41 @@ public FlightOrders(Amadeus client) {
3641
this.client = client;
3742
}
3843

44+
/**
45+
* Build the JSON from the Travelers array.
46+
*
47+
* @param travelers array of Traveler
48+
* @return
49+
*/
50+
private JsonArray buildTravelersJSON(Traveler[] travelers) {
51+
Gson gson = new GsonBuilder().create();
52+
JsonArray travelerArray = new JsonArray();
53+
54+
for (int i = 0; i < travelers.length; i++) {
55+
JsonElement traveler = gson.toJsonTree(travelers[i], Traveler.class);
56+
travelerArray.add(traveler);
57+
}
58+
return travelerArray;
59+
}
60+
61+
/**
62+
* Build the JSON from the Travelers array.
63+
*
64+
* @param flightOffers Array of FlightOfferSearch
65+
* @return JsonArray of the flightOffers
66+
*/
67+
private JsonArray buildFlightOffersJSON(FlightOfferSearch[] flightOffers) {
68+
Gson gson = new GsonBuilder().create();
69+
JsonArray flightOffersArray = new JsonArray();
70+
71+
for (int i = 0; i < flightOffers.length; i++) {
72+
JsonElement flightOffer = gson.toJsonTree(flightOffers[i], FlightOfferSearch.class);
73+
flightOffersArray.add(flightOffer);
74+
}
75+
return flightOffersArray;
76+
}
77+
78+
3979
/**
4080
* <p>
4181
* The Flight Create Orders API allows you to perform flight booking.
@@ -79,58 +119,82 @@ public FlightOrder post(String body) throws ResponseException {
79119
* amadeus.booking.flightOrders.post(object);</pre>
80120
*
81121
* @param flightOffersSearches List of flight-offers as FlightOfferSearch[]
82-
* @param travelers List of travelers as Traveler[]
122+
* @param travelers List of travelers as Traveler[]
83123
* @return an API resource
84124
* @throws ResponseException when an exception occurs
85125
*/
86-
public FlightOrder post(FlightOfferSearch[] flightOffersSearches,
87-
Traveler[] travelers) throws ResponseException {
88-
89-
JsonObject nameObject = new JsonObject();
90-
nameObject.addProperty("firstName", travelers[0].getName().getFirstName());
91-
nameObject.addProperty("lastName", travelers[0].getName().getLastName());
92-
93-
JsonObject phoneObject = new JsonObject();
94-
phoneObject.addProperty("countryCallingCode",
95-
travelers[0].getContact().getPhones()[0].getCountryCallingCode());
96-
phoneObject.addProperty("number", travelers[0].getContact().getPhones()[0].getNumber());
97-
phoneObject.addProperty("deviceType", travelers[0].getContact().getPhones()[0].getDeviceType());
98-
99-
100-
JsonArray phonesArray = new JsonArray();
101-
phonesArray.add(phoneObject);
102-
103-
JsonObject contactObject = new JsonObject();
104-
contactObject.add("phones", phonesArray);
105-
106-
JsonObject documentsOject = new JsonObject();
107-
documentsOject.addProperty("documentType", travelers[0].getDocuments()[0].getDocumentType());
108-
documentsOject.addProperty("number", travelers[0].getDocuments()[0].getNumber());
109-
documentsOject.addProperty("expiryDate", travelers[0].getDocuments()[0].getExpiryDate());
110-
documentsOject.addProperty("nationality", travelers[0].getDocuments()[0].getNationality());
111-
documentsOject.addProperty("issuanceCountry",
112-
travelers[0].getDocuments()[0].getIssuanceCountry());
113-
documentsOject.addProperty("holder", travelers[0].getDocuments()[0].isHolder());
114-
JsonArray documentsArray = new JsonArray();
115-
documentsArray.add(documentsOject);
116-
117-
JsonObject travelerObject = new JsonObject();
118-
travelerObject.addProperty("id", travelers[0].getId());
119-
travelerObject.addProperty("dateOfBirth", travelers[0].getDateOfBirth());
120-
travelerObject.add("name", nameObject);
121-
travelerObject.add("contact", contactObject);
122-
travelerObject.add("documents", documentsArray);
123-
JsonArray travelerArray = new JsonArray();
124-
travelerArray.add(travelerObject);
125-
126+
public FlightOrder post(FlightOfferSearch[] flightOffersSearches,
127+
Traveler[] travelers) throws ResponseException {
128+
129+
JsonObject typeObject = new JsonObject();
130+
typeObject.addProperty("type", "flight-order");
131+
132+
// Prepare the Flight Offers JSON
133+
JsonArray flightOffersArray = buildFlightOffersJSON(flightOffersSearches);
134+
typeObject.add("flightOffers", flightOffersArray);
135+
136+
// Prepare the TravelerJSON
137+
JsonArray travelerArray = buildTravelersJSON(travelers);
138+
typeObject.add("travelers", travelerArray);
139+
140+
JsonObject jsonObject = new JsonObject();
141+
jsonObject.add("data", typeObject);
142+
143+
Response response = client.post("/v1/booking/flight-orders", jsonObject);
144+
return (FlightOrder) Resource.fromObject(response, FlightOrder.class);
145+
}
146+
147+
/**
148+
* <p>
149+
* The Flight Create Orders API allows you to perform flight booking.
150+
* </p>
151+
*
152+
* <pre>
153+
* amadeus.booking.flightOrders.post(flightOfferSearch, traveler);</pre>
154+
*
155+
* @param flightOffersSearch a flight-offer as FlightOfferSearch
156+
* @param travelers List of travelers as Traveler[]
157+
* @return an API resource
158+
* @throws ResponseException when an exception occurs
159+
*/
160+
public FlightOrder post(FlightOfferSearch flightOffersSearch,
161+
Traveler[] travelers) throws ResponseException {
162+
FlightOfferSearch[] flightOffersSearchArray = new FlightOfferSearch[1];
163+
flightOffersSearchArray[0] = flightOffersSearch;
164+
165+
return post(flightOffersSearchArray, travelers);
166+
}
167+
168+
/**
169+
* <p>
170+
* The Flight Create Orders API allows you to perform flight booking.
171+
* </p>
172+
*
173+
* <pre>
174+
* amadeus.booking.flightOrders.post(flightOfferSearch, traveler);</pre>
175+
*
176+
* @param flightPrice a flight-offers-pricing as FlightPrice
177+
* @param travelers List of travelers as Traveler[]
178+
* @return an API resource
179+
* @throws ResponseException when an exception occurs
180+
*/
181+
public FlightOrder post(FlightPrice flightPrice,
182+
Traveler[] travelers) throws ResponseException {
183+
126184
JsonObject typeObject = new JsonObject();
127185
typeObject.addProperty("type", "flight-order");
128-
typeObject.add("flightOffers", flightOffersSearches[0].getResponse().getData());
186+
187+
Gson gson = new GsonBuilder().create();
188+
189+
JsonArray flightOffersArray = buildFlightOffersJSON(flightPrice.getFlightOffers());
190+
typeObject.add("flightOffers", flightOffersArray);
191+
192+
// Build Traveler JSON
193+
JsonArray travelerArray = buildTravelersJSON(travelers);
129194
typeObject.add("travelers", travelerArray);
130195

131196
JsonObject jsonObject = new JsonObject();
132197
jsonObject.add("data", typeObject);
133-
System.out.println(jsonObject);
134198

135199
Response response = client.post("/v1/booking/flight-orders", jsonObject);
136200
return (FlightOrder) Resource.fromObject(response, FlightOrder.class);

src/main/java/com/amadeus/resources/FlightOfferSearch.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.amadeus.resources;
22

33
import com.google.gson.annotations.SerializedName;
4-
import java.util.Date;
54
import lombok.Getter;
65
import lombok.ToString;
76

@@ -19,7 +18,7 @@ protected FlightOfferSearch() {}
1918
private @Getter boolean instantTicketingRequired;
2019
private @Getter boolean nonHomogeneous;
2120
private @Getter boolean oneWay;
22-
private @Getter Date lastTicketingDate;
21+
private @Getter String lastTicketingDate;
2322
private @Getter int numberOfBookableSeats;
2423
private @Getter Itinerary[] itineraries;
2524
private @Getter SearchPrice price;
@@ -61,7 +60,7 @@ public class Co2Emissions {
6160
protected Co2Emissions() {
6261
}
6362

64-
private @Getter String weight;
63+
private @Getter int weight;
6564
private @Getter String weightUnit;
6665
private @Getter String cabin;
6766
}
@@ -108,6 +107,11 @@ protected Fee() {
108107
@ToString
109108
public class PricingOptions {
110109
private @Getter boolean includedCheckedBagsOnly;
110+
private @Getter String[] fareType;
111+
private @Getter String[] corporateCodes;
112+
private @Getter boolean refundableFare;
113+
private @Getter boolean noRestrictionFare;
114+
private @Getter boolean noPenaltyFare;
111115
}
112116

113117
@ToString
@@ -141,7 +145,7 @@ public class IncludedCheckedBags {
141145
protected IncludedCheckedBags() {
142146
}
143147

144-
private @Getter double weight;
148+
private @Getter int weight;
145149
private @Getter String weightUnit;
146150
}
147151
}

src/main/java/com/amadeus/resources/Resource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ protected Resource() {}
1414
/**
1515
* The original response that this object is populated from.
1616
*/
17-
private @Getter Response response;
17+
private transient @Getter Response response;
1818
/**
1919
* The class used for deserialization.
2020
* @hide as only used internally
2121
*/
22-
private @Getter Class deSerializationClass;
22+
private transient @Getter Class deSerializationClass;
2323

2424
/**
2525
* Turns a response into a Gson deserialized array of resources,

0 commit comments

Comments
 (0)