Mercurial > hg > WSGraph
annotate wsgraph/web.py @ 2:7a3b21cafc00
start to sketch out REST API
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sun, 09 Dec 2012 10:40:17 -0800 |
parents | cfcfa093e4b4 |
children | 42f484880808 |
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 | |
10 def JSONFormatter(**kwargs): | |
11 return json.dumps(kwargs, sort_keys=True) | |
12 | |
13 def JSONGraphFormatter(graph): | |
14 return json.dumps({'nodes': graph.nodes(), | |
15 'edges': graph.edges()}, | |
16 sort_keys=True) | |
17 | |
18 class Dispatcher(object): | |
19 | |
20 def __init__(self, graph, | |
21 graph_formatters=None, # TODO | |
22 node_formatters=None, # TODO | |
23 edge_formatters=None, # TODO | |
24 require_auth=False): | |
25 self.graph = graph | |
26 self.require_auth = require_auth | |
27 self.formatters = {0: JSONGraphFormatter, | |
28 1: JSONFormatter, | |
29 2: JSONFormatter} | |
30 | |
31 def __call__(self, environ, start_response): | |
32 request = Request(environ) | |
33 import pdb; pdb.set_trace() | |
34 return response(environ, start_response) | |
35 | |
36 @staticmethod | |
37 def path_segments(request): | |
38 import pdb; pdb.set_trace() | |
39 | |
40 # HTTP methods | |
41 | |
42 def GET(self, request): | |
2
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
43 """ |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
44 respond to a GET request |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
45 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
46 Formatters are keyed off of |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
47 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
48 formatters = {0: { |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
49 } |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
50 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
51 API: |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
52 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
53 ?format=<format> |
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 """ |
0 | 56 |
57 segments = self.path_segments(request) | |
58 | |
59 # formatter | |
60 formatter = self.formatters[len(segments)] | |
61 return Response(content_type='text/plain', | |
62 body="WSGraph") | |
63 | |
64 def POST(self, request): | |
2
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
65 """ |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
66 respond to a POST request |
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 API: |
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 ?update : |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
71 """ |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
72 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
73 return exc.HTTPSeeOther('/') # TODO: /path/to/self |
0 | 74 |
75 def HEAD(self, request): | |
76 """respond to a HEAD request""" | |
77 raise NotImplementedError | |
78 | |
79 if __name__ == '__main__': | |
80 | |
81 # imports | |
82 from wsgiref import simple_server | |
83 from .model import MemoryCache | |
84 | |
85 graph = MemoryCache() | |
86 | |
87 app = Dispatcher(graph=graph) | |
88 server = simple_server.make_server(host='0.0.0.0', port=8080, app=app) | |
89 server.serve_forever() | |
90 |