Skip to content

Commit

Permalink
ADD Flight Choice Prediction + fix POST
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Anthony Roux committed Jul 3, 2019
1 parent 5b89251 commit ace0035
Show file tree
Hide file tree
Showing 13 changed files with 280 additions and 67 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ install: true

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


before_install:
- chmod +x gradlew
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,19 @@ FlightOffer[] flightOffers = amadeus.shopping.flightOffers.get(Params
.and("destination", "MAD")
.and("departureDate", "2019-08-01"));

// Flight Choice Prediction
// Note that the example calls 2 APIs: Flight Low-fare Search & Flight Choice Prediction
FlightOffer[] flightOffers = amadeus.shopping.flightOffers
.get(Params.with("origin", "MAD").and("destination", "NYC").and("departureDate", "2020-01-01").and("max", "2"));

// Using a JSonObject
JsonObject result = flightOffers[0].getResponse().getResult();
FlightOffer[] flightOffersPrediction = amadeus.shopping.flightOffers.prediction.post(result);

// Using a String
String body = flightOffers[0].getResponse().getBody();
FlightOffer[] flightOffersPrediction = amadeus.shopping.flightOffers.prediction.post(body);

// Flight Check-in Links
CheckinLink[] checkinLinks = amadeus.referenceData.urls.checkinLinks.get(Params
.with("airlineCode", "BA"));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/amadeus/Amadeus.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static Configuration builder(@NonNull String clientId, @NonNull String cl
*/
public static Configuration builder(Map<String, String> environment) {
String clientId = environment.get("AMADEUS_CLIENT_ID");
String clientSecret = environment.get("AMADEUS_CLIENT_ID");
String clientSecret = environment.get("AMADEUS_CLIENT_SECRET");

Configuration configuration = Amadeus.builder(clientId, clientSecret);
configuration.parseEnvironment(environment);
Expand Down
91 changes: 78 additions & 13 deletions src/main/java/com/amadeus/HTTPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.amadeus.exceptions.NetworkException;
import com.amadeus.exceptions.ResponseException;
import com.amadeus.resources.Resource;
import com.google.gson.JsonObject;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -36,7 +37,7 @@ protected HTTPClient(Configuration configuration) {
* @see Amadeus#get(String, Params)
*/
public Response get(String path) throws ResponseException {
return request(Constants.GET, path, null);
return request(Constants.GET, path, null,null);
}

/**
Expand All @@ -63,7 +64,7 @@ public Response get(String path) throws ResponseException {
* @return a Response object containing the status code, body, and parsed data.
*/
public Response get(String path, Params params) throws ResponseException {
return request(Constants.GET, path, params);
return request(Constants.GET, path, params, null);
}

/**
Expand All @@ -73,7 +74,7 @@ public Response get(String path, Params params) throws ResponseException {
* @see Amadeus#post(String, Params)
*/
public Response post(String path) throws ResponseException {
return request(Constants.POST, path, null);
return request(Constants.POST, path, null, null);
}

/**
Expand All @@ -100,7 +101,57 @@ public Response post(String path) throws ResponseException {
* @return a Response object containing the status code, body, and parsed data.
*/
public Response post(String path, Params params) throws ResponseException {
return request(Constants.POST, path, params);
return request(Constants.POST, path, params, null);
}

/**
* <p>
* A helper module for making generic POST requests calls. It is used by
* every namespaced API POST method.
* </p>
*
* <pre>
* amadeus.foo.bar.post(Params.with("airline", "1X"));
* </pre>
*
* <p>
* It can be used to make any generic API call that is automatically
* authenticated using your API credentials:
* </p>
*
* <pre>
* amadeus.post("/v1/foo/bar", Params.with("airline", "1X"));
* </pre>
*
* @param path The full path for the API call
* @param body The optional POST params to pass to the API
* @return a Response object containing the status code, body, and parsed data.
*/
public Response post(String path, String body) throws ResponseException {
return request(Constants.POST, path, null, body);
}

/**
* <p>
* A helper module for making generic POST requests calls. It is used by
* every namespaced API POST method.
* </p>
*
* <p>
* It can be used to make any generic API call that is automatically
* authenticated using your API credentials:
* </p>
*
* <pre>
* amadeus.post("/v1/foo/bar", { "foo" : "bar" })
* </pre>
*
* @param path The full path for the API call
* @param body The POST JsonObject body to pass to the API
* @return a Response object containing the status code, body, and parsed data.
*/
public Response post(String path, JsonObject body) throws ResponseException {
return request(Constants.POST, path, null, body.toString());
}

/**
Expand All @@ -110,9 +161,9 @@ public Response post(String path, Params params) throws ResponseException {
*
* @hides as only used internally
*/
public Response unauthenticatedRequest(String verb, String path, Params params,
String bearerToken) throws ResponseException {
Request request = buildRequest(verb, path, params, bearerToken);
public Response unauthenticatedRequest(String verb, String path, Params params, String body,
String bearerToken) throws ResponseException {
Request request = buildRequest(verb, path, params, body, bearerToken);
log(request);
return execute(request);
}
Expand Down Expand Up @@ -198,13 +249,15 @@ public Resource[] last(Resource resource) throws ResponseException {
}

// A generic method for making requests of any verb.
protected Response request(String verb, String path, Params params) throws ResponseException {
return unauthenticatedRequest(verb, path, params, accessToken.getBearerToken());
protected Response request(String verb, String path, Params params, String body)
throws ResponseException {
return unauthenticatedRequest(verb, path, params, body, accessToken.getBearerToken());
}

// Builds a request
protected Request buildRequest(String verb, String path, Params params, String bearerToken) {
return new Request(verb, path, params, bearerToken, this);
protected Request buildRequest(String verb, String path, Params params, String body,
String bearerToken) {
return new Request(verb, path, params, body, bearerToken, this);
}

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

// Writes the parameters to the request.
private void write(Request request) throws IOException {


if (request.getVerb() == Constants.POST && request.getParams() != null) {
OutputStream os = request.getConnection().getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
Expand All @@ -246,6 +301,16 @@ private void write(Request request) throws IOException {
writer.close();
os.close();
}
if (request.getVerb() == Constants.POST && request.getParams() == null) {
OutputStream os = request.getConnection().getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
if (request.getBody() != null) {
writer.write(request.getBody());
}
writer.flush();
writer.close();
os.close();
}
}

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

return request(request.getVerb(), request.getPath(), params);
return request(request.getVerb(), request.getPath(), params, "emptyBody");
} catch (NullPointerException e) {
return null;
}
Expand All @@ -277,4 +342,4 @@ protected Resource[] page(String pageName, Resource resource) throws ResponseExc
Response response = page(pageName, resource.getResponse());
return Resource.fromArray(response, resource.getDeSerializationClass());
}
}
}
9 changes: 8 additions & 1 deletion src/main/java/com/amadeus/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.amadeus.Constants;

import com.google.gson.JsonElement;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
Expand Down Expand Up @@ -40,6 +41,10 @@ public class Request {
* The params to send to the API endpoint.
*/
private @Getter Params params;
/**
* The body to send to the API endpoint.
*/
private @Getter String body;
/**
* The bearer token used to authenticate the API call.
*/
Expand Down Expand Up @@ -80,14 +85,15 @@ public class Request {
// The connection used to make the API call.
private @Getter HttpURLConnection connection;

protected Request(String verb, String path, Params params, String bearerToken,
protected Request(String verb, String path, Params params, String body, String bearerToken,
HTTPClient client) {
Configuration config = client.getConfiguration();

this.verb = verb;
this.host = config.getHost();
this.path = path;
this.params = params;
this.body = body;
this.bearerToken = bearerToken;
this.languageVersion = System.getProperty("java.version");
this.clientVersion = Amadeus.VERSION;
Expand Down Expand Up @@ -130,6 +136,7 @@ private void prepareHeaders() {
this.headers = new HashMap<String, String>();
headers.put(Constants.USER_AGENT, buildUserAgent());
headers.put(Constants.ACCEPT, "application/json, application/vnd.amadeus+json");

if (bearerToken != null) {
headers.put(Constants.AUTHORIZATION, bearerToken);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/amadeus/client/AccessToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ private Response fetchAccessToken() throws ResponseException {
Params.with(Constants.GRANT_TYPE, Constants.CLIENT_CREDENTIALS)
.and(Constants.CLIENT_ID, config.getClientId())
.and(Constants.CLIENT_SECRET, config.getClientSecret()),
null
null,
null
);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/amadeus/resources/FlightOffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ protected FlightOffer() {}
private @Getter String type;
private @Getter String id;
private @Getter OfferItem[] offerItems;
private @Getter String choiceProbability;

/**
* An FlightOffer-related object as returned by the FlightOffers API.
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/amadeus/shopping/FlightOffers.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.amadeus.exceptions.ResponseException;
import com.amadeus.resources.FlightOffer;
import com.amadeus.resources.Resource;
import com.amadeus.shopping.flightOffers.Prediction;
import com.google.gson.Gson;

/**
Expand All @@ -24,13 +25,15 @@
*/
public class FlightOffers {
private Amadeus client;
public Prediction prediction;

/**
* Constructor.
* @hide
*/
public FlightOffers(Amadeus client) {
this.client = client;
this.prediction = new Prediction(client);
}

/**
Expand Down
88 changes: 88 additions & 0 deletions src/main/java/com/amadeus/shopping/flightOffers/Prediction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.amadeus.shopping.flightOffers;

import com.amadeus.Amadeus;
import com.amadeus.Response;
import com.amadeus.exceptions.ResponseException;
import com.amadeus.resources.FlightOffer;
import com.amadeus.resources.Resource;

import com.google.gson.JsonObject;





/**
* <p>
* A namespaced client for the
* <code>/v1/shopping/flight-offers/prediction</code> endpoints.
* </p>
*
* <p>
* Access via the Amadeus client object.
* </p>
*
* <pre>
* Amadeus amadeus = Amadeus.builder(API_KEY, API_SECRET).build();
* amadeus.shopping.flightOffers.prediction;</pre>
*/
public class Prediction {

private Amadeus client;

/**
* Constructor.
*
* @hide
*/
public Prediction(Amadeus client) {
this.client = client;
}

/**
* <p>
* This machine learning API is based on a prediction model that takes the response of a flight
* search as input (Flight Low-fare Search) and predict, for each itinerary, the probably for a
* travel to select it.
* </p>
*
* <pre>
* amadeus.shopping.flightOffers.prediction.post(body);</pre>
*
* @param body the parameters to send to the API as a JSonObject
* @return an API resource
* @throws ResponseException when an exception occurs
*/
public FlightOffer[] post(JsonObject body) throws ResponseException {
Response response = client.post("/v1/shopping/flight-offers/prediction", body);
return (FlightOffer[]) Resource.fromArray(response, FlightOffer[].class);
}

/**
* <p>
* This machine learning API is based on a prediction model that takes the response of a flight
* search as input (Flight Low-fare Search) and predict, for each itinerary, the probably for a
* travel to select it.
* </p>
*
* <pre>
* amadeus.shopping.flightOffers.prediction.post(body);</pre>
*
* @param body the parameters to send to the API as a String
* @return an API resource
* @throws ResponseException when an exception occurs
*/
public FlightOffer[] post(String body) throws ResponseException {
Response response = client.post("/v1/shopping/flight-offers/prediction", body);
return (FlightOffer[]) Resource.fromArray(response, FlightOffer[].class);
}

/**
* Convenience method for calling <code>post</code> without any parameters.
*
* @see Prediction#post()
*/
public FlightOffer[] post() throws ResponseException {
return post((String) null);
}
}
Loading

0 comments on commit ace0035

Please sign in to comment.