From 2f60a0ef113d6028aaaf65786c1c8adf3738491b Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 3 Jan 2010 17:03:33 -0800 Subject: [PATCH] First shot at a tight and intuitive client library for Gallery 3. --- client/Gallery3.php | 207 ++++++++++++++++++++++++++++++++++++++++++++ client/example.php | 37 ++++++++ 2 files changed, 244 insertions(+) create mode 100644 client/Gallery3.php create mode 100644 client/example.php diff --git a/client/Gallery3.php b/client/Gallery3.php new file mode 100644 index 00000000..e7b9337a --- /dev/null +++ b/client/Gallery3.php @@ -0,0 +1,207 @@ + $user, "password" => $pass)); + + return self::factory($url, $response->token, null); + } + + /** + * Create a new Gallery3 instance associated with a remote resource + * @param string the url + * @param string security token + * @param object parent object + * @return object Gallery3 + */ + static function factory($url, $token, $parent) { + $resource = new Gallery3(); + $resource->url = $url; + $resource->token = $token; + $resource->parent = $parent; + return $resource; + } + + /** + * Retrieve a remote resource, by url. + * + * @param string the path relative to the current resource + * @return object Gallery3 + */ + public function get($relative_path) { + return self::factory("$this->url/$relative_path", $this->token, $this)->load(); + } + + /** + * Set a value on the remote resource + * + * @param string key + * @param string value + * @return object Gallery3 + */ + public function set_value($key, $value) { + $this->data->$key = $value; + return $this; + } + + /** + * Attach a file to the remote resource. + * + * @param string path to a local file (eg: /tmp/foo.jpg) + * @return object Gallery3 + */ + public function set_file($file) { + $this->file = $file; + return $this; + } + + /** + * Create a new album. You must specify a name for the album, and call save() before the album + * will be created in the remote Gallery 3. + * + * @return object Gallery3 + */ + public function create_album() { + return Gallery3::factory(null, $this->token, $this) + ->set_value("type", "album"); + } + + /** + * Create a new photo. You must specify a name for the photo and use set_file() to attach an + * image file. Finally, you must call save() before the photo will be created in the remote + * Gallery 3. + * + * @return object Gallery3 + */ + public function create_photo() { + return Gallery3::factory(null, $this->token, $this) + ->set_value("type", "photo"); + } + + /** + * Save any local changes made to this resource. + * + * @return object Gallery3 + */ + public function save() { + if ($this->url) { + $response = Gallery3_Helper::request("put", $this->url, $this->token, $this->data); + } else { + $response = Gallery3_Helper::request( + "post", $this->parent->url, $this->token, $this->data, $this->file); + } + + return $this->load($response->url); + } + + /** + * Delete the remote resource. + * + * @return object Gallery3 + */ + public function delete() { + if (empty($this->url)) { + throw new Gallery3_Exception("Missing remote resource"); + } + + Gallery3_Helper::request("delete", $this->url, $this->token); + $this->reset(); + } + + /** + * Reload the resource from a given url. This is useful after the remote resource has been + * modified. + * + * @param string optional url, only necessary if the url changes. + * @return object Gallery3 + */ + protected function load($url=null) { + if ($url) { + $this->url = $url; + } + $this->data = Gallery3_Helper::request("get", $this->url, $this->token); + 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->url = null; + return $this; + } +} + +class Gallery3_Helper { + static function request($method, $url, $token=null, $params=array(), $file=null) { + $req = new HTTP_Request($url); + $req->setMethod($method == "get" ? HTTP_REQUEST_METHOD_GET : HTTP_REQUEST_METHOD_POST); + $req->addHeader("X-Gallery-Request-Method", $method); + if ($token) { + $req->addHeader("X-Gallery-Request-Key", $token); + } + foreach ($params as $key => $value) { + $req->addPostData($key, $value); + } + if ($file) { + $req->addFile("file", $file, mime_content_type($file)); + } + $req->sendRequest(); + + switch ($req->getResponseCode()) { + case 200: + return json_decode($req->getResponseBody()); + + case 403: + throw new Gallery3_Forbidden_Exception($req->getResponseBody()); + + default: + throw new Gallery3_Exception($req->getResponseBody()); + } + } +} + +class Gallery3_Exception extends Exception { +} + +class Gallery3_Forbidden_Exception extends Gallery3_Exception { +} \ No newline at end of file diff --git a/client/example.php b/client/example.php new file mode 100644 index 00000000..f22709bc --- /dev/null +++ b/client/example.php @@ -0,0 +1,37 @@ +get("gallery"); + +// Create a new album +$album = $root->create_album() + ->set_value("name", "Sample Album") + ->set_value("title", "This is my Sample Album") + ->save(); + +// Upload a new photo +$photo = $album->create_photo() + ->set_value("name", "Sample Photo") + ->set_value("title", "Sample Photo") + ->set_file("/tmp/foo.jpg") + ->save(); + +// Look up the album and modify it. +$album = $root->get("Sample-Album") + ->set_value("title", "This is my title") + ->save(); + +// Now delete the album +$album->delete(); +?> \ No newline at end of file