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) {
// 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');
print "That's too long. Max notice size is 255 chars.\n";
exit();

View File

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

View File

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

View File

@ -80,8 +80,7 @@ class TwitapistatusesAction extends TwitterapiAction {
function show_xml_timeline($notice) {
header('Content-Type: application/xml; charset=utf-8');
common_start_xml();
$this->init_document('xml');
common_element_start('statuses', array('type' => 'array'));
if (is_array($notice)) {
@ -97,14 +96,12 @@ class TwitapistatusesAction extends TwitterapiAction {
}
common_element_end('statuses');
common_end_xml();
$this->end_document('xml');
}
function show_rss_timeline($notice, $title, $id, $link, $subtitle) {
header("Content-Type: application/rss+xml; charset=utf-8");
$this->init_twitter_rss();
$this->init_document('rss');
common_element_start('channel');
common_element('title', NULL, $title);
@ -132,9 +129,7 @@ class TwitapistatusesAction extends TwitterapiAction {
function show_atom_timeline($notice, $title, $id, $link, $subtitle=NULL) {
header('Content-Type: application/atom+xml; charset=utf-8');
$this->init_twitter_atom();
$this->init_document('atom');
common_element('title', NULL, $title);
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) {
header('Content-Type: application/json; charset=utf-8');
$this->init_document('json');
$statuses = array();
@ -175,6 +171,8 @@ class TwitapistatusesAction extends TwitterapiAction {
}
$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
*/
function replies($args, $apidata) {
parent::handle($args);
$since = $this->arg('since');

View File

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