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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 web handler for WSGraph
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 """
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 import json
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 from webob import Request, Response, exc
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 def JSONFormatter(**kwargs):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 return json.dumps(kwargs, sort_keys=True)
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 def JSONGraphFormatter(graph):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 return json.dumps({'nodes': graph.nodes(),
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 'edges': graph.edges()},
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 sort_keys=True)
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 class Dispatcher(object):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 def __init__(self, graph,
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 graph_formatters=None, # TODO
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 node_formatters=None, # TODO
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 edge_formatters=None, # TODO
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 require_auth=False):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 self.graph = graph
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 self.require_auth = require_auth
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 self.formatters = {0: JSONGraphFormatter,
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 1: JSONFormatter,
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 2: JSONFormatter}
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 def __call__(self, environ, start_response):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 request = Request(environ)
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 import pdb; pdb.set_trace()
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 return response(environ, start_response)
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 @staticmethod
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 def path_segments(request):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 import pdb; pdb.set_trace()
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 # HTTP methods
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 segments = self.path_segments(request)
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 # formatter
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 formatter = self.formatters[len(segments)]
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 return Response(content_type='text/plain',
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 body="WSGraph")
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
75 def HEAD(self, request):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
76 """respond to a HEAD request"""
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
77 raise NotImplementedError
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
78
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
79 if __name__ == '__main__':
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
80
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
81 # imports
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
82 from wsgiref import simple_server
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
83 from .model import MemoryCache
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
84
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
85 graph = MemoryCache()
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
86
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
87 app = Dispatcher(graph=graph)
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
88 server = simple_server.make_server(host='0.0.0.0', port=8080, app=app)
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
89 server.serve_forever()
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
90