Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add access to pagination from last response #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 65 additions & 39 deletions disqusapi/disqusapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
*
* http://disqus.com/api/
*
* @author DISQUS <[email protected]>
* @copyright 2007-2010 Big Head Labs
* @link http://disqus.com/
* @package disqusapi
* @version 0.1.1
* @author DISQUS <[email protected]>
* @copyright 2007-2010 Big Head Labs
* @link http://disqus.com/
* @package disqusapi
* @version 0.1.1
*
* $disqus = new DisqusAPI($secret_key)
* $disqus->trends->listThreads()
Expand All @@ -23,31 +23,39 @@
require_once(dirname(__FILE__) . '/url.php');

if (!extension_loaded('json')) {
require_once(dirname(__FILE__) . '/json.php');
function dsq_json_decode($data) {
$json = new JSON;
return $json->unserialize($data);
}
require_once(dirname(__FILE__) . '/json.php');
function dsq_json_decode($data) {
$json = new JSON;
return $json->unserialize($data);
}
} else {
function dsq_json_decode($data) {
return json_decode($data);
}
function dsq_json_decode($data) {
return json_decode($data);
}
}

global $DISQUS_API_INTERFACES;

$DISQUS_API_INTERFACES = dsq_json_decode(file_get_contents(dirname(__FILE__) . '/interfaces.json'));

class DisqusInterfaceNotDefined extends Exception {}
class DisqusInterfaceNotDefined extends Exception {

}

class DisqusAPIError extends Exception {

public function __construct($code, $message) {
$this->code = $code;
$this->message = $message;
}
}

class DisqusResource {
public function __construct($api, $interface=null, $node=null, $tree=array()) {

// For access to cursor information from the last call.
public $cursor = NULL;

public function __construct($api, $interface = NULL, $node = NULL, $tree = []) {
global $DISQUS_API_INTERFACES;

if (!$interface) {
Expand All @@ -62,14 +70,6 @@ public function __construct($api, $interface=null, $node=null, $tree=array()) {
$this->tree = $tree;
}

public function __get($attr) {
$interface = $this->interface->$attr;
if (!$interface) {
throw new DisqusInterfaceNotDefined();
}
return new DisqusResource($this->api, $interface, $attr, $this->tree);
}

public function __call($name, $args) {
$resource = $this->interface->$name;
if (!$resource) {
Expand All @@ -79,7 +79,7 @@ public function __call($name, $args) {

foreach ((array)$resource->required as $k) {
if (empty($kwargs[$k])) {
throw new Exception('Missing required argument: '.$k);
throw new Exception('Missing required argument: ' . $k);
}
}

Expand All @@ -94,38 +94,48 @@ public function __call($name, $args) {
$format = (!empty($kwargs['format']) ? $kwargs['format'] : $api->format);
unset($kwargs['version'], $kwargs['format']);

$url = 'https://'.DISQUS_API_HOST;
$path = '/api/'.$version.'/'.implode('/', $this->tree).'/'.$name.'.'.$format;
$url = 'https://' . DISQUS_API_HOST;
$path = '/api/' . $version . '/' . implode('/', $this->tree) . '/' . $name . '.' . $format;

if (!empty($kwargs)) {
if ($resource->method == 'POST') {
$post_data = $kwargs;
} else {
$post_data = false;
$path .= '?'.dsq_get_query_string($kwargs);
$post_data = FALSE;
$path .= '?' . dsq_get_query_string($kwargs);
}
}


$response = dsq_urlopen($url.$path, $post_data);

$response = dsq_urlopen($url . $path, $post_data);
$data = call_user_func($api->formats[$format], $response['data']);

if ($response['code'] != 200) {
throw new DisqusAPIError($data->code, $data->response);
}

DisqusAPICursor::setCursor(isset($data->cursor) ? $data->cursor : NULL);

return $data->response;
}

public function __get($attr) {
$interface = $this->interface->$attr;
if (!$interface) {
throw new DisqusInterfaceNotDefined();
}
return new DisqusResource($this->api, $interface, $attr, $this->tree);
}
}


class DisqusAPI extends DisqusResource {
public $formats = array(
'json' => 'dsq_json_decode'
);

public function __construct($key=null, $format='json', $version='3.0') {
public $formats = [
'json' => 'dsq_json_decode',
];

public function __construct($key = NULL, $format = 'json', $version = '3.0') {
$this->key = $key;
$this->format = $format;
$this->version = $version;
Expand All @@ -136,15 +146,31 @@ public function __invoke() {
throw new Exception('You cannot call the API without a resource.');
}

public function setKey($key) {
$this->key = $key;
}

public function setFormat($format) {
$this->format = $format;
}

public function setKey($key) {
$this->key = $key;
}

public function setVersion($version) {
$this->version = $version;
}
}
}

class DisqusAPICursor {

private static $cursor = NULL;

private function __construct() {
}

public static function getCursor() {
return static::$cursor;
}

public static function setCursor($cursor) {
static::$cursor = $cursor;
}
}