-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
307 lines (256 loc) · 25.1 KB
/
index.html
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"0>
<title>tubes web library</title>
<link rel="stylesheet" href="css/code.css" type="text/css" media="screen" charset="utf-8"/>
<link rel="stylesheet" href="css/master.css" type="text/css" media="screen" charset="utf-8"/>
<script type="text/javascript" src="js/jquery-1.3.2.js" charset="utf-8"></script>
</head>
<body>
<div id="outer">
<div id="header">
<h1 id="heading">tubes!</h1>
<p>a series of tubes...</p>
</div>
<div id="main">
<div class="question">
<h2>what is it?</h2>
<p>
it's a small library on top of
<a href="http://werkzeug.pocoo.org/">werkzeug</a>
to create
<a href="http://en.wikipedia.org/wiki/REST">REST</a>
APIs (and websites) using
<a href="http://en.wikipedia.org/wiki/JSON">JSON</a>
</p>
</div>
<div class="question">
<h2>why?</h2>
<p>because I make websites and I don't like big frameworks, and I don't like the webpy way of doing things</p>
</div>
<div class="question">
<h2>no, really, why??</h2>
<p>because I'm tired of coding the same stuff all over again, so I decided to put them together for great justice</p>
</div>
<div class="question">
<h2>why the name?</h2>
<p>because as everyone knows, <a href="http://www.youtube.com/watch?v=f99PcP0aFNE">internet is a series of tubes</a></p>
</div>
<div class="question">
<h2>where is the code?</h2>
<p>the code is hosted at <a href="http://github.com/marianoguerra/tubes">github</a>, you can submit bugs or send me messages there</p>
</div>
<div class="question">
<h2>features?</h2>
<p>
in no particular order
<ul>
<li><em>simple</em> sintax using decorators to map an url to a method</li>
<li>match urls with regular expression</li>
<li>automatic marshalling and unmarshalling of objects</li>
<li>based on <a href="http://werkzeug.pocoo.org/">werkzeug</a> (which is <em>awesome</em>)</li>
<li>dispatch based on content type</li>
<li>based on <a href="http://wsgi.org/wsgi/What_is_WSGI">wsgi</a></li>
<li>google appengine ready</li>
<li><em>choose</em> your template engine and ORM</li>
<li>automatic generation of code for requests, classes and API tests</li>
</ul>
</p>
</div>
<div class="question">
<h2>examples?</h2>
<h3>hello world</h3>
<div class="highlight"><pre><span class="k">import</span> <span class="nn">tubes</span>
<span class="n">handler</span> <span class="o">=</span> <span class="n">tubes</span><span class="o">.</span><span class="n">Handler</span><span class="p">()</span>
<span class="nd">@handler</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">"^.*$"</span><span class="p">,</span> <span class="n">produces</span><span class="o">=</span><span class="n">tubes</span><span class="o">.</span><span class="n">HTML</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">hello_world</span><span class="p">(</span><span class="n">handler</span><span class="p">):</span>
<span class="k">return</span> <span class="s">"hello world!"</span>
<span class="n">tubes</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">handler</span><span class="p">)</span>
</pre></div>
<h4>run it</h4>
<pre>
$ python hello_world.py
* Running on http://0.0.0.0:8000/
</pre>
<h4>test it</h4>
<p>point your browser to that address, you can add anything after the url, that handler will match any url</p>
</div>
<a href="#" onclick="$(this).fadeOut();$('#moreexamples').fadeIn();return false;">more!</a>
<div class="more" id="moreexamples">
<h3>hello name</h3>
<div class="highlight"><pre><span class="k">import</span> <span class="nn">tubes</span>
<span class="n">handler</span> <span class="o">=</span> <span class="n">tubes</span><span class="o">.</span><span class="n">Handler</span><span class="p">()</span>
<span class="nd">@handler</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">"^/?$"</span><span class="p">,</span> <span class="n">produces</span><span class="o">=</span><span class="n">tubes</span><span class="o">.</span><span class="n">HTML</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">hello_world</span><span class="p">(</span><span class="n">handler</span><span class="p">):</span>
<span class="k">return</span> <span class="s">"hello world!"</span>
<span class="c"># match one or more characters of any type, optionally an ending slash</span>
<span class="nd">@handler</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">"^/(.+?)/?$"</span><span class="p">,</span> <span class="n">produces</span><span class="o">=</span><span class="n">tubes</span><span class="o">.</span><span class="n">HTML</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">hello_name</span><span class="p">(</span><span class="n">handler</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
<span class="k">return</span> <span class="s">"hello "</span> <span class="o">+</span> <span class="n">name</span>
<span class="n">tubes</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">handler</span><span class="p">)</span>
</pre></div>
<h4>run it</h4>
<pre>
# same as before, just change the file name in the following examples
$ python hello_name.py
* Running on http://0.0.0.0:8000/
</pre>
<h4>test it</h4>
<p>
point your browser to that address, you will se a hello world, now go to
<a href="http://0.0.0.0:8000/yourname">http://0.0.0.0:8000/yourname</a> you
should see "hello yourname". you can change <em>yourname</em> for any
string or path
</p>
<h3>responses</h3>
<div class="highlight"><pre><span class="k">import</span> <span class="nn">tubes</span>
<span class="n">handler</span> <span class="o">=</span> <span class="n">tubes</span><span class="o">.</span><span class="n">Handler</span><span class="p">()</span>
<span class="nd">@handler</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">"^/?$"</span><span class="p">,</span> <span class="n">produces</span><span class="o">=</span><span class="n">tubes</span><span class="o">.</span><span class="n">HTML</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">hello_world</span><span class="p">(</span><span class="n">handler</span><span class="p">):</span>
<span class="k">return</span> <span class="s">"hello world!"</span>
<span class="nd">@handler</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">"^/fail/?$"</span><span class="p">,</span> <span class="n">produces</span><span class="o">=</span><span class="n">tubes</span><span class="o">.</span><span class="n">HTML</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">fail</span><span class="p">(</span><span class="n">handler</span><span class="p">):</span>
<span class="k">return</span> <span class="n">tubes</span><span class="o">.</span><span class="n">Response</span><span class="p">(</span><span class="s">"introductory example fail"</span><span class="p">,</span> <span class="mf">500</span><span class="p">)</span>
<span class="nd">@handler</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">"^/not-here/?$"</span><span class="p">,</span> <span class="n">produces</span><span class="o">=</span><span class="n">tubes</span><span class="o">.</span><span class="n">HTML</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">not_here</span><span class="p">(</span><span class="n">handler</span><span class="p">):</span>
<span class="k">return</span> <span class="n">tubes</span><span class="o">.</span><span class="n">Response</span><span class="p">(</span><span class="s">"nothing to see here, please move along"</span><span class="p">,</span> <span class="mf">404</span><span class="p">)</span>
<span class="nd">@handler</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">"^/redirect/?$"</span><span class="p">,</span> <span class="n">produces</span><span class="o">=</span><span class="n">tubes</span><span class="o">.</span><span class="n">HTML</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">redirect</span><span class="p">(</span><span class="n">handler</span><span class="p">):</span>
<span class="k">return</span> <span class="n">tubes</span><span class="o">.</span><span class="n">redirect</span><span class="p">(</span><span class="s">"/"</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="mf">301</span><span class="p">)</span>
<span class="c"># you can make werkzeug reload the server everytime a change is made</span>
<span class="c"># also you can enable a debugger useful when developing to inspect exceptions</span>
<span class="c"># on the browser</span>
<span class="n">tubes</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">handler</span><span class="p">,</span> <span class="n">use_reloader</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">use_debugger</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
<h4>test it</h4>
<p>
point your browser to that the following addresses
<a href="http://0.0.0.0:8000/">hello world</a>,
<a href="http://0.0.0.0:8000/fail">fail (500 error)</a>,
<a href="http://0.0.0.0:8000/not-here">not found (404 error)</a>,
<a href="http://0.0.0.0:8000/redirect">redirect (to hello world)</a>
</p>
<h3>advanced</h3>
<p>
<div class="highlight"><pre><span class="k">import</span> <span class="nn">re</span>
<span class="k">import</span> <span class="nn">tubes</span>
<span class="c"># this module generates code for us (you can ignore it if you want)</span>
<span class="k">import</span> <span class="nn">intertubes</span>
<span class="c"># decorator to allow python<->json<->javascript</span>
<span class="nd">@tubes</span><span class="o">.</span><span class="n">JsonClass</span><span class="p">()</span>
<span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="sd">"""a class that represents an user"""</span>
<span class="n">USER_FORMAT</span> <span class="o">=</span> <span class="s">'[a-z][a-zA-Z0-9]*'</span>
<span class="n">ALL_USER_FORMAT</span> <span class="o">=</span> <span class="s">'^'</span> <span class="o">+</span> <span class="n">USER_FORMAT</span> <span class="o">+</span> <span class="s">'$'</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">user</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">mail</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">firstname</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
<span class="n">lastname</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">website</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""constructor"""</span>
<span class="bp">self</span><span class="o">.</span><span class="n">user</span> <span class="o">=</span> <span class="n">user</span>
<span class="bp">self</span><span class="o">.</span><span class="n">mail</span> <span class="o">=</span> <span class="n">mail</span>
<span class="bp">self</span><span class="o">.</span><span class="n">firstname</span> <span class="o">=</span> <span class="n">firstname</span>
<span class="bp">self</span><span class="o">.</span><span class="n">lastname</span> <span class="o">=</span> <span class="n">lastname</span>
<span class="bp">self</span><span class="o">.</span><span class="n">website</span> <span class="o">=</span> <span class="n">website</span>
<span class="nd">@classmethod</span>
<span class="k">def</span> <span class="nf">is_valid_username</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">username</span><span class="p">):</span>
<span class="sd">'''return True if username matches User.USER_FORMAT</span>
<span class="sd"> '''</span>
<span class="k">return</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">User</span><span class="o">.</span><span class="n">ALL_USER_FORMAT</span><span class="p">,</span> <span class="n">username</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span>
<span class="n">handler</span> <span class="o">=</span> <span class="n">tubes</span><span class="o">.</span><span class="n">Handler</span><span class="p">()</span>
<span class="c"># if the path starts with /files serve</span>
<span class="c"># as static files from the files/ directory</span>
<span class="n">handler</span><span class="o">.</span><span class="n">register_static_path</span><span class="p">(</span><span class="s">'/files'</span><span class="p">,</span> <span class="s">'files/'</span><span class="p">)</span>
<span class="c"># a dict to simulate a database</span>
<span class="n">USERS</span> <span class="o">=</span> <span class="p">{}</span>
<span class="c"># when a post is made to this url the body is extracted and procesed by running</span>
<span class="c"># the User.from_json method, that way we receive a User object</span>
<span class="nd">@handler</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="s">'^/user/?$'</span><span class="p">,</span> <span class="n">accepts</span><span class="o">=</span><span class="n">tubes</span><span class="o">.</span><span class="n">JSON</span><span class="p">,</span> <span class="n">transform_body</span><span class="o">=</span><span class="n">User</span><span class="o">.</span><span class="n">from_json</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">new_user</span><span class="p">(</span><span class="n">handler</span><span class="p">,</span> <span class="n">user</span><span class="p">):</span>
<span class="c"># add the new user</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">User</span><span class="o">.</span><span class="n">is_valid_username</span><span class="p">(</span><span class="n">user</span><span class="o">.</span><span class="n">user</span><span class="p">):</span>
<span class="k">return</span> <span class="n">tubes</span><span class="o">.</span><span class="n">Response</span><span class="p">(</span><span class="s">'Invalid username'</span><span class="p">,</span> <span class="mf">500</span><span class="p">)</span>
<span class="n">USERS</span><span class="p">[</span><span class="n">user</span><span class="o">.</span><span class="n">user</span><span class="p">]</span> <span class="o">=</span> <span class="n">user</span>
<span class="c"># update user</span>
<span class="nd">@handler</span><span class="o">.</span><span class="kp">put</span><span class="p">(</span><span class="s">'^/user/?$'</span><span class="p">,</span> <span class="n">accepts</span><span class="o">=</span><span class="n">tubes</span><span class="o">.</span><span class="n">JSON</span><span class="p">,</span> <span class="n">transform_body</span><span class="o">=</span><span class="n">User</span><span class="o">.</span><span class="n">from_json</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">update_user</span><span class="p">(</span><span class="n">handler</span><span class="p">,</span> <span class="n">user</span><span class="p">):</span>
<span class="k">if</span> <span class="n">user</span><span class="o">.</span><span class="n">user</span> <span class="ow">in</span> <span class="n">USERS</span><span class="p">:</span>
<span class="n">USERS</span><span class="p">[</span><span class="n">user</span><span class="o">.</span><span class="n">user</span><span class="p">]</span> <span class="o">=</span> <span class="n">user</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">tubes</span><span class="o">.</span><span class="n">Response</span><span class="p">(</span><span class="s">"user not found"</span><span class="p">,</span> <span class="mf">404</span><span class="p">)</span>
<span class="c"># match a valid username identifier</span>
<span class="c"># if no produces keyword is given assume JSON</span>
<span class="nd">@handler</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'^/user/('</span> <span class="o">+</span> <span class="n">User</span><span class="o">.</span><span class="n">USER_FORMAT</span> <span class="o">+</span> <span class="s">')/?'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get_user</span><span class="p">(</span><span class="n">handler</span><span class="p">,</span> <span class="n">username</span><span class="p">):</span>
<span class="k">if</span> <span class="n">username</span> <span class="ow">in</span> <span class="n">USERS</span><span class="p">:</span>
<span class="k">return</span> <span class="n">USERS</span><span class="p">[</span><span class="n">username</span><span class="p">]</span>
<span class="k">return</span> <span class="n">tubes</span><span class="o">.</span><span class="n">Response</span><span class="p">(</span><span class="s">"user not found"</span><span class="p">,</span> <span class="mf">404</span><span class="p">)</span>
<span class="c"># return all users</span>
<span class="nd">@handler</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'^/users/?'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get_users</span><span class="p">(</span><span class="n">handler</span><span class="p">):</span>
<span class="c"># isn't this easy?</span>
<span class="k">return</span> <span class="n">User</span><span class="o">.</span><span class="n">to_json_list</span><span class="p">(</span><span class="n">USERS</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>
<span class="nd">@handler</span><span class="o">.</span><span class="kp">delete</span><span class="p">(</span><span class="s">'^/user/('</span> <span class="o">+</span> <span class="n">User</span><span class="o">.</span><span class="n">USER_FORMAT</span> <span class="o">+</span> <span class="s">')/?'</span><span class="p">,</span> <span class="n">transform_body</span><span class="o">=</span><span class="n">User</span><span class="o">.</span><span class="n">from_json</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">remove_user</span><span class="p">(</span><span class="n">handler</span><span class="p">,</span> <span class="n">username</span><span class="p">):</span>
<span class="k">if</span> <span class="n">username</span> <span class="ow">in</span> <span class="n">USERS</span><span class="p">:</span>
<span class="k">del</span> <span class="n">USERS</span><span class="p">[</span><span class="n">username</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">tubes</span><span class="o">.</span><span class="n">Response</span><span class="p">(</span><span class="s">"user not found"</span><span class="p">,</span> <span class="mf">404</span><span class="p">)</span>
<span class="c"># return the generated requests javascript code on that url</span>
<span class="c"># (on production you should save it to a file)</span>
<span class="nd">@handler</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'^/requests.js/?$'</span><span class="p">,</span> <span class="n">produces</span><span class="o">=</span><span class="n">tubes</span><span class="o">.</span><span class="n">JS</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">requests</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="sd">'''return the requests.js file to interact with this API'''</span>
<span class="k">return</span> <span class="n">tubes</span><span class="o">.</span><span class="n">Response</span><span class="p">(</span><span class="n">REQUESTS</span><span class="p">)</span>
<span class="c"># return the generated model javascript code on that url</span>
<span class="c"># (on production you should save it to a file)</span>
<span class="nd">@handler</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'^/model.js/?$'</span><span class="p">,</span> <span class="n">produces</span><span class="o">=</span><span class="n">tubes</span><span class="o">.</span><span class="n">JS</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">model</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="sd">'''return the model.js file to interact with this API'''</span>
<span class="k">return</span> <span class="n">MODEL</span>
<span class="c"># show the API test page here</span>
<span class="nd">@handler</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'^/test.html/?$'</span><span class="p">,</span> <span class="n">produces</span><span class="o">=</span><span class="n">tubes</span><span class="o">.</span><span class="n">HTML</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="sd">'''return a dummy html to play with the API'''</span>
<span class="k">return</span> <span class="n">TEST_PAGE</span>
<span class="n">REQUESTS</span> <span class="o">=</span> <span class="n">intertubes</span><span class="o">.</span><span class="n">generate_requests</span><span class="p">(</span><span class="n">handler</span><span class="p">)</span>
<span class="n">MODEL</span> <span class="o">=</span> <span class="n">intertubes</span><span class="o">.</span><span class="n">generate_model</span><span class="p">([</span><span class="n">User</span><span class="p">])</span>
<span class="n">TEST_PAGE</span> <span class="o">=</span> <span class="n">intertubes</span><span class="o">.</span><span class="n">generate_html_example</span><span class="p">(</span><span class="n">handler</span><span class="p">,</span>
<span class="p">(</span><span class="s">'/files/json2.js'</span><span class="p">,</span> <span class="s">'/model.js'</span><span class="p">))</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">tubes</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">handler</span><span class="p">,</span> <span class="n">use_reloader</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
<h4>test it</h4>
<p>
point your browser to
<a href="http://0.0.0.0:8000/test.html">http://0.0.0.0:8000/test.html</a>
to test the API from the generated test page.
</p>
<p>
you can also see the code generated for the
<a href="http://0.0.0.0:8000/requests.js">requests</a> and
<a href="http://0.0.0.0:8000/model.js">model</a>
</p>
<p>
try to add a new user with something like
<pre>
model.User('marianoguerra', 'user@account')
</pre>
on the newUser form. That content will be evaluated and sent to the server, then try with getUser/removeUser (using the username you used above) and getUsers.
</p>
<p>
keep adding methods and reloading the test page to test them
</p>
</div>
</div>
</div>
</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10841887-1");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>