3ef0e517521c9e9c0ab94ff390c86f0511bed76b.svn-base 19.2 KB
<!DOCTYPE html>  <html> <head>   <title>common.js</title>   <meta http-equiv="content-type" content="text/html; charset=UTF-8">   <link rel="stylesheet" media="all" href="../docco.css" /> </head> <body>   <div id="container">     <div id="background"></div>            <div id="jump_to">         Jump To &hellip;         <div id="jump_wrapper">           <div id="jump_page">                                           <a class="source" href="..//loggly/common.html">                 loggly/common.html               </a>                                           <a class="source" href="..//loggly/config.html">                 loggly/config.html               </a>                                           <a class="source" href="..//loggly/core.html">                 loggly/core.html               </a>                                           <a class="source" href="..//loggly/device.html">                 loggly/device.html               </a>                                           <a class="source" href="..//loggly/facet.html">                 loggly/facet.html               </a>                                           <a class="source" href="..//loggly/input.html">                 loggly/input.html               </a>                                           <a class="source" href="..//loggly/search.html">                 loggly/search.html               </a>                                           <a class="source" href="..//loggly.html">                 loggly.html               </a>                        </div>         </div>       </div>          <table cellpadding="0" cellspacing="0">       <thead>         <tr>           <th class="docs">             <h1>               common.js             </h1>           </th>           <th class="code">           </th>         </tr>       </thead>       <tbody>                               <tr id="section-1">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-1">&#182;</a>               </div>                            </td>             <td class="code">               <div class="highlight"><pre><span class="cm">/*</span>
<span class="cm"> * common.js: Common utility functions for requesting against Loggly APIs</span>
<span class="cm"> *</span>
<span class="cm"> * (C) 2010 Nodejitsu Inc.</span>
<span class="cm"> * MIT LICENSE</span>
<span class="cm"> *</span>
<span class="cm"> */</span>

<span class="kd">var</span> <span class="nx">request</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;request&#39;</span><span class="p">),</span>
    <span class="nx">loggly</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;../loggly&#39;</span><span class="p">);</span>

<span class="kd">var</span> <span class="nx">common</span> <span class="o">=</span> <span class="nx">exports</span><span class="p">;</span></pre></div>             </td>           </tr>                               <tr id="section-2">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-2">&#182;</a>               </div>               <p>Failure HTTP Response codes based
off Loggly specification.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="kd">var</span> <span class="nx">failCodes</span> <span class="o">=</span> <span class="nx">common</span><span class="p">.</span><span class="nx">failCodes</span> <span class="o">=</span> <span class="p">{</span>
  <span class="mi">400</span><span class="o">:</span> <span class="s2">&quot;Bad Request&quot;</span><span class="p">,</span>
  <span class="mi">401</span><span class="o">:</span> <span class="s2">&quot;Unauthorized&quot;</span><span class="p">,</span>
  <span class="mi">403</span><span class="o">:</span> <span class="s2">&quot;Forbidden&quot;</span><span class="p">,</span>
  <span class="mi">404</span><span class="o">:</span> <span class="s2">&quot;Not Found&quot;</span><span class="p">,</span>
  <span class="mi">409</span><span class="o">:</span> <span class="s2">&quot;Conflict / Duplicate&quot;</span><span class="p">,</span>
  <span class="mi">410</span><span class="o">:</span> <span class="s2">&quot;Gone&quot;</span><span class="p">,</span>
  <span class="mi">500</span><span class="o">:</span> <span class="s2">&quot;Internal Server Error&quot;</span><span class="p">,</span>
  <span class="mi">501</span><span class="o">:</span> <span class="s2">&quot;Not Implemented&quot;</span><span class="p">,</span>
  <span class="mi">503</span><span class="o">:</span> <span class="s2">&quot;Throttled&quot;</span>
<span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-3">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-3">&#182;</a>               </div>               <p>Success HTTP Response codes based
off Loggly specification.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="kd">var</span> <span class="nx">successCodes</span> <span class="o">=</span> <span class="nx">common</span><span class="p">.</span><span class="nx">successCodes</span> <span class="o">=</span> <span class="p">{</span>
  <span class="mi">200</span><span class="o">:</span> <span class="s2">&quot;OK&quot;</span><span class="p">,</span>
  <span class="mi">201</span><span class="o">:</span> <span class="s2">&quot;Created&quot;</span><span class="p">,</span> 
  <span class="mi">202</span><span class="o">:</span> <span class="s2">&quot;Accepted&quot;</span><span class="p">,</span>
  <span class="mi">203</span><span class="o">:</span> <span class="s2">&quot;Non-authoritative information&quot;</span><span class="p">,</span>
  <span class="mi">204</span><span class="o">:</span> <span class="s2">&quot;Deleted&quot;</span><span class="p">,</span>
<span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-4">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-4">&#182;</a>               </div>               <p>Core method that actually sends requests to Loggly.
This method is designed to be flexible w.r.t. arguments 
and continuation passing given the wide range of different
requests required to fully implement the Loggly API.</p>

<p>Continuations: 
  1. 'callback': The callback passed into every node-loggly method
  2. 'success':  A callback that will only be called on successful requests.
                 This is used throughout node-loggly to conditionally
                 do post-request processing such as JSON parsing.</p>

<p>Possible Arguments (1 &amp; 2 are equivalent):
  1. common.loggly('some-fully-qualified-url', auth, callback, success)
  2. common.loggly('GET', 'some-fully-qualified-url', auth, callback, success)
  3. common.loggly('DELETE', 'some-fully-qualified-url', auth, callback, success)
  4. common.loggly({ method: 'POST', uri: 'some-url', body: { some: 'body'} }, callback, success)</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">common</span><span class="p">.</span><span class="nx">loggly</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
  <span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">),</span>
      <span class="nx">success</span> <span class="o">=</span> <span class="nx">args</span><span class="p">.</span><span class="nx">pop</span><span class="p">(),</span>
      <span class="nx">callback</span> <span class="o">=</span> <span class="nx">args</span><span class="p">.</span><span class="nx">pop</span><span class="p">(),</span>
      <span class="nx">requestBody</span><span class="p">,</span> 
      <span class="nx">headers</span><span class="p">,</span>
      <span class="nx">method</span><span class="p">,</span> 
      <span class="nx">auth</span><span class="p">,</span> 
      <span class="nx">uri</span><span class="p">;</span>
  </pre></div>             </td>           </tr>                               <tr id="section-5">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-5">&#182;</a>               </div>               <p>Now that we've popped off the two callbacks
We can make decisions about other arguments</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="k">if</span> <span class="p">(</span><span class="nx">args</span><span class="p">.</span><span class="nx">length</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">===</span> <span class="s1">&#39;string&#39;</span><span class="p">)</span> <span class="p">{</span></pre></div>             </td>           </tr>                               <tr id="section-6">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-6">&#182;</a>               </div>               <p>If we got a string assume that it's the URI </p>             </td>             <td class="code">               <div class="highlight"><pre>      <span class="nx">method</span> <span class="o">=</span> <span class="s1">&#39;GET&#39;</span><span class="p">;</span>
      <span class="nx">uri</span>    <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
    <span class="p">}</span>
    <span class="k">else</span> <span class="p">{</span>
      <span class="nx">method</span>      <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;method&#39;</span><span class="p">]</span> <span class="o">||</span> <span class="s1">&#39;GET&#39;</span><span class="p">,</span>
      <span class="nx">uri</span>         <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;uri&#39;</span><span class="p">];</span>
      <span class="nx">requestBody</span> <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;body&#39;</span><span class="p">];</span>
      <span class="nx">auth</span>        <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;auth&#39;</span><span class="p">];</span>
      <span class="nx">headers</span>     <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;headers&#39;</span><span class="p">];</span>
    <span class="p">}</span>
  <span class="p">}</span>
  <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">args</span><span class="p">.</span><span class="nx">length</span> <span class="o">==</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">method</span> <span class="o">=</span> <span class="s1">&#39;GET&#39;</span><span class="p">;</span>
    <span class="nx">uri</span>    <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
    <span class="nx">auth</span>   <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span>
  <span class="p">}</span>
  <span class="k">else</span> <span class="p">{</span>
    <span class="nx">method</span> <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
    <span class="nx">uri</span>    <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span>
    <span class="nx">auth</span>   <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="mi">2</span><span class="p">];</span>
  <span class="p">}</span>
  
  <span class="kd">function</span> <span class="nx">onError</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
      <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
    <span class="p">}</span>
  <span class="p">}</span>
  
  <span class="kd">var</span> <span class="nx">requestOptions</span> <span class="o">=</span> <span class="p">{</span>
    <span class="nx">uri</span><span class="o">:</span> <span class="nx">uri</span><span class="p">,</span>
    <span class="nx">method</span><span class="o">:</span> <span class="nx">method</span><span class="p">,</span>
    <span class="nx">headers</span><span class="o">:</span> <span class="nx">headers</span> <span class="o">||</span> <span class="p">{}</span>
  <span class="p">};</span>
  
  <span class="k">if</span> <span class="p">(</span><span class="nx">auth</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">requestOptions</span><span class="p">.</span><span class="nx">headers</span><span class="p">[</span><span class="s1">&#39;authorization&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;Basic &#39;</span> <span class="o">+</span> <span class="k">new</span> <span class="nx">Buffer</span><span class="p">(</span><span class="nx">auth</span><span class="p">.</span><span class="nx">username</span> <span class="o">+</span> <span class="s1">&#39;:&#39;</span> <span class="o">+</span> <span class="nx">auth</span><span class="p">.</span><span class="nx">password</span><span class="p">).</span><span class="nx">toString</span><span class="p">(</span><span class="s1">&#39;base64&#39;</span><span class="p">);</span>
  <span class="p">}</span>
  
  <span class="k">if</span> <span class="p">(</span><span class="nx">requestBody</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">requestOptions</span><span class="p">.</span><span class="nx">body</span> <span class="o">=</span> <span class="nx">requestBody</span><span class="p">;</span>
  <span class="p">}</span>
  
  <span class="k">try</span> <span class="p">{</span>
    <span class="nx">request</span><span class="p">(</span><span class="nx">requestOptions</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">res</span><span class="p">,</span> <span class="nx">body</span><span class="p">)</span> <span class="p">{</span>
      <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="nx">onError</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
      <span class="p">}</span>

      <span class="kd">var</span> <span class="nx">statusCode</span> <span class="o">=</span> <span class="nx">res</span><span class="p">.</span><span class="nx">statusCode</span><span class="p">.</span><span class="nx">toString</span><span class="p">();</span>
      <span class="k">if</span> <span class="p">(</span><span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">failCodes</span><span class="p">).</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">statusCode</span><span class="p">)</span> <span class="o">!==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="nx">onError</span><span class="p">((</span><span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">&#39;Loggly Error (&#39;</span> <span class="o">+</span> <span class="nx">statusCode</span> <span class="o">+</span> <span class="s1">&#39;): &#39;</span> <span class="o">+</span> <span class="nx">failCodes</span><span class="p">[</span><span class="nx">statusCode</span><span class="p">])));</span>
      <span class="p">}</span>

      <span class="nx">success</span><span class="p">(</span><span class="nx">res</span><span class="p">,</span> <span class="nx">body</span><span class="p">);</span>
    <span class="p">});</span>
  <span class="p">}</span>
  <span class="k">catch</span> <span class="p">(</span><span class="nx">ex</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">onError</span><span class="p">(</span><span class="nx">ex</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-7">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-7">&#182;</a>               </div>               <p>function clone (obj)
  Helper method for deep cloning pure JSON objects
  i.e. JSON objects that are either literals or objects (no Arrays, etc)</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">common</span><span class="p">.</span><span class="nx">clone</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
  <span class="kd">var</span> <span class="nx">clone</span> <span class="o">=</span> <span class="p">{};</span>
  <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">clone</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="k">instanceof</span> <span class="nb">Object</span> <span class="o">?</span> <span class="nx">common</span><span class="p">.</span><span class="nx">clone</span><span class="p">(</span><span class="nx">obj</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span> <span class="o">:</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span>
  <span class="p">}</span>

  <span class="k">return</span> <span class="nx">clone</span><span class="p">;</span>
<span class="p">};</span>

</pre></div>             </td>           </tr>                </tbody>     </table>   </div> </body> </html>