1
0

Made the Gallery3::load() and create() functions a little more robust

Added example for REST comments.
This commit is contained in:
Bharat Mediratta 2010-06-20 09:54:22 -07:00
parent 2b75fef32f
commit 033e35d07e
2 changed files with 14 additions and 2 deletions

View File

@ -111,6 +111,10 @@ class Gallery3 {
* @return object Gallery3 * @return object Gallery3
*/ */
public function create($url, $token) { public function create($url, $token) {
if (!is_string($url)) {
throw new Gallery3_Exception("Invalid url: " . var_export($url));
}
$response = Gallery3_Helper::request( $response = Gallery3_Helper::request(
"post", $url, $token, array("entity" => $this->data->entity), $this->file); "post", $url, $token, array("entity" => $this->data->entity), $this->file);
$this->url = $response->url; $this->url = $response->url;
@ -153,7 +157,7 @@ class Gallery3 {
public function load() { public function load() {
$response = Gallery3_Helper::request("get", $this->url, $this->token); $response = Gallery3_Helper::request("get", $this->url, $this->token);
$this->data = $response; $this->data = $response;
$this->original_entity = (array)$response->entity; $this->original_entity = isset($response->entity) ? (array)$response->entity : null;
return $this; return $this;
} }
} }

View File

@ -13,6 +13,7 @@ alert("Connect to $SITE_URL");
$auth = Gallery3::login($SITE_URL, $USER, $PASSWORD); $auth = Gallery3::login($SITE_URL, $USER, $PASSWORD);
$root = Gallery3::factory("$SITE_URL/item/1", $auth); $root = Gallery3::factory("$SITE_URL/item/1", $auth);
$tags = Gallery3::factory("$SITE_URL/tags", $auth); $tags = Gallery3::factory("$SITE_URL/tags", $auth);
$comments = Gallery3::factory("$SITE_URL/comments", $auth);
$tag = Gallery3::factory() $tag = Gallery3::factory()
->set("name", "My Tag") ->set("name", "My Tag")
@ -45,6 +46,13 @@ for ($i = 0; $i < 2; $i++) {
$album->load(); $album->load();
alert("Album members: <b>" . join(", ", $album->data->members) . "</b>"); alert("Album members: <b>" . join(", ", $album->data->members) . "</b>");
$comment = Gallery3::factory()
->set("item", $album->data->members[0])
->set("type", "comment")
->set("text", "This is a random comment-- whee!")
->create($comments->url, $auth);
alert("Comment: <b>{$comment->url}</b>");
alert("Reorder the album"); alert("Reorder the album");
$album $album
->set_members(array($album->data->members[1], $album->data->members[0])) ->set_members(array($album->data->members[1], $album->data->members[0]))
@ -93,7 +101,7 @@ $tag->delete();
alert("Done!"); alert("Done!");
function alert($msg) { function alert($msg) {
print "$msg <br/>"; print "$msg <br/>\n";
flush(); flush();
} }
?> ?>