Zend_Service_Nirvanix.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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
  25. first 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 Application Key.
  31. 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 <acronym>SOAP</acronym> and a
  38. faster REST service. <classname>Zend_Service_Nirvanix</classname> provides a
  39. relatively thin <acronym>PHP</acronym> 5 wrapper around the REST service.
  40. </para>
  41. <para>
  42. <classname>Zend_Service_Nirvanix</classname> aims to make using the Nirvanix REST
  43. service 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
  48. <acronym>API</acronym> Documentation</ulink> provides an overview as well as detailed
  49. information using the service. Please familiarize yourself with this document and refer
  50. back to it 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 <acronym>PHP</acronym> 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
  75. <acronym>HTTP</acronym> client alone, mostly removing the need to manually
  76. construct <acronym>HTTP</acronym> POST 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 <ulink
  122. 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. <methodname>getService()</methodname> method to create a proxy for the namespace
  126. you want to use. Above, a proxy for the <constant>IMFS</constant> namespace is created.
  127. </para>
  128. <para>
  129. After you have a proxy for the namespace you want to use, call methods on it. The
  130. proxy will allow you to use any command available on the REST <acronym>API</acronym>.
  131. The 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 <methodname>getService()</methodname> method to
  140. return a proxy object to the <constant>IMFS</constant> namespace. The proxy object
  141. allows you to use the Nirvanix REST service in a way that's closer to making a normal
  142. <acronym>PHP</acronym> method call, as opposed to constructing your own
  143. <acronym>HTTP</acronym> request objects.
  144. </para>
  145. <para>
  146. A proxy object may provide convenience methods. These are methods that the
  147. <classname>Zend_Service_Nirvanix</classname> provides to simplify the use of
  148. the Nirvanix web services. In the previous example, the methods
  149. <methodname>putContents()</methodname>, <methodname>getContents()</methodname>, and
  150. <methodname>unlink()</methodname> do not have direct equivalents in the REST
  151. <acronym>API</acronym>. They are convenience methods provided by
  152. <classname>Zend_Service_Nirvanix</classname> that abstract more complicated operations
  153. on the REST <acronym>API</acronym>.
  154. </para>
  155. <para>
  156. For all other method calls to the proxy object, the proxy will dynamically convert the
  157. method call to the equivalent <acronym>HTTP</acronym> POST request to the REST
  158. <acronym>API</acronym>. It does this by using the method name as the
  159. <acronym>API</acronym> command, and an associative array in the first argument as the
  160. POST parameters.
  161. </para>
  162. <para>
  163. Let's say you want to call the REST <acronym>API</acronym> method <ulink
  164. url="http://developer.nirvanix.com/sitefiles/1000/API.html#_Toc175999923">RenameFile</ulink>,
  165. which does not have a convenience method in
  166. <classname>Zend_Service_Nirvanix</classname>:
  167. </para>
  168. <programlisting language="php"><![CDATA[
  169. $auth = array('username' => 'your-username',
  170. 'password' => 'your-password',
  171. 'appKey' => 'your-app-key');
  172. $nirvanix = new Zend_Service_Nirvanix($auth);
  173. $imfs = $nirvanix->getService('IMFS');
  174. $result = $imfs->renameFile(array('filePath' => '/path/to/foo.txt',
  175. 'newFileName' => 'bar.txt'));
  176. ]]></programlisting>
  177. <para>
  178. Above, a proxy for the <constant>IMFS</constant> namespace is created. A method,
  179. <methodname>renameFile()</methodname>, is then called on the proxy. This method does not
  180. exist as a convenience method in the <acronym>PHP</acronym> code, so it is trapped by
  181. <methodname>__call()</methodname> and converted into a POST request to the REST
  182. <acronym>API</acronym> where the associative array is used as the POST parameters.
  183. </para>
  184. <para>
  185. Notice in the Nirvanix <acronym>API</acronym> documentation that
  186. <code>sessionToken</code> is required for this method but we did not give it to the
  187. proxy object. It is added automatically for your convenience.
  188. </para>
  189. <para>
  190. The result of this operation will either be a
  191. <classname>Zend_Service_Nirvanix_Response</classname> object wrapping the
  192. <acronym>XML</acronym> returned by Nirvanix, or a
  193. <classname>Zend_Service_Nirvanix_Exception</classname> if an error occurred.
  194. </para>
  195. </sect2>
  196. <sect2 id="zend.service.nirvanix.examining-results">
  197. <title>Examining Results</title>
  198. <para>
  199. The Nirvanix REST <acronym>API</acronym> always returns its results in
  200. <acronym>XML</acronym>. <classname>Zend_Service_Nirvanix</classname> parses this
  201. <acronym>XML</acronym> with the <code>SimpleXML</code> extension and then decorates the
  202. resulting <code>SimpleXMLElement</code> with a
  203. <classname>Zend_Service_Nirvanix_Response</classname> object.
  204. </para>
  205. <para>
  206. The simplest way to examine a result from the service is to use the
  207. built-in <acronym>PHP</acronym> functions like <methodname>print_r()</methodname>:
  208. </para>
  209. <programlisting language="php"><![CDATA[
  210. <?php
  211. $auth = array('username' => 'your-username',
  212. 'password' => 'your-password',
  213. 'appKey' => 'your-app-key');
  214. $nirvanix = new Zend_Service_Nirvanix($auth);
  215. $imfs = $nirvanix->getService('IMFS');
  216. $result = $imfs->putContents('/foo.txt', 'fourteen bytes');
  217. print_r($result);
  218. ?>
  219. Zend_Service_Nirvanix_Response Object
  220. (
  221. [_sxml:protected] => SimpleXMLElement Object
  222. (
  223. [ResponseCode] => 0
  224. [FilesUploaded] => 1
  225. [BytesUploaded] => 14
  226. )
  227. )
  228. ]]></programlisting>
  229. <para>
  230. You can access any property or method of the decorated <code>SimpleXMLElement</code>.
  231. In the above example, <code>$result->BytesUploaded</code> could be used to see the
  232. number of bytes received. Should you want to access the <code>SimpleXMLElement</code>
  233. directly, just use <code>$result->getSxml()</code>.
  234. </para>
  235. <para>
  236. The most common response from Nirvanix is success (<code>ResponseCode</code> of zero).
  237. It is not normally necessary to check <code>ResponseCode</code> because any non-zero
  238. result will throw a <classname>Zend_Service_Nirvanix_Exception</classname>. See the next
  239. section on handling errors.
  240. </para>
  241. </sect2>
  242. <sect2 id="zend.service.nirvanix.handling-errors">
  243. <title>Handling Errors</title>
  244. <para>
  245. When using Nirvanix, it's important to anticipate errors that can be returned
  246. by the service and handle them appropriately.
  247. </para>
  248. <para>
  249. All operations against the REST service result in an <acronym>XML</acronym> return
  250. payload that contains a <code>ResponseCode</code> element, such as the following
  251. example:
  252. </para>
  253. <programlisting language="xml"><![CDATA[
  254. <Response>
  255. <ResponseCode>0</ResponseCode>
  256. </Response>
  257. ]]></programlisting>
  258. <para>
  259. When the <code>ResponseCode</code> is zero such as in the example
  260. above, the operation was successful. When the operation is not
  261. successful, the <code>ResponseCode</code> is non-zero and an
  262. <code>ErrorMessage</code> element should be present.
  263. </para>
  264. <para>
  265. To alleviate the need to repeatedly check if the <code>ResponseCode</code>
  266. is non-zero, <classname>Zend_Service_Nirvanix</classname> automatically checks each
  267. response returned by Nirvanix. If the <code>ResponseCode</code> indicates an
  268. error, a <classname>Zend_Service_Nirvanix_Exception</classname> will be thrown.
  269. </para>
  270. <programlisting language="xml"><![CDATA[
  271. $auth = array('username' => 'your-username',
  272. 'password' => 'your-password',
  273. 'appKey' => 'your-app-key');
  274. $nirvanix = new Zend_Service_Nirvanix($auth);
  275. try {
  276. $imfs = $nirvanix->getService('IMFS');
  277. $imfs->unlink('/a-nonexistant-path');
  278. } catch (Zend_Service_Nirvanix_Exception $e) {
  279. echo $e->getMessage() . "\n";
  280. echo $e->getCode();
  281. }
  282. ]]></programlisting>
  283. <para>
  284. In the example above, <methodname>unlink()</methodname> is a convenience method that
  285. wraps the <code>DeleteFiles</code> command on the REST <acronym>API</acronym>. The
  286. <code>filePath</code> parameter required by the <ulink
  287. url="http://developer.nirvanix.com/sitefiles/1000/API.html#_Toc175999918">DeleteFiles</ulink>
  288. command contains a path that does not exist. This will result in a
  289. <classname>Zend_Service_Nirvanix</classname> exception being thrown with the message
  290. "Invalid path" and code 70005.
  291. </para>
  292. <para>
  293. The <ulink url="http://developer.nirvanix.com/sitefiles/1000/API.html">Nirvanix
  294. <acronym>API</acronym> Documentation</ulink> describes the errors associated with each
  295. command. Depending on your needs, you may wrap each command in a <code>try</code> block
  296. or wrap many commands in the same <code>try</code> block for convenience.
  297. </para>
  298. </sect2>
  299. </sect1>