Mercurial > hg > WSGraph
comparison wsgraph/web.py @ 23:24d57daaca21
well, now the request dispatches
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Mon, 10 Dec 2012 21:15:24 -0800 |
| parents | a401bf63c938 |
| children |
comparison
equal
deleted
inserted
replaced
| 22:a401bf63c938 | 23:24d57daaca21 |
|---|---|
| 32 return Response(content_type=self.content_type, | 32 return Response(content_type=self.content_type, |
| 33 body=self.format(graph.edge(node1, node2))) | 33 body=self.format(graph.edge(node1, node2))) |
| 34 | 34 |
| 35 def graph(self, request, graph): | 35 def graph(self, request, graph): |
| 36 return Response(content_type=self.content_type, | 36 return Response(content_type=self.content_type, |
| 37 body='hello? is it me you\'re looking for?') | 37 body = self.format(graph())) |
| 38 | 38 |
| 39 | 39 |
| 40 class Dispatcher(object): | 40 class Dispatcher(object): |
| 41 | 41 |
| 42 def __init__(self, graph, | 42 def __init__(self, graph, |
| 73 """split a path into segments""" | 73 """split a path into segments""" |
| 74 segments = path.strip('/').split('/') | 74 segments = path.strip('/').split('/') |
| 75 if segments == ['']: | 75 if segments == ['']: |
| 76 segments = [] | 76 segments = [] |
| 77 return segments | 77 return segments |
| 78 | |
| 79 | |
| 80 def formatter(self, rank, request): | |
| 81 """returns the formatter for a request""" | |
| 82 formatters = self.formatters[rank] | |
| 83 format = request.GET.get('format') | |
| 84 return formatters.get(format, formatters[None]) | |
| 78 | 85 |
| 79 # HTTP methods | 86 # HTTP methods |
| 80 | 87 |
| 81 def GET(self, request): | 88 def GET(self, request): |
| 82 """ | 89 """ |
| 115 if (rank == EDGE) or (rank == NODE): | 122 if (rank == EDGE) or (rank == NODE): |
| 116 if tuple(segments) not in self.graph: | 123 if tuple(segments) not in self.graph: |
| 117 return exc.HTTPNotFound() | 124 return exc.HTTPNotFound() |
| 118 | 125 |
| 119 # formatter | 126 # formatter |
| 120 formatter = self.formatters[len(segments)] | 127 formatter = self.formatter(rank, request) |
| 121 return formatter(request, self.graph, *segments) | 128 return formatter(request, self.graph, *segments) |
| 122 return Response(content_type='text/plain', | |
| 123 body=content) | |
| 124 | 129 |
| 125 def POST(self, request): | 130 def POST(self, request): |
| 126 """ | 131 """ |
| 127 respond to a POST request | 132 respond to a POST request |
| 128 | 133 |
| 129 API: | 134 API: |
| 130 | 135 |
| 131 ?update : | 136 ?update : |
| 132 """ | 137 """ |
| 138 | |
| 139 # check authorization | |
| 140 if self.require_auth: | |
| 141 raise NotImplementedError | |
| 133 | 142 |
| 134 return exc.HTTPSeeOther(location='/') # TODO: /path/to/self | 143 return exc.HTTPSeeOther(location='/') # TODO: /path/to/self |
| 135 | 144 |
| 136 def HEAD(self, request): | 145 def HEAD(self, request): |
| 137 """respond to a HEAD request""" | 146 """respond to a HEAD request""" |
