comparison 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
comparison
equal deleted inserted replaced
3:42f484880808 4:c7170cab1184
4 web handler for WSGraph 4 web handler for WSGraph
5 """ 5 """
6 6
7 import json 7 import json
8 from webob import Request, Response, exc 8 from webob import Request, Response, exc
9
10 GRAPH = 0
11 NODE = 1
12 EDGE = 2
9 13
10 def JSONFormatter(**kwargs): 14 def JSONFormatter(**kwargs):
11 return json.dumps(kwargs, sort_keys=True) 15 return json.dumps(kwargs, sort_keys=True)
12 16
13 def JSONGraphFormatter(graph): 17 def JSONGraphFormatter(graph):
53 Formatters are keyed off of 57 Formatters are keyed off of
54 58
55 formatters = {0: { 59 formatters = {0: {
56 } 60 }
57 61
62 A graph formatter takes the following arguments:
63
64 def sample_graph_formatter(graph, request):
65
58 API: 66 API:
59 67
60 ?format=<format> 68 ?format=<format>
61 69
62 """ 70 """
63 71
64 segments = self.path_segments(request) 72 segments = self.path_segments(request)
73 if len(segments) not in (0,1,2):
74 return exc.HTTPNotFound()
65 75
66 # formatter 76 # formatter
67 formatter = self.formatters[len(segments)] 77 formatter = self.formatters[len(segments)]
68 return Response(content_type='text/plain', 78 return Response(content_type='text/plain',
69 body="WSGraph") 79 body="WSGraph")
75 API: 85 API:
76 86
77 ?update : 87 ?update :
78 """ 88 """
79 89
80 return exc.HTTPSeeOther('/') # TODO: /path/to/self 90 return exc.HTTPSeeOther(location='/') # TODO: /path/to/self
81 91
82 def HEAD(self, request): 92 def HEAD(self, request):
83 """respond to a HEAD request""" 93 """respond to a HEAD request"""
84 raise NotImplementedError 94 raise NotImplementedError
85 95
86 def OPTIONS(self, request): 96 def OPTIONS(self, request):
87 raise NotImplementedError 97 return Response() # TODO: Allow=', '.join(self.methods); Content-Length=0
88 98
89 if __name__ == '__main__': 99 if __name__ == '__main__':
90 100
91 ### example web server 101 ### example web server
92 102