Skip to content

Commit b1066bd

Browse files
authored
Merge pull request #9 from anthonyroux/issue_8_string_handling
Issue #8 - Extract consts in a dedicated file
2 parents b235133 + a088abf commit b1066bd

File tree

5 files changed

+78
-26
lines changed

5 files changed

+78
-26
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.amadeus;
2+
3+
/**
4+
* A class for constant variables.
5+
*
6+
* <pre>
7+
* To use: Constants.VARIABLE
8+
* </pre>
9+
*/
10+
public final class Constants {
11+
// HTTP verbs
12+
public static final String GET = "GET";
13+
public static final String POST = "POST";
14+
15+
public static final String HTTPS = "https";
16+
public static final String HTTP = "http";
17+
18+
public static final String USER_AGENT = "User-Agent";
19+
public static final String ACCEPT = "Accept";
20+
public static final String AUTHORIZATION = "Authorization";
21+
public static final String CONTENT_TYPE = "Content-Type";
22+
23+
// Pagination
24+
public static final String FIRST = "first";
25+
public static final String LAST = "last";
26+
public static final String NEXT = "next";
27+
public static final String PREVIOUS = "previous";
28+
29+
// Authorization
30+
public static final String GRANT_TYPE = "grant_type";
31+
public static final String CLIENT_CREDENTIALS = "client_credentials";
32+
public static final String CLIENT_ID = "client_id";
33+
public static final String CLIENT_SECRET = "client_secret";
34+
public static final String AUTH_URL = "/v1/security/oauth2/token";
35+
public static final String ACCESS_TOKEN = "access_token";
36+
public static final String EXPIRES_IN = "expires_in";
37+
38+
39+
/**
40+
* The caller references the constants using <tt>Consts.EMPTY_STRING</tt>,
41+
* and so on. Thus, the caller should be prevented from constructing objects of
42+
* this class, by declaring this private constructor.
43+
*/
44+
private Constants() {
45+
throw new AssertionError();
46+
}
47+
}

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.amadeus;
22

3+
import com.amadeus.Constants;
34
import com.amadeus.client.AccessToken;
45
import com.amadeus.exceptions.NetworkException;
56
import com.amadeus.exceptions.ResponseException;
@@ -35,7 +36,7 @@ protected HTTPClient(Configuration configuration) {
3536
* @see Amadeus#get(String, Params)
3637
*/
3738
public Response get(String path) throws ResponseException {
38-
return request("GET", path, null);
39+
return request(Constants.GET, path, null);
3940
}
4041

4142
/**
@@ -62,7 +63,7 @@ public Response get(String path) throws ResponseException {
6263
* @return a Response object containing the status code, body, and parsed data.
6364
*/
6465
public Response get(String path, Params params) throws ResponseException {
65-
return request("GET", path, params);
66+
return request(Constants.GET, path, params);
6667
}
6768

6869
/**
@@ -72,7 +73,7 @@ public Response get(String path, Params params) throws ResponseException {
7273
* @see Amadeus#post(String, Params)
7374
*/
7475
public Response post(String path) throws ResponseException {
75-
return request("POST", path, null);
76+
return request(Constants.POST, path, null);
7677
}
7778

7879
/**
@@ -99,7 +100,7 @@ public Response post(String path) throws ResponseException {
99100
* @return a Response object containing the status code, body, and parsed data.
100101
*/
101102
public Response post(String path, Params params) throws ResponseException {
102-
return request("POST", path, params);
103+
return request(Constants.POST, path, params);
103104
}
104105

105106
/**
@@ -123,7 +124,7 @@ public Response unauthenticatedRequest(String verb, String path, Params params,
123124
* @throws ResponseException if the page could not be found
124125
*/
125126
public Response previous(Response response) throws ResponseException {
126-
return page("previous", response);
127+
return page(Constants.PREVIOUS, response);
127128
}
128129

129130
/**
@@ -133,7 +134,7 @@ public Response previous(Response response) throws ResponseException {
133134
* @throws ResponseException if the page could not be found
134135
*/
135136
public Resource[] previous(Resource resource) throws ResponseException {
136-
return page("previous", resource);
137+
return page(Constants.PREVIOUS, resource);
137138
}
138139

139140
/**
@@ -143,7 +144,7 @@ public Resource[] previous(Resource resource) throws ResponseException {
143144
* @throws ResponseException if the page could not be found
144145
*/
145146
public Response next(Response response) throws ResponseException {
146-
return page("next", response);
147+
return page(Constants.NEXT, response);
147148
}
148149

149150
/**
@@ -153,7 +154,7 @@ public Response next(Response response) throws ResponseException {
153154
* @throws ResponseException if the page could not be found
154155
*/
155156
public Resource[] next(Resource resource) throws ResponseException {
156-
return page("next", resource);
157+
return page(Constants.NEXT, resource);
157158
}
158159

159160
/**
@@ -163,7 +164,7 @@ public Resource[] next(Resource resource) throws ResponseException {
163164
* @throws ResponseException if the page could not be found
164165
*/
165166
public Response first(Response response) throws ResponseException {
166-
return page("first", response);
167+
return page(Constants.FIRST, response);
167168
}
168169

169170
/**
@@ -173,7 +174,7 @@ public Response first(Response response) throws ResponseException {
173174
* @throws ResponseException if the page could not be found
174175
*/
175176
public Resource[] first(Resource resource) throws ResponseException {
176-
return page("first", resource);
177+
return page(Constants.FIRST, resource);
177178
}
178179

179180
/**
@@ -183,7 +184,7 @@ public Resource[] first(Resource resource) throws ResponseException {
183184
* @throws ResponseException if the page could not be found
184185
*/
185186
public Response last(Response response) throws ResponseException {
186-
return page("last", response);
187+
return page(Constants.LAST, response);
187188
}
188189

189190
/**
@@ -193,7 +194,7 @@ public Response last(Response response) throws ResponseException {
193194
* @throws ResponseException if the page could not be found
194195
*/
195196
public Resource[] last(Resource resource) throws ResponseException {
196-
return page("last", resource);
197+
return page(Constants.LAST, resource);
197198
}
198199

199200
// A generic method for making requests of any verb.
@@ -237,7 +238,7 @@ private Request fetch(Request request) throws NetworkException {
237238

238239
// Writes the parameters to the request.
239240
private void write(Request request) throws IOException {
240-
if (request.getVerb() == "POST" && request.getParams() != null) {
241+
if (request.getVerb() == Constants.POST && request.getParams() != null) {
241242
OutputStream os = request.getConnection().getOutputStream();
242243
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
243244
writer.write(request.getParams().toQueryString());

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.amadeus;
22

3+
import com.amadeus.Constants;
4+
35
import java.io.IOException;
46
import java.net.HttpURLConnection;
57
import java.net.URL;
@@ -112,7 +114,7 @@ protected void establishConnection() throws IOException {
112114

113115
// Determines the scheme based on the SSL value
114116
private void determineScheme() {
115-
this.scheme = isSsl() ? "https" : "http";
117+
this.scheme = isSsl() ? Constants.HTTPS : Constants.HTTP;
116118
}
117119

118120
// Prepares the full URL based on the scheme, host, port and path.
@@ -124,10 +126,10 @@ private void prepareUrl() {
124126
// Prepares the headers to be sent in the request
125127
private void prepareHeaders() {
126128
this.headers = new HashMap<String, String>();
127-
headers.put("User-Agent", buildUserAgent());
128-
headers.put("Accept", "application/json, application/vnd.amadeus+json");
129+
headers.put(Constants.USER_AGENT, buildUserAgent());
130+
headers.put(Constants.ACCEPT, "application/json, application/vnd.amadeus+json");
129131
if (bearerToken != null) {
130-
headers.put("Authorization", bearerToken);
132+
headers.put(Constants.AUTHORIZATION, bearerToken);
131133
}
132134
}
133135

@@ -144,7 +146,7 @@ private String buildUserAgent() {
144146

145147
// Gets the serialized params, only if this is a Get call
146148
private String getQueryParams() {
147-
if (verb == "GET" && params != null) {
149+
if (verb == Constants.GET && params != null) {
148150
return params.toQueryString();
149151
} else {
150152
return "";

src/main/java/com/amadeus/Response.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.amadeus;
22

3+
import com.amadeus.Constants;
34
import com.amadeus.exceptions.AuthenticationException;
45
import com.amadeus.exceptions.ClientException;
56
import com.amadeus.exceptions.NotFoundException;
@@ -152,7 +153,7 @@ private boolean isJson() {
152153

153154
// Checks if the response headers include a JSON mime-type.
154155
private boolean hasJsonHeader() {
155-
String contentType = getRequest().getConnection().getHeaderField("Content-Type");
156+
String contentType = getRequest().getConnection().getHeaderField(Constants.CONTENT_TYPE);
156157
String[] expectedContentTypes = new String[] {
157158
"application/json", "application/vnd.amadeus+json"
158159
};

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.amadeus.client;
22

33
import com.amadeus.Configuration;
4+
import com.amadeus.Constants;
45
import com.amadeus.HTTPClient;
56
import com.amadeus.Params;
67
import com.amadeus.Response;
@@ -64,19 +65,19 @@ private boolean needsRefresh() {
6465
private Response fetchAccessToken() throws ResponseException {
6566
Configuration config = client.getConfiguration();
6667
return client.unauthenticatedRequest(
67-
"POST",
68-
"/v1/security/oauth2/token",
69-
Params.with("grant_type", "client_credentials")
70-
.and("client_id", config.getClientId())
71-
.and("client_secret", config.getClientSecret()),
68+
Constants.POST,
69+
Constants.AUTH_URL,
70+
Params.with(Constants.GRANT_TYPE, Constants.CLIENT_CREDENTIALS)
71+
.and(Constants.CLIENT_ID, config.getClientId())
72+
.and(Constants.CLIENT_SECRET, config.getClientSecret()),
7273
null
7374
);
7475
}
7576

7677
// Store the fetched access token and expiry date
7778
private void storeAccessToken(JsonObject result) {
78-
this.accessToken = result.get("access_token").getAsString();
79-
int expiresIn = result.get("expires_in").getAsInt();
79+
this.accessToken = result.get(Constants.ACCESS_TOKEN).getAsString();
80+
int expiresIn = result.get(Constants.EXPIRES_IN).getAsInt();
8081
this.expiresAt = System.currentTimeMillis() + expiresIn * 1000L;
8182
}
8283
}

0 commit comments

Comments
 (0)