Zend_Service_Nirvanix.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.service.nirvanix">
  4. <title>Zend_Service_Nirvanix</title>
  5. <sect2 id="zend.service.nirvanix.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. Nirvanix provides an Internet Media File System (IMFS), an
  9. Internet storage service that allows applications to upload, store and
  10. organize files and subsequently access them using a standard Web
  11. Services interface. An IMFS is distributed clustered file system,
  12. accessed over the Internet, and optimized for dealing with media files
  13. (audio, video, etc). The goal of an IMFS is to provide massive
  14. scalability to deal with the challenges of media storage growth, with
  15. guaranteed access and availability regardless of time and location.
  16. Finally, an IMFS gives applications the ability to access data
  17. securely, without the large fixed costs associated with acquiring and
  18. maintaining physical storage assets.
  19. </para>
  20. </sect2>
  21. <sect2 id="zend.service.nirvanix.registering">
  22. <title>Registering with Nirvanix</title>
  23. <para>
  24. Before you can get started with <classname>Zend_Service_Nirvanix</classname>, you must first
  25. register for an account. Please see the
  26. <ulink url="http://www.nirvanix.com/gettingStarted.aspx">Getting Started</ulink>
  27. page on the Nirvanix website for more information.
  28. </para>
  29. <para>
  30. After registering, you will receive a Username, Password, and
  31. Application Key. All three are required to use <classname>Zend_Service_Nirvanix</classname>.
  32. </para>
  33. </sect2>
  34. <sect2 id="zend.service.nirvanix.apiDocumentation">
  35. <title>API Documentation</title>
  36. <para>
  37. Access to the Nirvanix IMFS is available through both SOAP and a faster
  38. REST service. <classname>Zend_Service_Nirvanix</classname> provides a
  39. relatively thin PHP 5 wrapper around the REST service.
  40. </para>
  41. <para>
  42. <classname>Zend_Service_Nirvanix</classname> aims to make using the Nirvanix REST service
  43. easier but understanding the service itself is still essential to be successful
  44. with Nirvanix.
  45. </para>
  46. <para>
  47. The <ulink url="http://developer.nirvanix.com/sitefiles/1000/API.html">Nirvanix API Documentation</ulink>
  48. provides an overview as well as detailed information using the
  49. service. Please familiarize yourself with this document and refer back to it
  50. as you use <classname>Zend_Service_Nirvanix</classname>.
  51. </para>
  52. </sect2>
  53. <sect2 id="zend.service.nirvanix.features">
  54. <title>Features</title>
  55. <para>
  56. Nirvanix's REST service can be used effectively with PHP using the
  57. <ulink url="http://www.php.net/simplexml">SimpleXML</ulink>
  58. extension and <classname>Zend_Http_Client</classname> alone. However, using it this way
  59. is somewhat inconvenient due to repetitive operations like passing the
  60. session token on every request and repeatedly checking the response body for
  61. error codes.
  62. </para>
  63. <para>
  64. <classname>Zend_Service_Nirvanix</classname> provides the following functionality:
  65. <itemizedlist>
  66. <listitem>
  67. <para>
  68. A single point for configuring your Nirvanix authentication
  69. credentials that can be used across the Nirvanix namespaces.
  70. </para>
  71. </listitem>
  72. <listitem>
  73. <para>
  74. A proxy object that is more convenient to use than an HTTP client
  75. alone, mostly removing the need to manually construct HTTP POST
  76. requests to access the REST service.
  77. </para>
  78. </listitem>
  79. <listitem>
  80. <para>
  81. A response wrapper that parses each response body and throws an
  82. exception if an error occurred, alleviating the need to repeatedly
  83. check the success of many commands.
  84. </para>
  85. </listitem>
  86. <listitem>
  87. <para>
  88. Additional convenience methods for some of the more common operations.
  89. </para>
  90. </listitem>
  91. </itemizedlist>
  92. </para>
  93. </sect2>
  94. <sect2 id="zend.service.nirvanix.storing-your-first">
  95. <title>Getting Started</title>
  96. <para>
  97. Once you have registered with Nirvanix, you're ready to store your first
  98. file on the IMFS. The most common operations that you will need to do
  99. on the IMFS are creating a new file, downloading an existing file, and
  100. deleting a file. <classname>Zend_Service_Nirvanix</classname> provides convenience
  101. methods for these three operations.
  102. </para>
  103. <programlisting language="php"><![CDATA[
  104. $auth = array('username' => 'your-username',
  105. 'password' => 'your-password',
  106. 'appKey' => 'your-app-key');
  107. $nirvanix = new Zend_Service_Nirvanix($auth);
  108. $imfs = $nirvanix->getService('IMFS');
  109. $imfs->putContents('/foo.txt', 'contents to store');
  110. echo $imfs->getContents('/foo.txt');
  111. $imfs->unlink('/foo.txt');
  112. ]]></programlisting>
  113. <para>
  114. The first step to using <classname>Zend_Service_Nirvanix</classname> is always
  115. to authenticate against the service. This is done by passing your
  116. credentials to the <classname>Zend_Service_Nirvanix</classname> constructor
  117. above. The associative array is passed directly to Nirvanix as POST
  118. parameters.
  119. </para>
  120. <para>
  121. Nirvanix divides its web services into
  122. <ulink url="http://developer.nirvanix.com/sitefiles/1000/API.html#_Toc175999879">namespaces</ulink>.
  123. Each namespace encapsulates a group of related operations. After getting
  124. an instance of <classname>Zend_Service_Nirvanix</classname>, call the
  125. <code>getService()</code> method to create a proxy for the namespace
  126. you want to use. Above, a proxy for the <code>IMFS</code> namespace is created.
  127. </para>
  128. <para>
  129. After you have a proxy for the namespace you want to use, call methods on it.
  130. The proxy will allow you to use any command available on the REST API. The
  131. proxy may also make convenience methods available, which wrap web service
  132. commands. The example above shows using the IMFS convenience methods to create a
  133. new file, retrieve and display that file, and finally delete the file.
  134. </para>
  135. </sect2>
  136. <sect2 id="zend.service.nirvanix.understanding-proxy">
  137. <title>Understanding the Proxy</title>
  138. <para>
  139. In the previous example, we used the <code>getService()</code> method to return
  140. a proxy object to the <code>IMFS</code> namespace. The proxy object allows
  141. you to use the Nirvanix REST service in a way that's closer to making a normal
  142. PHP method call, as opposed to constructing your own HTTP request objects.
  143. </para>
  144. <para>
  145. A proxy object may provide convenience methods. These are methods that the
  146. <classname>Zend_Service_Nirvanix</classname> provides to simplify the use of
  147. the Nirvanix web services. In the previous example, the methods
  148. <code>putContents()</code>, <code>getContents()</code>, and <code>unlink()</code>
  149. do not have direct equivalents in the REST API. They are convenience methods
  150. provided by <classname>Zend_Service_Nirvanix</classname> that abstract more complicated
  151. operations on the REST API.
  152. </para>
  153. <para>
  154. For all other method calls to the proxy object, the proxy will dynamically
  155. convert the method call to the equivalent HTTP POST request to the REST API.
  156. It does this by using the method name as the API command, and an associative
  157. array in the first argument as the POST parameters.
  158. </para>
  159. <para>
  160. Let's say you want to call the REST API method
  161. <ulink url="http://developer.nirvanix.com/sitefiles/1000/API.html#_Toc175999923">RenameFile</ulink>,
  162. which does not have a convenience method in <classname>Zend_Service_Nirvanix</classname>:
  163. </para>
  164. <programlisting language="php"><![CDATA[
  165. $auth = array('username' => 'your-username',
  166. 'password' => 'your-password',
  167. 'appKey' => 'your-app-key');
  168. $nirvanix = new Zend_Service_Nirvanix($auth);
  169. $imfs = $nirvanix->getService('IMFS');
  170. $result = $imfs->renameFile(array('filePath' => '/path/to/foo.txt',
  171. 'newFileName' => 'bar.txt'));
  172. ]]></programlisting>
  173. <para>
  174. Above, a proxy for the <code>IMFS</code> namespace is created. A method,
  175. <code>renameFile()</code>, is then called on the proxy. This method does not
  176. exist as a convenience method in the PHP code, so it is trapped by
  177. <code>__call()</code> and converted into a POST request to the REST API where
  178. the associative array is used as the POST parameters.
  179. </para>
  180. <para>
  181. Notice in the Nirvanix API documentation that <code>sessionToken</code> is required
  182. for this method but we did not give it to the proxy object. It is added
  183. automatically for your convenience.
  184. </para>
  185. <para>
  186. The result of this operation will either be a <classname>Zend_Service_Nirvanix_Response</classname>
  187. object wrapping the XML returned by Nirvanix, or a <classname>Zend_Service_Nirvanix_Exception</classname>
  188. if an error occurred.
  189. </para>
  190. </sect2>
  191. <sect2 id="zend.service.nirvanix.examining-results">
  192. <title>Examining Results</title>
  193. <para>
  194. The Nirvanix REST API always returns its results in XML. <classname>Zend_Service_Nirvanix</classname>
  195. parses this XML with the <code>SimpleXML</code> extension and then decorates
  196. the resulting <code>SimpleXMLElement</code> with a <classname>Zend_Service_Nirvanix_Response</classname>
  197. object.
  198. </para>
  199. <para>
  200. The simplest way to examine a result from the service is to use the
  201. built-in PHP functions like <code>print_r()</code>:
  202. </para>
  203. <programlisting language="php"><![CDATA[
  204. <?php
  205. $auth = array('username' => 'your-username',
  206. 'password' => 'your-password',
  207. 'appKey' => 'your-app-key');
  208. $nirvanix = new Zend_Service_Nirvanix($auth);
  209. $imfs = $nirvanix->getService('IMFS');
  210. $result = $imfs->putContents('/foo.txt', 'fourteen bytes');
  211. print_r($result);
  212. ?>
  213. Zend_Service_Nirvanix_Response Object
  214. (
  215. [_sxml:protected] => SimpleXMLElement Object
  216. (
  217. [ResponseCode] => 0
  218. [FilesUploaded] => 1
  219. [BytesUploaded] => 14
  220. )
  221. )
  222. ]]></programlisting>
  223. <para>
  224. You can access any property or method of the decorated <code>SimpleXMLElement</code>.
  225. In the above example, <code>$result->BytesUploaded</code> could be used to see the
  226. number of bytes received. Should you want to access the <code>SimpleXMLElement</code>
  227. directly, just use <code>$result->getSxml()</code>.
  228. </para>
  229. <para>
  230. The most common response from Nirvanix is success (<code>ResponseCode</code> of zero).
  231. It is not normally necessary to check <code>ResponseCode</code> because any non-zero
  232. result will throw a <classname>Zend_Service_Nirvanix_Exception</classname>. See the next
  233. section on handling errors.
  234. </para>
  235. </sect2>
  236. <sect2 id="zend.service.nirvanix.handling-errors">
  237. <title>Handling Errors</title>
  238. <para>
  239. When using Nirvanix, it's important to anticipate errors that can be returned
  240. by the service and handle them appropriately.
  241. </para>
  242. <para>
  243. All operations against the REST service result in an XML return payload that
  244. contains a <code>ResponseCode</code> element, such as the following example:
  245. </para>
  246. <programlisting language="xml"><![CDATA[
  247. <Response>
  248. <ResponseCode>0</ResponseCode>
  249. </Response>
  250. ]]></programlisting>
  251. <para>
  252. When the <code>ResponseCode</code> is zero such as in the example
  253. above, the operation was successful. When the operation is not
  254. successful, the <code>ResponseCode</code> is non-zero and an
  255. <code>ErrorMessage</code> element should be present.
  256. </para>
  257. <para>
  258. To alleviate the need to repeatedly check if the <code>ResponseCode</code>
  259. is non-zero, <classname>Zend_Service_Nirvanix</classname> automatically checks each response
  260. returned by Nirvanix. If the <code>ResponseCode</code> indicates an
  261. error, a <classname>Zend_Service_Nirvanix_Exception</classname> will be thrown.
  262. </para>
  263. <programlisting language="xml"><![CDATA[
  264. $auth = array('username' => 'your-username',
  265. 'password' => 'your-password',
  266. 'appKey' => 'your-app-key');
  267. $nirvanix = new Zend_Service_Nirvanix($auth);
  268. try {
  269. $imfs = $nirvanix->getService('IMFS');
  270. $imfs->unlink('/a-nonexistant-path');
  271. } catch (Zend_Service_Nirvanix_Exception $e) {
  272. echo $e->getMessage() . "\n";
  273. echo $e->getCode();
  274. }
  275. ]]></programlisting>
  276. <para>
  277. In the example above, <code>unlink()</code> is a convenience method that wraps the
  278. <code>DeleteFiles</code> command on the REST API. The <code>filePath</code> parameter required by the
  279. <ulink url="http://developer.nirvanix.com/sitefiles/1000/API.html#_Toc175999918">DeleteFiles</ulink>
  280. command contains a path that does not exist. This will result in a
  281. <classname>Zend_Service_Nirvanix</classname> exception being thrown with the message
  282. "Invalid path" and code 70005.
  283. </para>
  284. <para>
  285. The <ulink url="http://developer.nirvanix.com/sitefiles/1000/API.html">Nirvanix
  286. API Documentation</ulink> describes the errors associated with each command. Depending
  287. on your needs, you may wrap each command in a <code>try</code> block or
  288. wrap many commands in the same <code>try</code> block for convenience.
  289. </para>
  290. </sect2>
  291. </sect1>