|
| 1 | +package com.amadeus.booking; |
| 2 | + |
| 3 | +import com.amadeus.Amadeus; |
| 4 | +import com.amadeus.Response; |
| 5 | +import com.amadeus.exceptions.ResponseException; |
| 6 | +import com.amadeus.resources.FlightOfferSearch; |
| 7 | +import com.amadeus.resources.FlightOrder; |
| 8 | +import com.amadeus.resources.Resource; |
| 9 | +import com.amadeus.resources.Traveler; |
| 10 | +import com.google.gson.JsonArray; |
| 11 | +import com.google.gson.JsonObject; |
| 12 | + |
| 13 | +/** |
| 14 | + * <p> |
| 15 | + * A namespaced client for the |
| 16 | + * <code>/v1/booking/flight-orders</code> endpoints. |
| 17 | + * </p> |
| 18 | + * |
| 19 | + * <p> |
| 20 | + * Access via the Amadeus client object. |
| 21 | + * </p> |
| 22 | + * |
| 23 | + * <pre> |
| 24 | + * Amadeus amadeus = Amadeus.builder(API_KEY, API_SECRET).build(); |
| 25 | + * amadeus.booking.flightOrders;</pre> |
| 26 | + */ |
| 27 | +public class FlightOrders { |
| 28 | + private Amadeus client; |
| 29 | + |
| 30 | + /** |
| 31 | + * Constructor. |
| 32 | + * |
| 33 | + * @hide |
| 34 | + */ |
| 35 | + public FlightOrders(Amadeus client) { |
| 36 | + this.client = client; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * <p> |
| 41 | + * The Flight Create Orders API allows you to perform flight booking. |
| 42 | + * </p> |
| 43 | + * |
| 44 | + * <pre> |
| 45 | + * amadeus.booking.flightOrders.post(body);</pre> |
| 46 | + * |
| 47 | + * @param body the parameters to send to the API as a JSonObject |
| 48 | + * @return an API resource |
| 49 | + * @throws ResponseException when an exception occurs |
| 50 | + */ |
| 51 | + public FlightOrder post(JsonObject body) throws ResponseException { |
| 52 | + Response response = client.post("/v1/booking/flight-orders", body); |
| 53 | + return (FlightOrder) Resource.fromObject(response, FlightOrder.class); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * <p> |
| 58 | + * The Flight Create Orders API allows you to perform flight booking. |
| 59 | + * </p> |
| 60 | + * |
| 61 | + * <pre> |
| 62 | + * amadeus.booking.flightOrders.post(body);</pre> |
| 63 | + * |
| 64 | + * @param body the parameters to send to the API as a String |
| 65 | + * @return an API resource |
| 66 | + * @throws ResponseException when an exception occurs |
| 67 | + */ |
| 68 | + public FlightOrder post(String body) throws ResponseException { |
| 69 | + Response response = client.post("/v1/booking/flight-orders", body); |
| 70 | + return (FlightOrder) Resource.fromObject(response, FlightOrder.class); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * <p> |
| 75 | + * The Flight Create Orders API allows you to perform flight booking. |
| 76 | + * </p> |
| 77 | + * |
| 78 | + * <pre> |
| 79 | + * amadeus.booking.flightOrders.post(object);</pre> |
| 80 | + * |
| 81 | + * @param flightOffersSearches List of flight-offers as FlightOfferSearch[] |
| 82 | + * @param travelers List of travelers as Traveler[] |
| 83 | + * @return an API resource |
| 84 | + * @throws ResponseException when an exception occurs |
| 85 | + */ |
| 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 | + JsonObject typeObject = new JsonObject(); |
| 127 | + typeObject.addProperty("type", "flight-order"); |
| 128 | + typeObject.add("flightOffers", flightOffersSearches[0].getResponse().getData()); |
| 129 | + typeObject.add("travelers", travelerArray); |
| 130 | + |
| 131 | + JsonObject jsonObject = new JsonObject(); |
| 132 | + jsonObject.add("data", typeObject); |
| 133 | + System.out.println(jsonObject); |
| 134 | + |
| 135 | + Response response = client.post("/v1/booking/flight-orders", jsonObject); |
| 136 | + return (FlightOrder) Resource.fromObject(response, FlightOrder.class); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Convenience method for calling <code>post</code> without any parameters. |
| 141 | + * |
| 142 | + * @see FlightOrders#post() |
| 143 | + */ |
| 144 | + public FlightOrder post() throws ResponseException { |
| 145 | + return post((String) null); |
| 146 | + } |
| 147 | +} |
0 commit comments