forked from CartoDB/cartodb-java-client
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc68a58
commit 1c6a891
Showing
13 changed files
with
266 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
*.class | ||
bin | ||
test/com/cartodb/Secret.java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,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 interface CartoDBClientIF { | ||
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 String executeQuery(String sqlQuery); | ||
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; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.cartodb; | ||
|
||
public class CartoDBException extends Exception { | ||
|
||
public CartoDBException(String msg) { | ||
super(msg); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.cartodb.impl; | ||
|
||
import java.io.IOException; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLEncoder; | ||
|
||
import javax.smartcardio.CardException; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.scribe.builder.ServiceBuilder; | ||
import org.scribe.model.OAuthRequest; | ||
import org.scribe.model.Response; | ||
import org.scribe.model.Token; | ||
import org.scribe.model.Verb; | ||
import org.scribe.oauth.OAuthService; | ||
|
||
import com.cartodb.CartoDBAPI; | ||
import com.cartodb.CartoDBClientIF; | ||
import com.cartodb.CartoDBException; | ||
|
||
/** | ||
* CartoDB client implementation to access protected resources on CartoDB using OAuth. | ||
* You can perform any SQL queries so please, be careful. | ||
* @author canadensys | ||
* | ||
*/ | ||
public class ApiKeyCartoDBClient extends CartoDBClientIF { | ||
|
||
private static final String DEFAULT_API_VERSION = "1"; | ||
private static final String ENCODING = "UTF-8"; | ||
|
||
private static final String SQL_API_BASE_URL = "http://%s.cartodb.com/api/v%s/sql/"; | ||
|
||
private String user; | ||
private String apiVersion = DEFAULT_API_VERSION; | ||
private String apiURL = null; | ||
|
||
/** | ||
* aki key, it can be found in your dashboard | ||
*/ | ||
private String apiKey; | ||
|
||
|
||
/** | ||
* Default constructor | ||
*/ | ||
public ApiKeyCartoDBClient(){} | ||
|
||
/** | ||
* After this constructor, the object is ready to use. | ||
* @param user | ||
* @param apiKey api provided by cartodb to access to secured resources | ||
* @throws CartoDBException | ||
*/ | ||
public ApiKeyCartoDBClient(String user, String apiKey) throws CartoDBException { | ||
this.user = user; | ||
this.apiKey = apiKey; | ||
if(this.apiKey == null || this.apiKey.length() == 0) { | ||
throw new CartoDBException("provided API key is not valid"); | ||
} | ||
init(); | ||
} | ||
|
||
/** | ||
* Initialization method for a regular CartoDB client. | ||
* You only need it if you're using the default constructor. | ||
*/ | ||
public void init(){ | ||
apiURL = String.format(SQL_API_BASE_URL, user, apiVersion); | ||
} | ||
|
||
/** | ||
* Send a sqlQuery to the CartoDB server. | ||
* The query will be sent in a URL parameter of a GET so, you should avoid very large query string. | ||
* @param sqlQuery | ||
* @throws CartoDBException | ||
*/ | ||
public String executeQuery(String sqlQuery) throws CartoDBException { | ||
String json = null; | ||
|
||
if(apiURL == null){ | ||
System.out.println("Error : uninitialized " + getClass().getName()); | ||
return null; | ||
} | ||
|
||
try { | ||
sqlQuery = URLEncoder.encode(sqlQuery, ENCODING); | ||
String params = "q=" + sqlQuery + "&api_key=" + this.apiKey; | ||
json = IOUtils.toString(new URL(apiURL + "?" + params), ENCODING); | ||
} catch (MalformedURLException e) { | ||
throw new CartoDBException("Could not get URL " + apiURL + sqlQuery); | ||
} catch (UnsupportedEncodingException e) { | ||
e.printStackTrace(); | ||
}catch (IOException e) { | ||
throw new CartoDBException("Could not execute " + sqlQuery + " on CartoDB : "); | ||
} | ||
return json; | ||
} | ||
|
||
public void setUser(String user){ | ||
this.user = user; | ||
} | ||
|
||
/** | ||
* Set the API version to use. | ||
* @param apiVersion number part only as String | ||
*/ | ||
public void setApiVersion(String apiVersion){ | ||
this.apiVersion = apiVersion; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.cartodb; | ||
|
||
import org.junit.Before; | ||
|
||
import com.cartodb.impl.ApiKeyCartoDBClient; | ||
import com.cartodb.impl.SecuredCartoDBClient; | ||
|
||
public class CartoDBApiKeyClient extends CartoDBTestClient { | ||
|
||
|
||
|
||
|
||
@Before | ||
public void setUp() throws Exception { | ||
this.cartoDBCLient = new ApiKeyCartoDBClient(Secret.user, Secret.API_KEY); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.cartodb; | ||
|
||
import org.junit.Before; | ||
|
||
import com.cartodb.impl.SecuredCartoDBClient; | ||
|
||
public class CartoDBSecureClientTest extends CartoDBTestClient { | ||
|
||
|
||
|
||
@Before | ||
public void setUp() throws Exception { | ||
this.cartoDBCLient = new SecuredCartoDBClient(Secret.user, Secret.password, Secret.CONSUMER_KEY, Secret.CONSUMER_SECRET); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.cartodb; | ||
|
||
import java.util.Map; | ||
|
||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
|
||
import com.cartodb.model.CartoDBResponse; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* base class for client tests | ||
* @author javi | ||
* | ||
*/ | ||
@Ignore | ||
public class CartoDBTestClient { | ||
|
||
CartoDBClientIF cartoDBCLient; | ||
|
||
/** | ||
* wrong sql should raise a CartoDBException | ||
* @throws CartoDBException | ||
*/ | ||
@Test(expected=CartoDBException.class) | ||
public void testWrongSql() throws CartoDBException { | ||
CartoDBResponse<String> res = cartoDBCLient.request("select * from asdasdasdas123asd limit 1"); | ||
assertEquals(res.getTotal_rows(), 1); | ||
} | ||
|
||
@Test | ||
public void testClientSQL() throws CartoDBException { | ||
|
||
CartoDBResponse<Map<String, Object>> res = cartoDBCLient.request("select * from " + Secret.EXISTING_TABLE_WITH_DATA + " limit 1"); | ||
assertEquals(res.getTotal_rows(), 1); | ||
assertTrue((Integer)res.getRows().get(0).get("cartodb_id") > 0); | ||
|
||
|
||
} | ||
|
||
} |