Skip to content

Commit ace0035

Browse files
author
Anthony Roux
committed
ADD Flight Choice Prediction + fix POST
Add the support for Flight Choice Prediction and fix the POST method add support for oraclejdk11 / openjdk10/ openjdk11 revert java versions oraclejdk is not available anymore. Switch to openjdk Disable jdk 9 tests
1 parent 5b89251 commit ace0035

File tree

13 files changed

+280
-67
lines changed

13 files changed

+280
-67
lines changed

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ install: true
33

44
sudo: false
55
jdk:
6-
- oraclejdk8
7-
- oraclejdk9
86
- openjdk8
7+
# JDK 9 disable because of TravisCI problems
8+
# https://travis-ci.community/t/install-jdk-sh-failing-for-openjdk9-and-10/3998/17
9+
# - openjdk9
10+
911

1012
before_install:
1113
- chmod +x gradlew

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,19 @@ FlightOffer[] flightOffers = amadeus.shopping.flightOffers.get(Params
225225
.and("destination", "MAD")
226226
.and("departureDate", "2019-08-01"));
227227

228+
// Flight Choice Prediction
229+
// Note that the example calls 2 APIs: Flight Low-fare Search & Flight Choice Prediction
230+
FlightOffer[] flightOffers = amadeus.shopping.flightOffers
231+
.get(Params.with("origin", "MAD").and("destination", "NYC").and("departureDate", "2020-01-01").and("max", "2"));
232+
233+
// Using a JSonObject
234+
JsonObject result = flightOffers[0].getResponse().getResult();
235+
FlightOffer[] flightOffersPrediction = amadeus.shopping.flightOffers.prediction.post(result);
236+
237+
// Using a String
238+
String body = flightOffers[0].getResponse().getBody();
239+
FlightOffer[] flightOffersPrediction = amadeus.shopping.flightOffers.prediction.post(body);
240+
228241
// Flight Check-in Links
229242
CheckinLink[] checkinLinks = amadeus.referenceData.urls.checkinLinks.get(Params
230243
.with("airlineCode", "BA"));

src/main/java/com/amadeus/Amadeus.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static Configuration builder(@NonNull String clientId, @NonNull String cl
8383
*/
8484
public static Configuration builder(Map<String, String> environment) {
8585
String clientId = environment.get("AMADEUS_CLIENT_ID");
86-
String clientSecret = environment.get("AMADEUS_CLIENT_ID");
86+
String clientSecret = environment.get("AMADEUS_CLIENT_SECRET");
8787

8888
Configuration configuration = Amadeus.builder(clientId, clientSecret);
8989
configuration.parseEnvironment(environment);

src/main/java/com/amadeus/HTTPClient.java

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.amadeus.exceptions.NetworkException;
66
import com.amadeus.exceptions.ResponseException;
77
import com.amadeus.resources.Resource;
8+
import com.google.gson.JsonObject;
89
import java.io.BufferedWriter;
910
import java.io.IOException;
1011
import java.io.OutputStream;
@@ -36,7 +37,7 @@ protected HTTPClient(Configuration configuration) {
3637
* @see Amadeus#get(String, Params)
3738
*/
3839
public Response get(String path) throws ResponseException {
39-
return request(Constants.GET, path, null);
40+
return request(Constants.GET, path, null,null);
4041
}
4142

4243
/**
@@ -63,7 +64,7 @@ public Response get(String path) throws ResponseException {
6364
* @return a Response object containing the status code, body, and parsed data.
6465
*/
6566
public Response get(String path, Params params) throws ResponseException {
66-
return request(Constants.GET, path, params);
67+
return request(Constants.GET, path, params, null);
6768
}
6869

6970
/**
@@ -73,7 +74,7 @@ public Response get(String path, Params params) throws ResponseException {
7374
* @see Amadeus#post(String, Params)
7475
*/
7576
public Response post(String path) throws ResponseException {
76-
return request(Constants.POST, path, null);
77+
return request(Constants.POST, path, null, null);
7778
}
7879

7980
/**
@@ -100,7 +101,57 @@ public Response post(String path) throws ResponseException {
100101
* @return a Response object containing the status code, body, and parsed data.
101102
*/
102103
public Response post(String path, Params params) throws ResponseException {
103-
return request(Constants.POST, path, params);
104+
return request(Constants.POST, path, params, null);
105+
}
106+
107+
/**
108+
* <p>
109+
* A helper module for making generic POST requests calls. It is used by
110+
* every namespaced API POST method.
111+
* </p>
112+
*
113+
* <pre>
114+
* amadeus.foo.bar.post(Params.with("airline", "1X"));
115+
* </pre>
116+
*
117+
* <p>
118+
* It can be used to make any generic API call that is automatically
119+
* authenticated using your API credentials:
120+
* </p>
121+
*
122+
* <pre>
123+
* amadeus.post("/v1/foo/bar", Params.with("airline", "1X"));
124+
* </pre>
125+
*
126+
* @param path The full path for the API call
127+
* @param body The optional POST params to pass to the API
128+
* @return a Response object containing the status code, body, and parsed data.
129+
*/
130+
public Response post(String path, String body) throws ResponseException {
131+
return request(Constants.POST, path, null, body);
132+
}
133+
134+
/**
135+
* <p>
136+
* A helper module for making generic POST requests calls. It is used by
137+
* every namespaced API POST method.
138+
* </p>
139+
*
140+
* <p>
141+
* It can be used to make any generic API call that is automatically
142+
* authenticated using your API credentials:
143+
* </p>
144+
*
145+
* <pre>
146+
* amadeus.post("/v1/foo/bar", { "foo" : "bar" })
147+
* </pre>
148+
*
149+
* @param path The full path for the API call
150+
* @param body The POST JsonObject body to pass to the API
151+
* @return a Response object containing the status code, body, and parsed data.
152+
*/
153+
public Response post(String path, JsonObject body) throws ResponseException {
154+
return request(Constants.POST, path, null, body.toString());
104155
}
105156

106157
/**
@@ -110,9 +161,9 @@ public Response post(String path, Params params) throws ResponseException {
110161
*
111162
* @hides as only used internally
112163
*/
113-
public Response unauthenticatedRequest(String verb, String path, Params params,
114-
String bearerToken) throws ResponseException {
115-
Request request = buildRequest(verb, path, params, bearerToken);
164+
public Response unauthenticatedRequest(String verb, String path, Params params, String body,
165+
String bearerToken) throws ResponseException {
166+
Request request = buildRequest(verb, path, params, body, bearerToken);
116167
log(request);
117168
return execute(request);
118169
}
@@ -198,13 +249,15 @@ public Resource[] last(Resource resource) throws ResponseException {
198249
}
199250

200251
// A generic method for making requests of any verb.
201-
protected Response request(String verb, String path, Params params) throws ResponseException {
202-
return unauthenticatedRequest(verb, path, params, accessToken.getBearerToken());
252+
protected Response request(String verb, String path, Params params, String body)
253+
throws ResponseException {
254+
return unauthenticatedRequest(verb, path, params, body, accessToken.getBearerToken());
203255
}
204256

205257
// Builds a request
206-
protected Request buildRequest(String verb, String path, Params params, String bearerToken) {
207-
return new Request(verb, path, params, bearerToken, this);
258+
protected Request buildRequest(String verb, String path, Params params, String body,
259+
String bearerToken) {
260+
return new Request(verb, path, params, body, bearerToken, this);
208261
}
209262

210263
// A simple log that only triggers if we are in debug mode
@@ -238,6 +291,8 @@ private Request fetch(Request request) throws NetworkException {
238291

239292
// Writes the parameters to the request.
240293
private void write(Request request) throws IOException {
294+
295+
241296
if (request.getVerb() == Constants.POST && request.getParams() != null) {
242297
OutputStream os = request.getConnection().getOutputStream();
243298
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
@@ -246,6 +301,16 @@ private void write(Request request) throws IOException {
246301
writer.close();
247302
os.close();
248303
}
304+
if (request.getVerb() == Constants.POST && request.getParams() == null) {
305+
OutputStream os = request.getConnection().getOutputStream();
306+
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
307+
if (request.getBody() != null) {
308+
writer.write(request.getBody());
309+
}
310+
writer.flush();
311+
writer.close();
312+
os.close();
313+
}
249314
}
250315

251316
/**
@@ -263,7 +328,7 @@ protected Response page(String pageName, Response response) throws ResponseExcep
263328
Params params = (Params) request.getParams().clone();
264329
params.put("page[offset]", pageNumber);
265330

266-
return request(request.getVerb(), request.getPath(), params);
331+
return request(request.getVerb(), request.getPath(), params, "emptyBody");
267332
} catch (NullPointerException e) {
268333
return null;
269334
}
@@ -277,4 +342,4 @@ protected Resource[] page(String pageName, Resource resource) throws ResponseExc
277342
Response response = page(pageName, resource.getResponse());
278343
return Resource.fromArray(response, resource.getDeSerializationClass());
279344
}
280-
}
345+
}

src/main/java/com/amadeus/Request.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.amadeus.Constants;
44

5+
import com.google.gson.JsonElement;
56
import java.io.IOException;
67
import java.net.HttpURLConnection;
78
import java.net.URL;
@@ -40,6 +41,10 @@ public class Request {
4041
* The params to send to the API endpoint.
4142
*/
4243
private @Getter Params params;
44+
/**
45+
* The body to send to the API endpoint.
46+
*/
47+
private @Getter String body;
4348
/**
4449
* The bearer token used to authenticate the API call.
4550
*/
@@ -80,14 +85,15 @@ public class Request {
8085
// The connection used to make the API call.
8186
private @Getter HttpURLConnection connection;
8287

83-
protected Request(String verb, String path, Params params, String bearerToken,
88+
protected Request(String verb, String path, Params params, String body, String bearerToken,
8489
HTTPClient client) {
8590
Configuration config = client.getConfiguration();
8691

8792
this.verb = verb;
8893
this.host = config.getHost();
8994
this.path = path;
9095
this.params = params;
96+
this.body = body;
9197
this.bearerToken = bearerToken;
9298
this.languageVersion = System.getProperty("java.version");
9399
this.clientVersion = Amadeus.VERSION;
@@ -130,6 +136,7 @@ private void prepareHeaders() {
130136
this.headers = new HashMap<String, String>();
131137
headers.put(Constants.USER_AGENT, buildUserAgent());
132138
headers.put(Constants.ACCEPT, "application/json, application/vnd.amadeus+json");
139+
133140
if (bearerToken != null) {
134141
headers.put(Constants.AUTHORIZATION, bearerToken);
135142
}

src/main/java/com/amadeus/client/AccessToken.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ private Response fetchAccessToken() throws ResponseException {
7070
Params.with(Constants.GRANT_TYPE, Constants.CLIENT_CREDENTIALS)
7171
.and(Constants.CLIENT_ID, config.getClientId())
7272
.and(Constants.CLIENT_SECRET, config.getClientSecret()),
73-
null
73+
null,
74+
null
7475
);
7576
}
7677

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ protected FlightOffer() {}
1515
private @Getter String type;
1616
private @Getter String id;
1717
private @Getter OfferItem[] offerItems;
18+
private @Getter String choiceProbability;
1819

1920
/**
2021
* An FlightOffer-related object as returned by the FlightOffers API.

src/main/java/com/amadeus/shopping/FlightOffers.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.amadeus.exceptions.ResponseException;
77
import com.amadeus.resources.FlightOffer;
88
import com.amadeus.resources.Resource;
9+
import com.amadeus.shopping.flightOffers.Prediction;
910
import com.google.gson.Gson;
1011

1112
/**
@@ -24,13 +25,15 @@
2425
*/
2526
public class FlightOffers {
2627
private Amadeus client;
28+
public Prediction prediction;
2729

2830
/**
2931
* Constructor.
3032
* @hide
3133
*/
3234
public FlightOffers(Amadeus client) {
3335
this.client = client;
36+
this.prediction = new Prediction(client);
3437
}
3538

3639
/**
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.amadeus.shopping.flightOffers;
2+
3+
import com.amadeus.Amadeus;
4+
import com.amadeus.Response;
5+
import com.amadeus.exceptions.ResponseException;
6+
import com.amadeus.resources.FlightOffer;
7+
import com.amadeus.resources.Resource;
8+
9+
import com.google.gson.JsonObject;
10+
11+
12+
13+
14+
15+
/**
16+
* <p>
17+
* A namespaced client for the
18+
* <code>/v1/shopping/flight-offers/prediction</code> endpoints.
19+
* </p>
20+
*
21+
* <p>
22+
* Access via the Amadeus client object.
23+
* </p>
24+
*
25+
* <pre>
26+
* Amadeus amadeus = Amadeus.builder(API_KEY, API_SECRET).build();
27+
* amadeus.shopping.flightOffers.prediction;</pre>
28+
*/
29+
public class Prediction {
30+
31+
private Amadeus client;
32+
33+
/**
34+
* Constructor.
35+
*
36+
* @hide
37+
*/
38+
public Prediction(Amadeus client) {
39+
this.client = client;
40+
}
41+
42+
/**
43+
* <p>
44+
* This machine learning API is based on a prediction model that takes the response of a flight
45+
* search as input (Flight Low-fare Search) and predict, for each itinerary, the probably for a
46+
* travel to select it.
47+
* </p>
48+
*
49+
* <pre>
50+
* amadeus.shopping.flightOffers.prediction.post(body);</pre>
51+
*
52+
* @param body the parameters to send to the API as a JSonObject
53+
* @return an API resource
54+
* @throws ResponseException when an exception occurs
55+
*/
56+
public FlightOffer[] post(JsonObject body) throws ResponseException {
57+
Response response = client.post("/v1/shopping/flight-offers/prediction", body);
58+
return (FlightOffer[]) Resource.fromArray(response, FlightOffer[].class);
59+
}
60+
61+
/**
62+
* <p>
63+
* This machine learning API is based on a prediction model that takes the response of a flight
64+
* search as input (Flight Low-fare Search) and predict, for each itinerary, the probably for a
65+
* travel to select it.
66+
* </p>
67+
*
68+
* <pre>
69+
* amadeus.shopping.flightOffers.prediction.post(body);</pre>
70+
*
71+
* @param body the parameters to send to the API as a String
72+
* @return an API resource
73+
* @throws ResponseException when an exception occurs
74+
*/
75+
public FlightOffer[] post(String body) throws ResponseException {
76+
Response response = client.post("/v1/shopping/flight-offers/prediction", body);
77+
return (FlightOffer[]) Resource.fromArray(response, FlightOffer[].class);
78+
}
79+
80+
/**
81+
* Convenience method for calling <code>post</code> without any parameters.
82+
*
83+
* @see Prediction#post()
84+
*/
85+
public FlightOffer[] post() throws ResponseException {
86+
return post((String) null);
87+
}
88+
}

0 commit comments

Comments
 (0)