1 | |
package org.codeforamerica.open311.facade; |
2 | |
|
3 | |
import java.io.IOException; |
4 | |
import java.net.MalformedURLException; |
5 | |
import java.net.URL; |
6 | |
import java.util.HashMap; |
7 | |
import java.util.LinkedList; |
8 | |
import java.util.List; |
9 | |
import java.util.Map; |
10 | |
|
11 | |
import org.codeforamerica.open311.facade.data.Attribute; |
12 | |
import org.codeforamerica.open311.facade.data.POSTServiceRequestResponse; |
13 | |
import org.codeforamerica.open311.facade.data.RelationshipManager; |
14 | |
import org.codeforamerica.open311.facade.data.Service; |
15 | |
import org.codeforamerica.open311.facade.data.ServiceDefinition; |
16 | |
import org.codeforamerica.open311.facade.data.ServiceRequest; |
17 | |
import org.codeforamerica.open311.facade.data.ServiceRequestIdResponse; |
18 | |
import org.codeforamerica.open311.facade.data.operations.GETServiceRequestsFilter; |
19 | |
import org.codeforamerica.open311.facade.data.operations.POSTServiceRequestData; |
20 | |
import org.codeforamerica.open311.facade.exceptions.APIWrapperException; |
21 | |
import org.codeforamerica.open311.facade.exceptions.APIWrapperException.Error; |
22 | |
import org.codeforamerica.open311.facade.exceptions.DataParsingException; |
23 | |
import org.codeforamerica.open311.facade.exceptions.GeoReportV2Error; |
24 | |
import org.codeforamerica.open311.facade.exceptions.InvalidValueError; |
25 | |
import org.codeforamerica.open311.internals.caching.Cache; |
26 | |
import org.codeforamerica.open311.internals.logging.LogManager; |
27 | |
import org.codeforamerica.open311.internals.network.NetworkManager; |
28 | |
import org.codeforamerica.open311.internals.network.URLBuilder; |
29 | |
import org.codeforamerica.open311.internals.parsing.DataParser; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class APIWrapper { |
39 | |
|
40 | |
private String endpointUrl; |
41 | |
private String apiKey; |
42 | |
private String jurisdictionId; |
43 | |
private EndpointType type; |
44 | |
private DataParser dataParser; |
45 | |
private NetworkManager networkManager; |
46 | |
private URLBuilder urlBuilder; |
47 | |
private Cache cache; |
48 | |
|
49 | |
|
50 | |
|
51 | 10 | private LogManager logManager = LogManager.getInstance(); |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
APIWrapper(String endpointUrl, Format format, |
67 | |
EndpointType type, DataParser dataParser, |
68 | |
NetworkManager networkManager, Cache cache, String jurisdictionId, |
69 | |
String apiKey) { |
70 | 10 | super(); |
71 | 10 | this.endpointUrl = endpointUrl; |
72 | 10 | this.type = type; |
73 | 10 | this.dataParser = dataParser; |
74 | 10 | this.networkManager = networkManager; |
75 | 10 | this.cache = cache; |
76 | 10 | this.jurisdictionId = jurisdictionId; |
77 | 10 | this.apiKey = apiKey; |
78 | 10 | this.urlBuilder = new URLBuilder(endpointUrl, this.jurisdictionId, |
79 | |
format.toString()); |
80 | 10 | } |
81 | |
|
82 | |
public String getEndpointUrl() { |
83 | 9 | return endpointUrl; |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public String getWrapperInfo() { |
92 | 3 | return endpointUrl + " - " + type.toString(); |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public Cache getCache() { |
101 | 0 | return cache; |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public void setFormat(Format format) { |
112 | 0 | logManager.logInfo(this, |
113 | |
"Changing wrapper format to " + format.toString()); |
114 | 0 | urlBuilder = new URLBuilder(endpointUrl, jurisdictionId, |
115 | |
format.toString()); |
116 | 0 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public List<Service> getServiceList() throws APIWrapperException { |
128 | 3 | logManager.logInfo(this, "GET Service List"); |
129 | 3 | List<Service> result = cache |
130 | |
.retrieveCachedServiceList(this.endpointUrl); |
131 | 3 | if (result == null) { |
132 | 3 | result = askEndpointForTheServiceList(); |
133 | 3 | cache.saveListOfServices(endpointUrl, result); |
134 | |
} |
135 | 3 | if (result != null) { |
136 | 3 | RelationshipManager.getInstance().addServiceWrapperRelationship( |
137 | |
result, this); |
138 | |
} |
139 | 3 | return result; |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
private List<Service> askEndpointForTheServiceList() |
150 | |
throws APIWrapperException { |
151 | 3 | logManager.logInfo(this, |
152 | |
"GET Service List is not cached, asking endpoint."); |
153 | 3 | String rawServiceListData = ""; |
154 | |
try { |
155 | 3 | URL serviceListUrl = urlBuilder.buildGetServiceListUrl(); |
156 | 3 | rawServiceListData = networkGet(serviceListUrl); |
157 | 3 | return dataParser.parseServiceList(rawServiceListData); |
158 | 0 | } catch (DataParsingException e) { |
159 | 0 | tryToParseError(rawServiceListData); |
160 | 0 | return null; |
161 | 0 | } catch (MalformedURLException e) { |
162 | 0 | throw new APIWrapperException(e.getMessage(), Error.URL_BUILDER, |
163 | |
null); |
164 | |
} |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
public ServiceDefinition getServiceDefinition(String serviceCode) |
179 | |
throws APIWrapperException { |
180 | 3 | logManager.logInfo(this, "GET Service Definition (service_code: " |
181 | |
+ serviceCode + ")"); |
182 | 3 | ServiceDefinition result = cache.retrieveCachedServiceDefinition( |
183 | |
endpointUrl, serviceCode); |
184 | 3 | if (result == null) { |
185 | 3 | result = askEndpointForAServiceDefinition(serviceCode); |
186 | 3 | cache.saveServiceDefinition(endpointUrl, serviceCode, result); |
187 | |
} |
188 | 3 | return result; |
189 | |
} |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
private ServiceDefinition askEndpointForAServiceDefinition( |
200 | |
String serviceCode) throws APIWrapperException { |
201 | 3 | logManager.logInfo(this, "GET Service Definition (service_code: " |
202 | |
+ serviceCode + ") is not cached, asking endpoint."); |
203 | 3 | String rawServiceDefinitionData = ""; |
204 | |
try { |
205 | 3 | URL serviceDefinitionUrl = urlBuilder |
206 | |
.buildGetServiceDefinitionUrl(serviceCode); |
207 | 3 | rawServiceDefinitionData = networkGet(serviceDefinitionUrl); |
208 | 3 | return dataParser.parseServiceDefinition(rawServiceDefinitionData); |
209 | 0 | } catch (DataParsingException e) { |
210 | 0 | tryToParseError(rawServiceDefinitionData); |
211 | 0 | return null; |
212 | 0 | } catch (MalformedURLException e) { |
213 | 0 | throw new APIWrapperException(e.getMessage(), Error.URL_BUILDER, |
214 | |
null); |
215 | |
} |
216 | |
} |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
public ServiceRequestIdResponse getServiceRequestIdFromToken(String token) |
228 | |
throws APIWrapperException { |
229 | 1 | logManager.logInfo(this, "GET Service Request Id from token (token: " |
230 | |
+ token + "), asking endpoint."); |
231 | 1 | String rawServiceRequestId = ""; |
232 | |
try { |
233 | 1 | URL serviceDefinitionUrl = urlBuilder |
234 | |
.buildGetServiceRequestIdFromATokenUrl(token); |
235 | 1 | rawServiceRequestId = networkGet(serviceDefinitionUrl); |
236 | 1 | return dataParser |
237 | |
.parseServiceRequestIdFromAToken(rawServiceRequestId); |
238 | 0 | } catch (DataParsingException e) { |
239 | 0 | tryToParseError(rawServiceRequestId); |
240 | 0 | return null; |
241 | 0 | } catch (MalformedURLException e) { |
242 | 0 | throw new APIWrapperException(e.getMessage(), Error.URL_BUILDER, |
243 | |
null); |
244 | |
} |
245 | |
} |
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
public List<ServiceRequest> getServiceRequests( |
258 | |
GETServiceRequestsFilter operationData) throws APIWrapperException { |
259 | 2 | logManager.logInfo(this, "GET Service Requests"); |
260 | 2 | operationData = operationData == null ? new GETServiceRequestsFilter() |
261 | |
: operationData; |
262 | 2 | List<ServiceRequest> result = cache.retrieveCachedServiceRequests( |
263 | |
endpointUrl, operationData); |
264 | 2 | if (result == null) { |
265 | 2 | result = askEndpointForServiceRequests(operationData); |
266 | 2 | cache.saveServiceRequestList(endpointUrl, operationData, result); |
267 | |
} |
268 | 2 | return result; |
269 | |
} |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
private List<ServiceRequest> askEndpointForServiceRequests( |
281 | |
GETServiceRequestsFilter operationData) throws APIWrapperException { |
282 | 2 | logManager |
283 | |
.logInfo(this, |
284 | |
"GET Service Requests with the given filter is not cached, asking endpoint."); |
285 | 2 | String rawServiceRequests = ""; |
286 | |
try { |
287 | 2 | URL serviceRequestsUrl = operationData != null ? urlBuilder |
288 | |
.buildGetServiceRequests(operationData |
289 | |
.getOptionalParametersMap()) : urlBuilder |
290 | |
.buildGetServiceRequests(null); |
291 | 2 | rawServiceRequests = networkGet(serviceRequestsUrl); |
292 | 2 | return dataParser.parseServiceRequests(rawServiceRequests); |
293 | 0 | } catch (DataParsingException e) { |
294 | 0 | tryToParseError(rawServiceRequests); |
295 | 0 | return null; |
296 | 0 | } catch (MalformedURLException e) { |
297 | 0 | throw new APIWrapperException(e.getMessage(), Error.URL_BUILDER, |
298 | |
null); |
299 | |
} |
300 | |
} |
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
public ServiceRequest getServiceRequest(String serviceRequestId) |
312 | |
throws APIWrapperException { |
313 | 2 | logManager.logInfo(this, "GET Service Request (service_request_id: " |
314 | |
+ serviceRequestId + ")"); |
315 | 2 | ServiceRequest result = cache.retrieveCachedServiceRequest(endpointUrl, |
316 | |
serviceRequestId); |
317 | 2 | if (result == null) { |
318 | 2 | result = askEndpointForAServiceRequest(serviceRequestId); |
319 | 2 | cache.saveSingleServiceRequest(endpointUrl, serviceRequestId, |
320 | |
result); |
321 | |
} |
322 | 2 | return result; |
323 | |
} |
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
private ServiceRequest askEndpointForAServiceRequest(String serviceRequestId) |
335 | |
throws APIWrapperException { |
336 | 2 | logManager.logInfo(this, "GET Service Request (service_request_id: " |
337 | |
+ serviceRequestId + ") is not cached, asking endpoint."); |
338 | 2 | String rawServiceRequests = ""; |
339 | |
try { |
340 | 2 | URL serviceRequestsUrl = urlBuilder |
341 | |
.buildGetServiceRequest(serviceRequestId); |
342 | 2 | rawServiceRequests = networkGet(serviceRequestsUrl); |
343 | 2 | List<ServiceRequest> parsedServiceRequests = dataParser |
344 | |
.parseServiceRequests(rawServiceRequests); |
345 | 2 | return parsedServiceRequests.size() > 0 ? parsedServiceRequests |
346 | |
.get(0) : null; |
347 | 0 | } catch (DataParsingException e) { |
348 | 0 | tryToParseError(rawServiceRequests); |
349 | 0 | return null; |
350 | 0 | } catch (MalformedURLException e) { |
351 | 0 | throw new APIWrapperException(e.getMessage(), Error.URL_BUILDER, |
352 | |
null); |
353 | |
} |
354 | |
} |
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
public POSTServiceRequestResponse postServiceRequest( |
367 | |
POSTServiceRequestData operationData) throws APIWrapperException { |
368 | 5 | logManager.logInfo(this, "POST Service Request"); |
369 | 5 | if (operationData == null) { |
370 | 0 | throw new InvalidValueError("The given parameter is null"); |
371 | |
} |
372 | 5 | Map<String, String> optionalArguments = operationData |
373 | |
.getBodyRequestParameters() != null ? operationData |
374 | |
.getBodyRequestParameters() : new HashMap<String, String>(); |
375 | 5 | List<Attribute> attributes = operationData.getAttributes() != null ? operationData |
376 | |
.getAttributes() : new LinkedList<Attribute>(); |
377 | |
try { |
378 | 5 | URL url = urlBuilder.buildPostServiceRequestUrl(); |
379 | 5 | return postServiceRequestInternal(url, optionalArguments, |
380 | |
attributes); |
381 | 0 | } catch (MalformedURLException e) { |
382 | 0 | throw new APIWrapperException(e.getMessage(), Error.URL_BUILDER, |
383 | |
null); |
384 | |
} |
385 | |
} |
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
private POSTServiceRequestResponse postServiceRequestInternal(URL url, |
403 | |
Map<String, String> arguments, List<Attribute> attributes) |
404 | |
throws APIWrapperException, MalformedURLException { |
405 | 5 | if (apiKey.length() > 0) { |
406 | 1 | arguments.put("api_key", apiKey); |
407 | |
} |
408 | 5 | if (jurisdictionId.length() > 0) { |
409 | 0 | arguments.put("jurisdiction_id", jurisdictionId); |
410 | |
} |
411 | 5 | Map<String, String> postArguments = urlBuilder |
412 | |
.buildPostServiceRequestBody(arguments, attributes); |
413 | 5 | String rawPostServiceRequestResponse = networkPost(url, postArguments); |
414 | |
try { |
415 | 4 | return dataParser |
416 | |
.parsePostServiceRequestResponse(rawPostServiceRequestResponse); |
417 | 1 | } catch (DataParsingException e) { |
418 | 1 | tryToParseError(rawPostServiceRequestResponse); |
419 | 0 | return null; |
420 | |
} |
421 | |
} |
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | |
private void tryToParseError(String rawData) throws APIWrapperException { |
435 | 1 | logManager.logError(this, |
436 | |
"There was an error, trying checking if it was an API error"); |
437 | |
try { |
438 | 1 | GeoReportV2Error error = dataParser.parseGeoReportV2Errors(rawData); |
439 | 1 | throw new APIWrapperException("GeoReport_v2 error", |
440 | |
Error.GEO_REPORT_V2, error); |
441 | 0 | } catch (DataParsingException ex) { |
442 | 0 | logManager.logError(this, |
443 | |
"The error couldn't be parsed (it is not an API error)."); |
444 | 0 | throw new APIWrapperException(ex.getMessage(), Error.DATA_PARSING, |
445 | |
null); |
446 | |
} |
447 | |
} |
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
protected String networkGet(URL url) throws APIWrapperException { |
459 | 11 | logManager.logInfo(this, "HTTP GET " + url.toString()); |
460 | |
try { |
461 | 11 | String response = networkManager.doGet(url); |
462 | 11 | String cutResponse = response.length() > 50 ? response.substring(0, |
463 | |
50) + "..." : response; |
464 | 11 | logManager.logInfo(this, |
465 | |
"HTTP GET response (50 or less first characters)" |
466 | |
+ cutResponse); |
467 | 11 | return response; |
468 | 0 | } catch (IOException e) { |
469 | 0 | logManager.logError(this, "HTTP GET error: " + e.getMessage()); |
470 | 0 | throw new APIWrapperException(e.getMessage(), |
471 | |
Error.NETWORK_MANAGER, null); |
472 | |
} |
473 | |
} |
474 | |
|
475 | |
|
476 | |
|
477 | |
|
478 | |
|
479 | |
|
480 | |
|
481 | |
|
482 | |
|
483 | |
|
484 | |
|
485 | |
|
486 | |
protected String networkPost(URL url, Map<String, String> parameters) |
487 | |
throws APIWrapperException { |
488 | 5 | logManager.logInfo(this, "HTTP POST " + url.toString()); |
489 | |
try { |
490 | 5 | String response = networkManager.doPost(url, parameters); |
491 | 4 | String cutResponse = response.length() > 50 ? response.substring(0, |
492 | |
50) + "..." : response; |
493 | 4 | logManager.logInfo(this, |
494 | |
"HTTP POST response (50 or less first characters)" |
495 | |
+ cutResponse); |
496 | 4 | return response; |
497 | 1 | } catch (IOException e) { |
498 | 1 | logManager.logError(this, "HTTP POST error: " + e.getMessage()); |
499 | 1 | throw new APIWrapperException(e.getMessage(), |
500 | |
Error.NETWORK_MANAGER, null); |
501 | |
} |
502 | |
} |
503 | |
|
504 | |
public String toString() { |
505 | 0 | return this.getWrapperInfo(); |
506 | |
} |
507 | |
} |