Skip to content

Commit 256c396

Browse files
committed
Merge branch 'master' into fix-instance-initialization
2 parents 3ce4b5c + b8888ea commit 256c396

File tree

8 files changed

+100
-88
lines changed

8 files changed

+100
-88
lines changed

src/main/java/com/amadeus/Constants.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212
* </pre>
1313
*/
1414
public final class Constants {
15-
// HTTP verbs
16-
public static final String GET = "GET";
17-
public static final String POST = "POST";
18-
public static final String PUT = "PUT";
19-
public static final String DELETE = "DELETE";
2015

2116

2217
public static final String HTTPS = "https";
@@ -60,4 +55,4 @@ public final class Constants {
6055
private Constants() {
6156
throw new AssertionError();
6257
}
63-
}
58+
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected HTTPClient(Configuration configuration) {
3737
* @see Amadeus#get(String, Params)
3838
*/
3939
public Response get(String path) throws ResponseException {
40-
return request(Constants.GET, path, null,null);
40+
return request(HttpVerbs.GET, path, null,null);
4141
}
4242

4343
/**
@@ -64,7 +64,7 @@ public Response get(String path) throws ResponseException {
6464
* @return a Response object containing the status code, body, and parsed data.
6565
*/
6666
public Response get(String path, Params params) throws ResponseException {
67-
return request(Constants.GET, path, params, null);
67+
return request(HttpVerbs.GET, path, params, null);
6868
}
6969

7070
/**
@@ -91,7 +91,7 @@ public Response get(String path, Params params) throws ResponseException {
9191
* @return a Response object containing the status code, body, and parsed data.
9292
*/
9393
public Response delete(String path, Params params) throws ResponseException {
94-
return request(Constants.DELETE, path, params, null);
94+
return request(HttpVerbs.DELETE, path, params, null);
9595
}
9696

9797
/**
@@ -101,7 +101,7 @@ public Response delete(String path, Params params) throws ResponseException {
101101
* @see Amadeus#delete(String, Params)
102102
*/
103103
public Response delete(String path) throws ResponseException {
104-
return request(Constants.DELETE, path, null,null);
104+
return request(HttpVerbs.DELETE, path, null,null);
105105
}
106106

107107
/**
@@ -111,7 +111,7 @@ public Response delete(String path) throws ResponseException {
111111
* @see Amadeus#post(String, Params)
112112
*/
113113
public Response post(String path) throws ResponseException {
114-
return request(Constants.POST, path, null, null);
114+
return request(HttpVerbs.POST, path, null, null);
115115
}
116116

117117
/**
@@ -138,7 +138,7 @@ public Response post(String path) throws ResponseException {
138138
* @return a Response object containing the status code, body, and parsed data.
139139
*/
140140
public Response post(String path, Params params) throws ResponseException {
141-
return request(Constants.POST, path, params, null);
141+
return request(HttpVerbs.POST, path, params, null);
142142
}
143143

144144
/**
@@ -165,7 +165,7 @@ public Response post(String path, Params params) throws ResponseException {
165165
* @return a Response object containing the status code, body, and parsed data.
166166
*/
167167
public Response post(String path, String body) throws ResponseException {
168-
return request(Constants.POST, path, null, body);
168+
return request(HttpVerbs.POST, path, null, body);
169169
}
170170

171171
/**
@@ -188,7 +188,7 @@ public Response post(String path, String body) throws ResponseException {
188188
* @return a Response object containing the status code, body, and parsed data.
189189
*/
190190
public Response post(String path, JsonObject body) throws ResponseException {
191-
return request(Constants.POST, path, null, body.toString());
191+
return request(HttpVerbs.POST, path, null, body.toString());
192192
}
193193

194194
/**
@@ -212,7 +212,7 @@ public Response post(String path, JsonObject body) throws ResponseException {
212212
* @return a Response object containing the status code, body, and parsed data.
213213
*/
214214
public Response post(String path, Params params, JsonObject body) throws ResponseException {
215-
return request(Constants.POST, path, params, body.toString());
215+
return request(HttpVerbs.POST, path, params, body.toString());
216216
}
217217

218218
/**
@@ -236,7 +236,7 @@ public Response post(String path, Params params, JsonObject body) throws Respons
236236
* @return a Response object containing the status code, body, and parsed data.
237237
*/
238238
public Response post(String path, Params params, String body) throws ResponseException {
239-
return request(Constants.POST, path, params, body);
239+
return request(HttpVerbs.POST, path, params, body);
240240
}
241241

242242
/**
@@ -246,7 +246,7 @@ public Response post(String path, Params params, String body) throws ResponseExc
246246
*
247247
* @hides as only used internally
248248
*/
249-
public Response unauthenticatedRequest(String verb, String path, Params params, String body,
249+
public Response unauthenticatedRequest(HttpVerbs verb, String path, Params params, String body,
250250
String bearerToken) throws ResponseException {
251251
Request request = buildRequest(verb, path, params, body, bearerToken);
252252
log(request);
@@ -334,13 +334,13 @@ public Resource[] last(Resource resource) throws ResponseException {
334334
}
335335

336336
// A generic method for making requests of any verb.
337-
protected Response request(String verb, String path, Params params, String body)
337+
protected Response request(HttpVerbs verb, String path, Params params, String body)
338338
throws ResponseException {
339339
return unauthenticatedRequest(verb, path, params, body, accessToken.getBearerToken());
340340
}
341341

342342
// Builds a request
343-
protected Request buildRequest(String verb, String path, Params params, String body,
343+
protected Request buildRequest(HttpVerbs verb, String path, Params params, String body,
344344
String bearerToken) {
345345
return new Request(verb, path, params, body, bearerToken, this);
346346
}
@@ -378,7 +378,7 @@ private Request fetch(Request request) throws NetworkException {
378378
private void write(Request request) throws IOException {
379379

380380
// POST with access token + body + URL parameters
381-
if (request.getVerb() == Constants.POST && request.getParams() != null
381+
if (request.getVerb() == HttpVerbs.POST && request.getParams() != null
382382
&& request.getBearerToken() != null) {
383383
OutputStream os = request.getConnection().getOutputStream();
384384
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
@@ -392,7 +392,7 @@ private void write(Request request) throws IOException {
392392
}
393393

394394
// POST with access without token (authentication call)
395-
if (request.getVerb() == Constants.POST && request.getParams() != null
395+
if (request.getVerb() == HttpVerbs.POST && request.getParams() != null
396396
&& request.getBearerToken() == null) {
397397
OutputStream os = request.getConnection().getOutputStream();
398398
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
@@ -403,7 +403,7 @@ private void write(Request request) throws IOException {
403403
}
404404

405405
// POST with access token + body
406-
if (request.getVerb() == Constants.POST && request.getParams() == null) {
406+
if (request.getVerb() == HttpVerbs.POST && request.getParams() == null) {
407407
OutputStream os = request.getConnection().getOutputStream();
408408
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
409409
if (request.getBody() != null) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.amadeus;
2+
3+
public enum HttpVerbs {
4+
// HTTP verbs
5+
GET,
6+
POST,
7+
PUT,
8+
DELETE
9+
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class Request {
2222
/**
2323
* The HTTPClient verb to use for API calls.
2424
*/
25-
private @Getter String verb;
25+
private @Getter HttpVerbs verb;
2626
/**
2727
* The scheme to use for API calls.
2828
*/
@@ -83,7 +83,7 @@ public class Request {
8383
// The connection used to make the API call.
8484
private @Getter HttpURLConnection connection;
8585

86-
protected Request(String verb, String path, Params params, String body, String bearerToken,
86+
protected Request(HttpVerbs verb, String path, Params params, String body, String bearerToken,
8787
HTTPClient client) {
8888
Configuration config = client.getConfiguration();
8989

@@ -108,9 +108,9 @@ protected Request(String verb, String path, Params params, String body, String b
108108
// Builds a HttpURLConnection using all the data for this request.
109109
protected void establishConnection() throws IOException {
110110
this.connection = (HttpURLConnection) new URL(uri).openConnection();
111-
connection.setRequestMethod(verb);
111+
connection.setRequestMethod(verb.name());
112112
connection.setDoInput(true);
113-
if (verb.equals(Constants.POST) || verb.equals(Constants.PUT)) {
113+
if (verb == HttpVerbs.POST || verb == HttpVerbs.PUT) {
114114
connection.setDoOutput(true);
115115
}
116116
for (Map.Entry<String, String> entry : headers.entrySet()) {
@@ -140,8 +140,8 @@ private void prepareHeaders() {
140140
headers.put(Constants.CONTENT_TYPE, "application/vnd.amadeus+json");
141141

142142
// Checks if the request needs an X-Http-Method-Override header
143-
if (Constants.APIS_WITH_EXTRA_HEADER.contains(path) && Objects.equals(verb, Constants.POST)) {
144-
headers.put(Constants.X_HTTP_METHOD_OVERRIDE, Constants.GET);
143+
if (Constants.APIS_WITH_EXTRA_HEADER.contains(path) && verb == HttpVerbs.POST) {
144+
headers.put(Constants.X_HTTP_METHOD_OVERRIDE, HttpVerbs.GET.name());
145145
}
146146

147147
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.amadeus.Configuration;
44
import com.amadeus.Constants;
55
import com.amadeus.HTTPClient;
6+
import com.amadeus.HttpVerbs;
67
import com.amadeus.Params;
78
import com.amadeus.Response;
89
import com.amadeus.exceptions.ResponseException;
@@ -65,7 +66,7 @@ private boolean needsRefresh() {
6566
private Response fetchAccessToken() throws ResponseException {
6667
Configuration config = client.getConfiguration();
6768
return client.unauthenticatedRequest(
68-
Constants.POST,
69+
HttpVerbs.POST,
6970
Constants.AUTH_URL,
7071
Params.with(Constants.GRANT_TYPE, Constants.CLIENT_CREDENTIALS)
7172
.and(Constants.CLIENT_ID, config.getClientId())

src/test/java/com/amadeus/AccessTokenTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class AccessTokenTest {
3535
json.addProperty("expires_in", 600);
3636
when(response.getResult()).thenReturn(json);
3737

38-
when(client.unauthenticatedRequest("POST",
38+
when(client.unauthenticatedRequest(HttpVerbs.POST,
3939
"/v1/security/oauth2/token", Params
4040
.with("grant_type", "client_credentials")
4141
.and("client_id", "client_id")
@@ -53,7 +53,7 @@ public class AccessTokenTest {
5353
accessToken.getBearerToken();
5454

5555
verify(client, times(1))
56-
.unauthenticatedRequest(anyString(), anyString(),
56+
.unauthenticatedRequest(any(HttpVerbs.class), anyString(),
5757
any(Params.class), isNull(), isNull());
5858
}
5959

@@ -65,7 +65,7 @@ public class AccessTokenTest {
6565
accessToken.getBearerToken();
6666

6767
verify(client, times(2))
68-
.unauthenticatedRequest(anyString(), anyString(),
68+
.unauthenticatedRequest(any(HttpVerbs.class), anyString(),
6969
any(Params.class), isNull(), isNull());
7070
}
7171
}

0 commit comments

Comments
 (0)