~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/service.py

  • Committer: Curtis Hovey
  • Date: 2012-03-17 21:01:26 UTC
  • Revision ID: curtis.hovey@canonical.com-20120317210126-tx0p2lg8qr1ch172
Remove old http request handler and fake service.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
__metaclass__ = type
2
 
__all__ = [
3
 
    'ForkedFakeService',
4
 
    'GrackleService',
5
 
    ]
6
2
 
7
3
import httplib
8
4
import logging
18
14
 
19
15
 
20
16
class GrackleService:
21
 
    """A request handler that forwards to an archive store."""
 
17
    """A request handler that forwards to server.store."""
22
18
 
23
19
    def __init__(self, store):
24
20
        self.store = store
30
26
        self.method = environ['REQUEST_METHOD']
31
27
        self.host_port = shift_path_info(environ)
32
28
        self.application = shift_path_info(environ)
33
 
        path = environ['PATH_INFO'].split('/')
34
 
        self.scheme = path.pop(0)
35
 
        self.path = path
 
29
        self.path = environ['PATH_INFO'].split('/')[1:]
36
30
        self.query_string = environ['QUERY_STRING']
37
31
        return self.handle_request()
38
32
 
39
33
    def handle_request(self):
40
 
        """Select the method to handle the request and return a response."""
41
 
        if self.application != 'archive':
42
 
            return self.send_response(httplib.NOT_FOUND)
43
34
        if self.method == 'PUT':
44
35
            return self.do_PUT()
45
 
        elif self.method == 'POST':
 
36
        if self.method == 'POST':
46
37
            return self.do_POST()
47
 
        elif self.method == 'GET':
 
38
        if self.method == 'GET':
48
39
            return self.do_GET()
49
 
        return self.send_response(httplib.METHOD_NOT_ALLOWED)
50
40
 
51
 
    def send_response(self, code, response='', reason=None, headers={}):
52
 
        """Set the status code and reason, then return the response."""
 
41
    def send_response(self, code, headers={}, reason=None):
53
42
        if reason is None:
54
 
            reason = httplib.responses[code]
 
43
            reason = httplib.responses[code].upper()
55
44
        response_code = '%s %s' % (code, reason)
56
45
        response_headers = {'content-type': 'application/json'}
57
46
        response_headers.update(headers.items())
58
47
        self.start_response(response_code, response_headers.items())
59
 
        return [response]
60
48
 
61
49
    def do_PUT(self):
62
50
        """Create an archive or message on PUT."""
64
52
            # This expected path is /archive/archive_id.
65
53
            try:
66
54
                self.store.put_archive(self.path[0])
67
 
                return self.send_response(httplib.CREATED)
 
55
                self.send_response(httplib.CREATED)
 
56
                return ['']
68
57
            except Exception, error:
69
 
                return self.send_response(
 
58
                self.send_response(
70
59
                    httplib.BAD_REQUEST, reason=error.__doc__)
71
 
        elif len(self.path) == 2:
 
60
                return ['']
 
61
        if len(self.path) == 2:
72
62
            # This expected path is /archive/archive_id/message_id.
73
63
            try:
74
64
                put_input = self.environ['wsgi.input']
75
65
                message = put_input.read(int(self.environ['CONTENT_LENGTH']))
76
66
                self.store.put_message(self.path[0], self.path[1], message)
77
 
                return self.send_response(httplib.CREATED)
78
 
            except Exception, error:
79
 
                return self.send_response(
80
 
                    httplib.BAD_REQUEST, reason=error.__doc__)
 
67
                self.send_response(httplib.CREATED)
 
68
                return ['']
 
69
            except:
 
70
                self.send_response(httplib.BAD_REQUEST)
 
71
                return ['']
81
72
 
82
73
    def do_POST(self):
83
74
        """Change a message on POST."""
87
78
                # This expected path is /archive/archive_id/message_id.
88
79
                response = self.store.hide_message(
89
80
                    self.path[0], self.path[1], self.query_string)
90
 
                response = simplejson.dumps(response)
91
 
                return self.send_response(httplib.OK, response=response)
92
 
            except Exception, error:
93
 
                return self.send_response(
94
 
                    httplib.BAD_REQUEST, reason=error.__doc__)
 
81
                self.send_response(httplib.OK)
 
82
                return [simplejson.dumps(response)]
 
83
            except:
 
84
                self.send_response(httplib.BAD_REQUEST)
 
85
                return ['']
95
86
 
96
87
    def do_GET(self):
97
88
        """Retrieve a list of messages on GET."""
98
89
        try:
99
90
            response = self.store.get_messages(
100
91
                self.path[0], self.query_string)
101
 
            response = simplejson.dumps(response)
102
 
            return self.send_response(httplib.OK, response=response)
 
92
            self.send_response(httplib.OK)
 
93
            return [simplejson.dumps(response)]
103
94
        except Exception, error:
104
 
            return self.send_response(
105
 
                httplib.BAD_REQUEST, reason=error.__doc__)
 
95
            self.send_response(httplib.BAD_REQUEST, reason=error.__doc__)
 
96
            return ['']
106
97
 
107
98
    def log_message(self, format, *args):
108
99
        """Override log_message to use standard Python logging."""
173
164
        os.kill(self.pid, SIGKILL)
174
165
 
175
166
 
 
167
def application(environ, start_response):
 
168
    start_response('200 OK', [('Content-Type', 'text/plain')])
 
169
    return "Hello World"
 
170
 
 
171
 
176
172
if __name__ == '__main__':
177
173
    app = GrackleService(MemoryStore({}))
178
174
    service = make_server('', 8787, app)