Skip to content

Commit

Permalink
Changed getRecords to use SQL API
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartones committed Oct 17, 2014
1 parent 9468b78 commit ac0b770
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
17 changes: 12 additions & 5 deletions cartodb.class.php
Expand Up @@ -43,6 +43,7 @@ function __construct($config) {
$this->TEMP_TOKEN_FILE_PATH = sys_get_temp_dir() . '/' . $this->subdomain . '.cartodbtempkey.txt';
$this->OAUTH_URL = 'https://' . $this->subdomain . '.cartodb.com/oauth/';
$this->API_URL = 'https://' . $this->subdomain . '.cartodb.com/api/v1/';
$this->API_URL_V2 = 'https://' . $this->subdomain . '.cartodb.com/api/v2/';

try {
if (file_exists($this->TEMP_TOKEN_FILE_PATH)) {
Expand All @@ -62,7 +63,8 @@ function __toString() {
return "OAuthConsumer[key=$this->key, secret=$this->secret]";
}

private function request($uri, $method = 'GET', $args = array()) {
private function request($uri, $method = 'GET', $args = array(), $apiVersion = 1) {
$url = ($apiVersion == 2 ? $this->API_URL_V2 : $this->API_URL) . $uri;
$url = $this->API_URL . $uri;
$sig_method = new OAuthSignatureMethod_HMAC_SHA1();
$consumer = new OAuthConsumer($this->key, $this->secret, NULL);
Expand Down Expand Up @@ -100,9 +102,14 @@ private function request($uri, $method = 'GET', $args = array()) {
return $response;
}

public function runSql($sql) {
$params = array('q' => $sql);
$response = $this->request('sql', 'POST', array('params' => $params));
public function runSql($sql, $additionalParams = array()) {
$params = array_merge(array(
'q' => $sql,
'rows_per_page' => 40,
'page' => 0,
), $additionalParams);

$response = $this->request('sql', 'POST', array('params' => $params), 2);

if ($response['info']['http_code'] != 200) {
throw new Exception('There was a problem with your request: ' . var_export($response['return'], true));
Expand Down Expand Up @@ -256,7 +263,7 @@ public function deleteRow($table, $row_id) {
* - 'page' : Page index.
*/
public function getRecords($table, $params = array()) {
return $this->request("tables/$table/records", 'GET', array('params' => $params));
return $this->runSql("SELECT * FROM $table", $params);
}

private function getAccessToken() {
Expand Down
6 changes: 4 additions & 2 deletions example.php
Expand Up @@ -9,7 +9,7 @@
// Check if the $key and $secret work fine and you are authorized
if (!$cartodb->authorized) {
error_log("uauth");
print 'There is a problem authenticating, check the key and secret.';
print 'There is a problem authenticating, check the key and secret.\n';
exit();
}

Expand Down Expand Up @@ -54,7 +54,9 @@
$response = $cartodb->deleteRow($tableName, $row->id);
print_r($response);

$response = $cartodb->getRecords($tableName, array('rows_per_page' => 0));

$response = $cartodb->getRecords($tableName, array('rows_per_page' => null));
print_r($response);
$total_rows = $response['return']['total_rows'];
$response = $cartodb->getRecords($tableName, array('rows_per_page' => $total_rows));
print_r($response);
Expand Down

0 comments on commit ac0b770

Please sign in to comment.