3ef0e517521c9e9c0ab94ff390c86f0511bed76b.svn-base
19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!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 … <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">¶</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">'request'</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">'../loggly'</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">¶</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">"Bad Request"</span><span class="p">,</span>
<span class="mi">401</span><span class="o">:</span> <span class="s2">"Unauthorized"</span><span class="p">,</span>
<span class="mi">403</span><span class="o">:</span> <span class="s2">"Forbidden"</span><span class="p">,</span>
<span class="mi">404</span><span class="o">:</span> <span class="s2">"Not Found"</span><span class="p">,</span>
<span class="mi">409</span><span class="o">:</span> <span class="s2">"Conflict / Duplicate"</span><span class="p">,</span>
<span class="mi">410</span><span class="o">:</span> <span class="s2">"Gone"</span><span class="p">,</span>
<span class="mi">500</span><span class="o">:</span> <span class="s2">"Internal Server Error"</span><span class="p">,</span>
<span class="mi">501</span><span class="o">:</span> <span class="s2">"Not Implemented"</span><span class="p">,</span>
<span class="mi">503</span><span class="o">:</span> <span class="s2">"Throttled"</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">¶</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">"OK"</span><span class="p">,</span>
<span class="mi">201</span><span class="o">:</span> <span class="s2">"Created"</span><span class="p">,</span>
<span class="mi">202</span><span class="o">:</span> <span class="s2">"Accepted"</span><span class="p">,</span>
<span class="mi">203</span><span class="o">:</span> <span class="s2">"Non-authoritative information"</span><span class="p">,</span>
<span class="mi">204</span><span class="o">:</span> <span class="s2">"Deleted"</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">¶</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 & 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">¶</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">'string'</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">¶</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">'GET'</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">'method'</span><span class="p">]</span> <span class="o">||</span> <span class="s1">'GET'</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">'uri'</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">'body'</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">'auth'</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">'headers'</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">'GET'</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">'authorization'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'Basic '</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">':'</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">'base64'</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">'Loggly Error ('</span> <span class="o">+</span> <span class="nx">statusCode</span> <span class="o">+</span> <span class="s1">'): '</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">¶</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>