Twitter-compatible API - refactoring and bug fixes

darcs-hash:20080720083428-ca946-c14a92345366f2105b3c452a3899714d89692daa.gz
This commit is contained in:
zach 2008-07-20 04:34:28 -04:00
parent 140689800b
commit 93ac0bcae3
5 changed files with 39 additions and 37 deletions

View File

@ -51,7 +51,7 @@ class TwitapiaccountAction extends TwitterapiAction {
if (!is_null($location) && strlen($location) > 255) { if (!is_null($location) && strlen($location) > 255) {
// XXX: Twitter just truncates and runs with it. // XXX: But Twitter just truncates and runs with it. -- Zach
header('HTTP/1.1 406 Not Acceptable'); header('HTTP/1.1 406 Not Acceptable');
print "That's too long. Max notice size is 255 chars.\n"; print "That's too long. Max notice size is 255 chars.\n";
exit(); exit();

View File

@ -33,7 +33,6 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if (!$other) { if (!$other) {
$this->client_error(_('Could not follow user: User not found.'), 403, $apidata['content-type']); $this->client_error(_('Could not follow user: User not found.'), 403, $apidata['content-type']);
exit(); exit();
return;
} }
$user = $apidata['user']; $user = $apidata['user'];
@ -41,7 +40,6 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if ($user->isSubscribed($other)) { if ($user->isSubscribed($other)) {
$this->client_error("Could not follow user: $other->nickname is already on your list.", 403, $apidata['content-type']); $this->client_error("Could not follow user: $other->nickname is already on your list.", 403, $apidata['content-type']);
exit(); exit();
return;
} }
$sub = new Subscription(); $sub = new Subscription();
@ -57,7 +55,6 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if (!$result) { if (!$result) {
$this->client_error("Could not follow user: $other->nickname.", 400, $apidata['content-type']); $this->client_error("Could not follow user: $other->nickname.", 400, $apidata['content-type']);
exit(); exit();
return;
} }
$sub->query('COMMIT'); $sub->query('COMMIT');
@ -66,7 +63,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
$type = $apidata['content-type']; $type = $apidata['content-type'];
$this->init_document($type); $this->init_document($type);
$this->show_profile($other); $this->show_profile($other, $type);
$this->end_document($type); $this->end_document($type);
exit(); exit();
} }
@ -107,7 +104,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
$type = $apidata['content-type']; $type = $apidata['content-type'];
$this->init_document($type); $this->init_document($type);
$this->show_profile($other); $this->show_profile($other, $type);
$this->end_document($type); $this->end_document($type);
exit(); exit();
} }
@ -135,10 +132,6 @@ class TwitapifriendshipsAction extends TwitterapiAction {
$user_a = $this->get_profile($user_a_id); $user_a = $this->get_profile($user_a_id);
$user_b = $this->get_profile($user_b_id); $user_b = $this->get_profile($user_b_id);
if($user_a) { print "got user a profile";}
if($user_b) { print "got user b profile";}
if (!$user_a || !$user_b) { if (!$user_a || !$user_b) {
$this->client_error(_('Two user ids or screen_names must be supplied.'), 400, $apidata['content-type']); $this->client_error(_('Two user ids or screen_names must be supplied.'), 400, $apidata['content-type']);
exit(); exit();
@ -152,15 +145,17 @@ class TwitapifriendshipsAction extends TwitterapiAction {
switch ($apidata['content-type']) { switch ($apidata['content-type']) {
case 'xml': case 'xml':
common_start_xml(); $this->init_document('xml');
common_element('friends', NULL, $result); common_element('friends', NULL, $result);
common_end_xml(); $this->end_document('xml');
break; break;
case 'json': case 'json':
$this->init_document('json');
print json_encode($result); print json_encode($result);
$this->end_document('json');
break; break;
default: default:
print $result; print $result; // Really? --Zach
break; break;
} }

View File

@ -30,13 +30,15 @@ class TwitapihelpAction extends TwitterapiAction {
function test($args, $apidata) { function test($args, $apidata) {
global $xw; global $xw;
if ($apidata['content-type'] == 'xml') { if ($apidata['content-type'] == 'xml') {
header('Content-Type: application/xml; charset=utf-8'); $this->init_document('xml');
common_start_xml(); common_start_xml();
common_element('ok', NULL, 'true'); common_element('ok', NULL, 'true');
common_end_xml(); common_end_xml();
$this->end_document('xml');
} elseif ($apidata['content-type'] == 'json') { } elseif ($apidata['content-type'] == 'json') {
header('Content-Type: application/json; charset=utf-8'); $this->init_document('json');
print '"ok"'; print '"ok"';
$this->end_document('json');
} else { } else {
common_user_error("API method not found!", $code=404); common_user_error("API method not found!", $code=404);
} }

View File

@ -80,8 +80,7 @@ class TwitapistatusesAction extends TwitterapiAction {
function show_xml_timeline($notice) { function show_xml_timeline($notice) {
header('Content-Type: application/xml; charset=utf-8'); $this->init_document('xml');
common_start_xml();
common_element_start('statuses', array('type' => 'array')); common_element_start('statuses', array('type' => 'array'));
if (is_array($notice)) { if (is_array($notice)) {
@ -97,14 +96,12 @@ class TwitapistatusesAction extends TwitterapiAction {
} }
common_element_end('statuses'); common_element_end('statuses');
common_end_xml(); $this->end_document('xml');
} }
function show_rss_timeline($notice, $title, $id, $link, $subtitle) { function show_rss_timeline($notice, $title, $id, $link, $subtitle) {
header("Content-Type: application/rss+xml; charset=utf-8"); $this->init_document('rss');
$this->init_twitter_rss();
common_element_start('channel'); common_element_start('channel');
common_element('title', NULL, $title); common_element('title', NULL, $title);
@ -132,9 +129,7 @@ class TwitapistatusesAction extends TwitterapiAction {
function show_atom_timeline($notice, $title, $id, $link, $subtitle=NULL) { function show_atom_timeline($notice, $title, $id, $link, $subtitle=NULL) {
header('Content-Type: application/atom+xml; charset=utf-8'); $this->init_document('atom');
$this->init_twitter_atom();
common_element('title', NULL, $title); common_element('title', NULL, $title);
common_element('id', NULL, $id); common_element('id', NULL, $id);
@ -153,12 +148,13 @@ class TwitapistatusesAction extends TwitterapiAction {
} }
} }
$this->end_twitter_atom(); $this->end_document('atom');
} }
function show_json_timeline($notice) { function show_json_timeline($notice) {
header('Content-Type: application/json; charset=utf-8'); $this->init_document('json');
$statuses = array(); $statuses = array();
@ -175,6 +171,8 @@ class TwitapistatusesAction extends TwitterapiAction {
} }
$this->show_twitter_json_statuses($statuses); $this->show_twitter_json_statuses($statuses);
$this->end_document('json');
} }
/* /*
@ -449,6 +447,7 @@ class TwitapistatusesAction extends TwitterapiAction {
ID. Ex: http://server/api/statuses/replies.xml?since_id=12345 ID. Ex: http://server/api/statuses/replies.xml?since_id=12345
*/ */
function replies($args, $apidata) { function replies($args, $apidata) {
parent::handle($args); parent::handle($args);
$since = $this->arg('since'); $since = $this->arg('since');

View File

@ -183,18 +183,18 @@ class TwitterapiAction extends Action {
} }
function show_single_xml_status($notice) { function show_single_xml_status($notice) {
header('Content-Type: application/xml; charset=utf-8'); $this->init_document('xml');
common_start_xml();
$twitter_status = $this->twitter_status_array($notice); $twitter_status = $this->twitter_status_array($notice);
$this->show_twitter_xml_status($twitter_status); $this->show_twitter_xml_status($twitter_status);
common_end_xml(); $this->end_document('xml');
exit(); exit();
} }
function show_single_json_status($notice) { function show_single_json_status($notice) {
header('Content-Type: application/json; charset=utf-8'); $this->init_document('json');
$status = $this->twitter_status_array($notice); $status = $this->twitter_status_array($notice);
$this->show_twitter_json_statuses($status); $this->show_twitter_json_statuses($status);
$this->end_document('json');
exit(); exit();
} }
@ -260,6 +260,8 @@ class TwitterapiAction extends Action {
$this->client_error(_('Unsupported type')); $this->client_error(_('Unsupported type'));
break; break;
} }
return;
} }
function end_document($type='xml') { function end_document($type='xml') {
@ -279,6 +281,7 @@ class TwitterapiAction extends Action {
$this->client_error(_('Unsupported type')); $this->client_error(_('Unsupported type'));
break; break;
} }
return;
} }
function client_error($msg, $code = 400, $content_type = 'json') { function client_error($msg, $code = 400, $content_type = 'json') {
@ -314,15 +317,17 @@ class TwitterapiAction extends Action {
header('HTTP/1.1 '.$code.' '.$status_string); header('HTTP/1.1 '.$code.' '.$status_string);
if ($content_type == 'xml') { if ($content_type == 'xml') {
common_start_xml(); $this->init_document('xml');
common_element_start('hash'); common_element_start('hash');
common_element('error', NULL, $msg); common_element('error', NULL, $msg);
common_element('request', NULL, $_SERVER['REQUEST_URI']); common_element('request', NULL, $_SERVER['REQUEST_URI']);
common_element_end('hash'); common_element_end('hash');
common_end_xml(); $this->end_document('xml');
} else { } else {
$this->init_document('json');
$error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']); $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
print(json_encode($error_array)); print(json_encode($error_array));
$this->end_document('json');
} }
exit(); exit();
@ -361,5 +366,6 @@ class TwitterapiAction extends Action {
$this->client_error(_('not a supported data format')); $this->client_error(_('not a supported data format'));
return; return;
} }
return;
} }
} }