com.amadeus.booking.FlightOrders Maven / Gradle / Ivy
package com.amadeus.booking;
import com.amadeus.Amadeus;
import com.amadeus.Response;
import com.amadeus.exceptions.ResponseException;
import com.amadeus.resources.FlightOfferSearch;
import com.amadeus.resources.FlightOrder;
import com.amadeus.resources.FlightOrder.Traveler;
import com.amadeus.resources.FlightPrice;
import com.amadeus.resources.Resource;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
/**
*
* A namespaced client for the
* /v1/booking/flight-orders
endpoints.
*
*
*
* Access via the Amadeus client object.
*
*
*
* Amadeus amadeus = Amadeus.builder(API_KEY, API_SECRET).build();
* amadeus.booking.flightOrders;
*/
public class FlightOrders {
private Amadeus client;
private static final String FLIGHT_ORDERS_URL = "/v1/booking/flight-orders";
/**
* Constructor.
*
* @hide
*/
public FlightOrders(Amadeus client) {
this.client = client;
}
/**
* Build the JSON from the Travelers array.
*
* @param travelers array of Traveler
* @return
*/
private JsonArray buildTravelersJSON(Traveler[] travelers) {
Gson gson = new GsonBuilder().create();
JsonArray travelerArray = new JsonArray();
for (int i = 0; i < travelers.length; i++) {
JsonElement traveler = gson.toJsonTree(travelers[i], Traveler.class);
travelerArray.add(traveler);
}
return travelerArray;
}
/**
* Build the JSON from the Travelers array.
*
* @param flightOffers Array of FlightOfferSearch
* @return JsonArray of the flightOffers
*/
private JsonArray buildFlightOffersJSON(FlightOfferSearch[] flightOffers) {
Gson gson = new GsonBuilder().create();
JsonArray flightOffersArray = new JsonArray();
for (int i = 0; i < flightOffers.length; i++) {
JsonElement flightOffer = gson.toJsonTree(flightOffers[i], FlightOfferSearch.class);
flightOffersArray.add(flightOffer);
}
return flightOffersArray;
}
/**
*
* The Flight Create Orders API allows you to perform flight booking.
*
*
*
* amadeus.booking.flightOrders.post(body);
*
* @param body the parameters to send to the API as a JSonObject
* @return an API resource
* @throws ResponseException when an exception occurs
*/
public FlightOrder post(JsonObject body) throws ResponseException {
Response response = client.post(FLIGHT_ORDERS_URL, body);
return (FlightOrder) Resource.fromObject(response, FlightOrder.class);
}
/**
*
* The Flight Create Orders API allows you to perform flight booking.
*
*
*
* amadeus.booking.flightOrders.post(body);
*
* @param body the parameters to send to the API as a String
* @return an API resource
* @throws ResponseException when an exception occurs
*/
public FlightOrder post(String body) throws ResponseException {
Response response = client.post(FLIGHT_ORDERS_URL, body);
return (FlightOrder) Resource.fromObject(response, FlightOrder.class);
}
/**
*
* The Flight Create Orders API allows you to perform flight booking.
*
*
*
* amadeus.booking.flightOrders.post(object);
*
* @param flightOffersSearches List of flight-offers as FlightOfferSearch[]
* @param travelers List of travelers as Traveler[]
* @return an API resource
* @throws ResponseException when an exception occurs
*/
public FlightOrder post(FlightOfferSearch[] flightOffersSearches,
Traveler[] travelers) throws ResponseException {
JsonObject typeObject = new JsonObject();
typeObject.addProperty("type", "flight-order");
// Prepare the Flight Offers JSON
JsonArray flightOffersArray = buildFlightOffersJSON(flightOffersSearches);
typeObject.add("flightOffers", flightOffersArray);
// Prepare the TravelerJSON
JsonArray travelerArray = buildTravelersJSON(travelers);
typeObject.add("travelers", travelerArray);
JsonObject jsonObject = new JsonObject();
jsonObject.add("data", typeObject);
Response response = client.post(FLIGHT_ORDERS_URL, jsonObject);
return (FlightOrder) Resource.fromObject(response, FlightOrder.class);
}
/**
*
* The Flight Create Orders API allows you to perform flight booking.
*
*
*
* amadeus.booking.flightOrders.post(flightOfferSearch, traveler);
*
* @param flightOffersSearch a flight-offer as FlightOfferSearch
* @param travelers List of travelers as Traveler[]
* @return an API resource
* @throws ResponseException when an exception occurs
*/
public FlightOrder post(FlightOfferSearch flightOffersSearch,
Traveler[] travelers) throws ResponseException {
FlightOfferSearch[] flightOffersSearchArray = new FlightOfferSearch[1];
flightOffersSearchArray[0] = flightOffersSearch;
return post(flightOffersSearchArray, travelers);
}
/**
*
* The Flight Create Orders API allows you to perform flight booking.
*
*
*
* amadeus.booking.flightOrders.post(flightOfferSearch, traveler);
*
* @param flightPrice a flight-offers-pricing as FlightPrice
* @param travelers List of travelers as Traveler[]
* @return an API resource
* @throws ResponseException when an exception occurs
*/
public FlightOrder post(FlightPrice flightPrice,
Traveler[] travelers) throws ResponseException {
JsonObject typeObject = new JsonObject();
typeObject.addProperty("type", "flight-order");
JsonArray flightOffersArray = buildFlightOffersJSON(flightPrice.getFlightOffers());
typeObject.add("flightOffers", flightOffersArray);
// Build Traveler JSON
JsonArray travelerArray = buildTravelersJSON(travelers);
typeObject.add("travelers", travelerArray);
JsonObject jsonObject = new JsonObject();
jsonObject.add("data", typeObject);
Response response = client.post(FLIGHT_ORDERS_URL, jsonObject);
return (FlightOrder) Resource.fromObject(response, FlightOrder.class);
}
/**
* Convenience method for calling post
without any parameters.
*
* @see FlightOrders#post()
*/
public FlightOrder post() throws ResponseException {
return post((String) null);
}
}