Skip to content

Commit f98913a

Browse files
committed
ApiConfig now controls GET vs POST.
Builder patter utilized in the model of the other API calls. POST payload serialized GeolocationApiRequest and passed as string to POST handler Error mapping moved to ApiException.from() Closes #27 Closes #26 Closes #24 Closes #18 Closes #6 Closes #28
1 parent 5281ed9 commit f98913a

File tree

10 files changed

+165
-75
lines changed

10 files changed

+165
-75
lines changed

src/main/java/com/google/maps/GeoApiContext.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ <T, R extends ApiResponse<T>> PendingResult<T> get(ApiConfig config, Class<? ext
141141
config.supportsClientId, query.toString());
142142
}
143143

144-
<T, R extends ApiResponse<T>, P> PendingResult<T> post(ApiConfig config,
144+
<T, R extends ApiResponse<T>> PendingResult<T> post(ApiConfig config,
145145
Class<? extends R> clazz,
146-
P payload) {
146+
Map<String, String> params) {
147147

148148
checkContext(config.supportsClientId);
149149

@@ -168,10 +168,7 @@ <T, R extends ApiResponse<T>, P> PendingResult<T> post(ApiConfig config,
168168
hostName = baseUrlOverride;
169169
}
170170

171-
Gson gson = new Gson();
172-
String jsonPayload = gson.toJson(payload);
173-
174-
return requestHandler.handlePost(hostName, url.toString(), jsonPayload, USER_AGENT, clazz, config.fieldNamingPolicy, errorTimeout);
171+
return requestHandler.handlePost(hostName, url.toString(), params.get("_payload"), USER_AGENT, clazz, config.fieldNamingPolicy, errorTimeout);
175172
}
176173

177174
private <T, R extends ApiResponse<T>> PendingResult<T> getWithPath(Class<R> clazz,

src/main/java/com/google/maps/GeolocationApi.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,18 @@ public class GeolocationApi {
3939
private static final Logger LOG = Logger.getLogger(GeolocationApi.class.getName());
4040

4141
static final ApiConfig GEOLOCATION_API_CONFIG = new ApiConfig("/geolocation/v1/geolocate")
42-
.hostName(API_BASE_URL)
43-
.supportsClientId(false)
44-
.fieldNamingPolicy(FieldNamingPolicy.IDENTITY);
42+
.hostName(API_BASE_URL)
43+
.supportsClientId(false)
44+
.fieldNamingPolicy(FieldNamingPolicy.IDENTITY)
45+
.requestVerb("POST");
4546

4647
private GeolocationApi () {
4748
}
4849

4950
public static PendingResult<GeolocationResult> geolocate(GeoApiContext context, GeolocationPayload payload) {
50-
return context.post(GEOLOCATION_API_CONFIG, Response.class, payload);
51+
return new GeolocationApiRequest(context)
52+
.Payload(payload)
53+
.CreatePayload();
5154
}
5255

5356
public static GeolocationApiRequest newRequest(GeoApiContext context) {
@@ -80,23 +83,7 @@ public ApiException getError() {
8083
return null;
8184
}
8285
ApiException e;
83-
// try and fit the older error codes into the new style geo api error formats
84-
if(reason.equals("keyInvalid")) {
85-
e = ApiException.from("ACCESS_NOT_CONFIGURED", reason +" - "+ message);
86-
} else if(reason.equals("dailyLimitExceeded")) {
87-
e = ApiException.from("RESOURCE_EXHAUSTED", reason +" - "+ message);
88-
} else if(reason.equals("userRateLimitExceeded")) {
89-
e = ApiException.from("RESOURCE_EXHAUSTED", reason +" - "+ message);
90-
} else if(reason.equals("notFound")) {
91-
e = ApiException.from("ZERO_RESULTS", reason +" - "+ message);
92-
} else if(reason.equals("parseError")) {
93-
e = ApiException.from("INVALID_ARGUMENT", reason +" - "+ message);
94-
} else if(reason.equals("invalid")) {
95-
e = ApiException.from("INVALID_ARGUMENT", reason +" - "+ message);
96-
} else {
97-
e = ApiException.from("UNKNOWN_ERROR", reason +" - "+ message);
98-
}
99-
return e;
86+
return ApiException.from(reason, message);
10087
}
10188
}
10289
}

src/main/java/com/google/maps/GeolocationApiRequest.java

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,97 @@
1515

1616
package com.google.maps;
1717

18+
import com.google.gson.Gson;
19+
import com.google.maps.model.CellTower;
1820
import com.google.maps.model.GeolocationPayload;
21+
import com.google.maps.model.GeolocationPayload.GeolocationPayloadBuilder;
1922
import com.google.maps.model.GeolocationResult;
23+
import com.google.maps.model.WifiAccessPoint;
2024

2125
/**
2226
* Request for the Geolocation API.
2327
*/
2428
public class GeolocationApiRequest
2529
extends PendingResultBase<GeolocationResult, GeolocationApiRequest, GeolocationApi.Response>{
2630

27-
private GeolocationPayload payload;
31+
private GeolocationPayload payload = null;
32+
private GeolocationPayloadBuilder builder = null;
33+
2834

2935
GeolocationApiRequest(GeoApiContext context) {
3036
super(context, GeolocationApi.GEOLOCATION_API_CONFIG, GeolocationApi.Response.class);
37+
builder = new GeolocationPayload.GeolocationPayloadBuilder();
3138
}
3239

3340
@Override
3441
protected void validateRequest() {
35-
// TODO: see DirectionsApiRequest for an example on how to validate
42+
if(this.payload.considerIp != null
43+
&& this.payload.considerIp == false
44+
&& this.payload.wifiAccessPoints != null
45+
&& this.payload.wifiAccessPoints.length < 2) {
46+
throw new IllegalArgumentException("Request must contain two or more 'Wifi Access Points'");
47+
}
48+
}
49+
public GeolocationApiRequest HomeMobileCountryCode(int newHomeMobileCountryCode)
50+
{
51+
this.builder.HomeMobileCountryCode(newHomeMobileCountryCode);
52+
return this;
53+
}
54+
public GeolocationApiRequest HomeMobileNetworkCode(int newHomeMobileNetworkCode)
55+
{
56+
this.builder.HomeMobileNetworkCode(newHomeMobileNetworkCode);
57+
return this;
58+
}
59+
public GeolocationApiRequest RadioType(String newRadioType)
60+
{
61+
this.builder.RadioType(newRadioType);
62+
return this;
63+
}
64+
public GeolocationApiRequest Carrier(String newCarrier)
65+
{
66+
this.builder.Carrier(newCarrier);
67+
return this;
68+
}
69+
public GeolocationApiRequest ConsiderIp(boolean newConsiderIp)
70+
{
71+
this.builder.ConsiderIp(newConsiderIp);
72+
return this;
73+
}
74+
public GeolocationApiRequest CellTowers(CellTower[] newCellTowers)
75+
{
76+
this.builder.CellTowers(newCellTowers);
77+
return this;
78+
}
79+
public GeolocationApiRequest AddCellTower(CellTower newCellTower)
80+
{
81+
this.builder.AddCellTower(newCellTower);
82+
return this;
83+
}
84+
public GeolocationApiRequest WifiAccessPoints(WifiAccessPoint[] newWifiAccessPoints)
85+
{
86+
this.builder.WifiAccessPoints(newWifiAccessPoints);
87+
return this;
88+
}
89+
public GeolocationApiRequest AddWifiAccessPoint(WifiAccessPoint newWifiAccessPoint)
90+
{
91+
this.builder.AddWifiAccessPoint(newWifiAccessPoint);
92+
return this;
93+
}
94+
public GeolocationApiRequest Payload(GeolocationPayload payload)
95+
{
96+
this.payload = payload;
97+
return this;
98+
}
99+
public GeolocationApiRequest CreatePayload()
100+
{
101+
if(this.payload == null) {
102+
// if the payload has not been set, create it
103+
this.payload = this.builder.createGeolocationPayload();
104+
} else {
105+
// use the payload that has been explicitly set by the Payload method above
106+
}
107+
Gson gson = new Gson();
108+
String jsonPayload = gson.toJson(this.payload);
109+
return param("_payload", jsonPayload);
36110
}
37111
}

src/main/java/com/google/maps/OkHttpRequestHandler.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ public <T, R extends ApiResponse<T>> PendingResult<T> handlePost(String hostName
6868
.header("User-Agent", userAgent)
6969
.url(hostName + url).build();
7070

71-
// TODO: remove these logs
72-
LOG.log(Level.INFO, "Request: {0}", hostName + url);
73-
LOG.log(Level.INFO, "Request Body: {0}", payload);
74-
7571
return new OkHttpPendingResult<T, R>(req, client, clazz, fieldNamingPolicy, errorTimeout);
7672
}
7773
@Override

src/main/java/com/google/maps/PendingResultBase.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ private PendingResult<T> makeRequest() {
7575
"'await', 'awaitIgnoreError' or 'setCallback' was already called.");
7676
}
7777
validateRequest();
78-
delegate = context.get(config, responseClass, params);
78+
if(config.requestVerb == "GET") {
79+
delegate = context.get(config, responseClass, params);
80+
} else if (config.requestVerb == "POST") {
81+
delegate = context.post(config, responseClass, params);
82+
}
7983
return delegate;
8084
}
8185

src/main/java/com/google/maps/errors/ApiException.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ public static ApiException from(String status, String errorMessage) {
6565
return new RequestDeniedException(errorMessage);
6666
}
6767

68+
// Geolocation Errors
69+
if("keyInvalid".equals(status)) {
70+
return new AccessNotConfiguredException(errorMessage);
71+
} else if("dailyLimitExceeded".equals(status)) {
72+
return new OverDailyLimitException(errorMessage);
73+
} else if("userRateLimitExceeded".equals(status)) {
74+
return new OverQueryLimitException(errorMessage);
75+
} else if("notFound".equals(status)) {
76+
return new NotFoundException(errorMessage);
77+
} else if("parseError".equals(status)) {
78+
return new InvalidRequestException(errorMessage);
79+
} else if("invalid".equals(status)) {
80+
return new InvalidRequestException(errorMessage);
81+
}
82+
6883
// We've hit an unknown error. This is not a state we should hit,
6984
// but we don't want to crash a user's application if we introduce a new error.
7085
return new UnknownErrorException("An unexpected error occurred. "

src/main/java/com/google/maps/internal/ApiConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class ApiConfig {
2525
public FieldNamingPolicy fieldNamingPolicy = FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES;
2626
public String hostName = "https://maps.googleapis.com";
2727
public boolean supportsClientId = true;
28+
public String requestVerb = "GET";
2829

2930
public ApiConfig(String path) {
3031
this.path = path;
@@ -44,4 +45,9 @@ public ApiConfig supportsClientId(boolean supportsClientId) {
4445
this.supportsClientId = supportsClientId;
4546
return this;
4647
}
48+
49+
public ApiConfig requestVerb(String requestVerb) {
50+
this.requestVerb = requestVerb;
51+
return this;
52+
}
4753
}

src/main/java/com/google/maps/internal/GeolocationResponseAdapter.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
import com.google.gson.stream.JsonWriter;
77
import com.google.maps.GeolocationApi;
88

9-
import sun.rmi.runtime.Log;
10-
119
import java.io.IOException;
12-
import java.util.logging.Level;
1310
import java.util.logging.Logger;
1411

1512

src/main/java/com/google/maps/internal/OkHttpPendingResult.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,6 @@ private T parseResponse(OkHttpPendingResult<T, R> request, Response response) th
216216
R resp;
217217
String contentType = response.header("Content-Type");
218218

219-
// TODO: remove these logs
220-
LOG.log(Level.INFO, "Response: {0}", response);
221-
LOG.log(Level.INFO, "Response Body: {0}", new String(bytes, "utf8"));
222-
223219
// Places Photo API special case
224220
if (contentType != null &&
225221
contentType.startsWith("image") &&
@@ -257,8 +253,6 @@ private T parseResponse(OkHttpPendingResult<T, R> request, Response response) th
257253
try {
258254
resp = gson.fromJson(new String(bytes, "utf8"), responseClass);
259255
} catch (JsonSyntaxException e) {
260-
// TODO: remove these logs
261-
LOG.log(Level.INFO, "JsonSyntaxException: {0}", e);
262256
// Check HTTP status for a more suitable exception
263257
if (!response.isSuccessful()) {
264258
// Some of the APIs return 200 even when the API request fails, as long as the transport

0 commit comments

Comments
 (0)