1
0

Refactored the API to deal with relationships and data in general more

cleanly.  Unfortunately it makes resource data access a lot more
unwieldy so I don't think we're done yet.
This commit is contained in:
Bharat Mediratta 2010-01-26 00:15:35 -08:00
parent db6b1f9042
commit 6a3851bae2
2 changed files with 105 additions and 154 deletions

View File

@ -25,14 +25,9 @@ class Gallery3 {
var $url; var $url;
var $token; var $token;
var $data; var $data;
var $changed_data;
var $file; var $file;
var $parent;
public function __construct() { protected $original_resource;
$this->data = new stdClass();
$this->changed_data = new stdClass();
}
/** /**
* Connect to a remote Gallery3 instance * Connect to a remote Gallery3 instance
@ -40,84 +35,52 @@ class Gallery3 {
* @param string Gallery 3 API url, eg http://example.com/gallery3/index.php/rest * @param string Gallery 3 API url, eg http://example.com/gallery3/index.php/rest
* @param string username * @param string username
* @param string password * @param string password
* @return object Gallery3 * @return string authentication token
*/ */
static function connect($url, $user, $pass) { static function login($url, $user, $pass) {
$response = Gallery3_Helper::request( $response = Gallery3_Helper::request(
"post", $url, null, array("user" => $user, "password" => $pass)); "post", $url, null, array("user" => $user, "password" => $pass));
return $response;
return self::factory($url, $response, null);
} }
/** /**
* Create a new Gallery3 instance associated with a remote resource * Construct a new Gallery3 instance associated with a remote resource
* @param string the url * @param string remote url
* @param string security token * @param string authentication token
* @param object parent object * @return object Gallery3
* @return object Gallery3
*/ */
static function factory($url, $token, $parent) { public function factory($url=null, $token=null) {
$resource = new Gallery3(); $obj = new Gallery3();
$resource->url = $url; $obj->token = $token;
$resource->token = $token; $obj->url = $url;
$resource->parent = $parent; if ($url && $token) {
return $resource; $obj->load();
}
/**
* Retrieve a remote resource, by url.
*
* @param string the path relative to the current resource
* @return object Gallery3
*/
public function get($relative_path, $params=array()) {
$query = "";
if ($params) {
foreach ($params as $key => $value) {
$query[] = rawurlencode($key) . "=" . rawurlencode($value);
}
if ($query) {
$query = "?" . join("&", $query);
}
} }
return $obj;
return self::factory("$this->url/$relative_path$query", $this->token, $this)->load();
} }
/** /**
* Set a value on the remote resource * Constructor.
*/
public function __construct() {
$this->data = new stdClass();
$this->token = null;
$this->url = null;
}
/**
* Set a value on the remote resource. You must call save for it to take effect.
* *
* @param string key * @param string key
* @param string value * @param string value
* @return object Gallery3 * @return object Gallery3
* @chainable
*/ */
public function set_value($key, $value) { public function set($key, $value) {
$this->changed_data->$key = $value; $this->data->resource->$key = $value;
return $this; return $this;
} }
/**
* Get a value from the remote resource.
*
* @param string $key
* @return string value
*/
public function __get($key) {
if (property_exists($this->changed_data, $key)) {
return $this->changed_data->$key;
}
return $this->data->$key;
}
/**
* Get the list of members from the remote resource
*
* @return array member urls
*/
public function members() {
return $this->members;
}
/** /**
* Attach a file to the remote resource. * Attach a file to the remote resource.
* *
@ -130,30 +93,30 @@ class Gallery3 {
} }
/** /**
* Add a new member to a collection. You must call save() for it to be created in the * Save any local changes made to this resource. If this is an existing resource, we'll return
* remote Gallery 3. * the resource itself. If we're creating a new resource, return the newly created resource.
* *
* @return object Gallery3 * @return object Gallery3
*/ */
public function add() { public function create($url, $token) {
return Gallery3::factory(null, $this->token, $this); $response = Gallery3_Helper::request(
"post", $url, $token, $this->data->resource, $this->file);
$this->url = $response->url;
$this->token = $token;
return $this->load();
} }
/** /**
* Save any local changes made to this resource. * Save any local changes made to this resource. If this is an existing resource, we'll return
* the resource itself. If we're creating a new resource, return the newly created resource.
* *
* @return object Gallery3 * @return object Gallery3
*/ */
public function save() { public function save() {
if ($this->url) { $response = Gallery3_Helper::request(
$response = Gallery3_Helper::request("put", $this->url, $this->token, $this->changed_data); "put", $this->url, $this->token,
} else { array_diff($this->original_resource, (array)$this->data->resource));
$response = Gallery3_Helper::request( return $this->load();
"post", $this->parent->url, $this->token, $this->changed_data, $this->file);
$this->parent->load();
}
return $this->load(!empty($response->url) ? $response->url : null);
} }
/** /**
@ -163,47 +126,21 @@ class Gallery3 {
*/ */
public function delete() { public function delete() {
Gallery3_Helper::request("delete", $this->url, $this->token); Gallery3_Helper::request("delete", $this->url, $this->token);
$this->reset(); $this->data = array();
} $this->url = null;
return $this;
/**
* Remove a member from the remote collection.
*
* @return object Gallery3
*/
public function remove($url) {
Gallery3_Helper::request("delete", $this->url, $this->token, array("url" => $url));
$this->load();
} }
/** /**
* Reload the resource from a given url. This is useful after the remote resource has been * Reload the resource from a given url. This is useful after the remote resource has been
* modified. * modified.
* *
* @param string optional url, only necessary if the url changes.
* @return object Gallery3 * @return object Gallery3
*/ */
protected function load($url=null) { public function load() {
if ($url) {
$this->url = $url;
}
$response = Gallery3_Helper::request("get", $this->url, $this->token); $response = Gallery3_Helper::request("get", $this->url, $this->token);
$this->data = $response;
$this->data = isset($response->resource) ? $response->resource : new stdClass(); $this->original_resource = (array)$response->resource;
$this->members = isset($response->members) ? $response->members : array();
$chis->changed_data = new stdClass();
return $this;
}
/**
* Reset all data for this reference, essentially disconnecting it from the remote resource.
*
* @return object Gallery3
*/
protected function reset() {
$this->data = array();
$this->changed_data = array();
$this->url = null;
return $this; return $this;
} }
} }

View File

@ -10,65 +10,79 @@ if (file_exists("local_config.php")) {
} }
alert("Connect to $SITE_URL"); alert("Connect to $SITE_URL");
$gallery3 = Gallery3::connect($SITE_URL, $USER, $PASSWORD); $auth = Gallery3::login($SITE_URL, $USER, $PASSWORD);
$root = $gallery3->get("gallery"); $root = Gallery3::factory("$SITE_URL/item/1", $auth);
$tags = $gallery3->get("tags"); $tags = Gallery3::factory("$SITE_URL/tags", $auth);
alert("Create a tag");
$tag = $tags->add() $tag = Gallery3::factory()
->set_value("name", "My Tag") ->set("name", "My Tag")
->create($tags->url, $auth);
alert("Created tag: <b>{$tag->url}</b>");
$album = Gallery3::factory()
->set("type", "album")
->set("name", "Sample Album")
->set("title", "This is my Sample Album")
->create($root->url, $auth);
alert("Created album: <b>{$album->url} {$album->data->resource->title}</b>");
alert("Modify the album");
$album
->set("title", "This is the new title")
->save(); ->save();
alert("New title: <b>{$album->data->resource->title}</b>");
alert("Create a new album");
$album = $root->add()
->set_value("type", "album")
->set_value("name", "Sample Album")
->set_value("title", "This is my Sample Album")
->save();
alert("Upload a photo"); $photo = Gallery3::factory()
$photo = $album->add() ->set("type", "photo")
->set_value("type", "photo") ->set("name", "Sample Photo.jpg")
->set_value("name", "Sample Photo.jpg") ->set("title", "Sample Photo")
->set_value("title", "Sample Photo")
->set_file("/tmp/foo.jpg") ->set_file("/tmp/foo.jpg")
->save(); ->create($album->url, $auth);
alert("Added: " . $album->members[0] . ""); alert("Uploaded photo: <b>{$photo->url}</b>");
alert("Album members: <b>" . join(", ", $album->data->members) . "</b>");
alert("Search for the photo"); alert("Search for the photo");
$photos = $root->get("", array("name" => "Sample")); $photos = Gallery3::factory($root->url, $auth)
alert("Found: {$photos->members[0]}"); ->set("name", "Sample")
->load();
alert("Found: {$photos->data->members[0]}");
alert("Grab a random photo"); alert("Grab a random photo");
$photos = $root->get("", array("random" => "true")); $photos = Gallery3::factory($root->url, $auth)
alert("Found: {$photos->members[0]}"); ->set("random", "true")
->load();
alert("Found: {$photos->data->members[0]}");
alert("Tag the album");
$tag->add()
->set_value("url", $album->url)
->save();
alert("Tag the photo"); alert("Tag the album (using the album's relationships: {$album->data->relationships->tags->url})");
$tag->add() $tag_relationship1 = Gallery3::factory()
->set_value("url", $photo->url) ->set("tag", $tag->url)
->save(); ->set("item", $root->url)
alert("Tagged items: " . join($tag->members, " ")); ->create($album->data->relationships->tags->url, $auth);
alert("Tag: {$tag_relationship1->url}");
alert("Tag the photo (using the tag's relationships: {$tag->data->relationships->items->url})");
$tag_relationship2 = Gallery3::factory()
->set("tag", $tag->url)
->set("item", $photo->url)
->create($tag->data->relationships->items->url, $auth);
alert("Tag: {$tag_relationship2->url}");
alert("Un-tag the photo"); alert("Un-tag the photo");
$tag->remove($photo->url); $tag_relationship2->delete();
alert("Tagged items: " . join($tag->members, " ")); $tag->load();
alert("1 remaining tag: <b>{$tag->data->relationships->items->members[0]}</b>");
alert("Find and modify the album");
$album = $root->get("Sample-Album")
->set_value("title", "This is my title")
->save();
alert("New title: $album->title");
// Now delete the album alert("Delete the album and tag");
alert("Delete the album");
$album->delete(); $album->delete();
// Delete the tag
$tag->delete(); $tag->delete();
alert("Done!"); alert("Done!");