Mercurial > hg > WSGraph
annotate wsgraph/web.py @ 4:c7170cab1184
more RESTful stubbing
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sun, 09 Dec 2012 20:10:07 -0800 |
parents | 42f484880808 |
children | 9d5a8c90c482 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
4 web handler for WSGraph | |
5 """ | |
6 | |
7 import json | |
8 from webob import Request, Response, exc | |
9 | |
4 | 10 GRAPH = 0 |
11 NODE = 1 | |
12 EDGE = 2 | |
13 | |
0 | 14 def JSONFormatter(**kwargs): |
15 return json.dumps(kwargs, sort_keys=True) | |
16 | |
17 def JSONGraphFormatter(graph): | |
18 return json.dumps({'nodes': graph.nodes(), | |
19 'edges': graph.edges()}, | |
20 sort_keys=True) | |
21 | |
22 class Dispatcher(object): | |
23 | |
24 def __init__(self, graph, | |
25 graph_formatters=None, # TODO | |
26 node_formatters=None, # TODO | |
27 edge_formatters=None, # TODO | |
28 require_auth=False): | |
29 self.graph = graph | |
30 self.require_auth = require_auth | |
31 self.formatters = {0: JSONGraphFormatter, | |
32 1: JSONFormatter, | |
33 2: JSONFormatter} | |
34 | |
3 | 35 self.methods = dict([(i, getattr(self, i)) |
36 for i in dir(self) | |
37 if i.isupper()]) | |
38 | |
0 | 39 def __call__(self, environ, start_response): |
40 request = Request(environ) | |
3 | 41 method = self.methods.get(request.method) |
42 if method is None: | |
43 return exc.HTTPMethodNotAllowed()(environ, start_response) | |
44 response = method(request) | |
0 | 45 return response(environ, start_response) |
46 | |
47 @staticmethod | |
48 def path_segments(request): | |
49 import pdb; pdb.set_trace() | |
50 | |
51 # HTTP methods | |
52 | |
53 def GET(self, request): | |
2
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
54 """ |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
55 respond to a GET request |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
56 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
57 Formatters are keyed off of |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
58 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
59 formatters = {0: { |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
60 } |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
61 |
4 | 62 A graph formatter takes the following arguments: |
63 | |
64 def sample_graph_formatter(graph, request): | |
65 | |
2
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
66 API: |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
67 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
68 ?format=<format> |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
69 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
70 """ |
0 | 71 |
72 segments = self.path_segments(request) | |
4 | 73 if len(segments) not in (0,1,2): |
74 return exc.HTTPNotFound() | |
0 | 75 |
76 # formatter | |
77 formatter = self.formatters[len(segments)] | |
78 return Response(content_type='text/plain', | |
79 body="WSGraph") | |
80 | |
81 def POST(self, request): | |
2
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
82 """ |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
83 respond to a POST request |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
84 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
85 API: |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
86 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
87 ?update : |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
88 """ |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
89 |
4 | 90 return exc.HTTPSeeOther(location='/') # TODO: /path/to/self |
0 | 91 |
92 def HEAD(self, request): | |
93 """respond to a HEAD request""" | |
94 raise NotImplementedError | |
95 | |
3 | 96 def OPTIONS(self, request): |
4 | 97 return Response() # TODO: Allow=', '.join(self.methods); Content-Length=0 |
3 | 98 |
0 | 99 if __name__ == '__main__': |
100 | |
3 | 101 ### example web server |
102 | |
0 | 103 # imports |
104 from wsgiref import simple_server | |
3 | 105 from model import MemoryCache |
106 from optparse import OptionParser | |
0 | 107 |
3 | 108 # parse command line options |
109 parser = OptionParser() | |
110 parser.add_option('-p', '--port', type='int', default=8080, | |
111 help="port to serve on") | |
112 options, args = parser.parse_args() | |
113 | |
114 # example model | |
0 | 115 graph = MemoryCache() |
116 | |
3 | 117 # WSGI app |
0 | 118 app = Dispatcher(graph=graph) |
3 | 119 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app) |
120 print 'http://localhost:%s/' % options.port | |
0 | 121 server.serve_forever() |
122 |