forked from CartoDB/cartodb-java-client
/
CartoDBClientIF.java
49 lines (40 loc) · 1.34 KB
/
CartoDBClientIF.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.cartodb;
import java.io.IOException;
import com.cartodb.model.CartoDBResponse;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Interface for CartoDB client implementation
* @author canadensys
* @param <T>
*
*/
public abstract class CartoDBClientIF {
ObjectMapper jsonMapper = new ObjectMapper();
/**
* Executes a query on the CartoDB server
* @param sqlQuery do not URL encode the query, this function will do it.
* @return response as JSON string or null if the query cannot be completed
* @throws CartoDBException
*/
public abstract String executeQuery(String sqlQuery) throws CartoDBException;
/**
* return a CartoDBResponse object with the response json parsed
* @param <T> object to map the columns
* @param sqlQuery
* @return CartoDBResponse object
* @throws CartoDBException
*/
public <T> CartoDBResponse<T> request(String sqlQuery) throws CartoDBException {
String json = executeQuery(sqlQuery);
CartoDBResponse<T> response;
try {
response = jsonMapper.readValue(json, new TypeReference<CartoDBResponse<T>>(){});
} catch (Exception e) {
throw new CartoDBException(e.getMessage());
}
return response;
}
}