~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/service.py

  • Committer: Curtis Hovey
  • Date: 2012-03-17 21:17:03 UTC
  • Revision ID: curtis.hovey@canonical.com-20120317211703-hsmk5h4xux2jgx2p
use send response to ensure a list of strings is returned.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
        if self.method == 'GET':
39
39
            return self.do_GET()
40
40
 
41
 
    def send_response(self, code, headers={}, reason=None):
 
41
    def send_response(self, code, response='', headers={}, reason=None):
42
42
        if reason is None:
43
43
            reason = httplib.responses[code]
44
44
        response_code = '%s %s' % (code, reason)
45
45
        response_headers = {'content-type': 'application/json'}
46
46
        response_headers.update(headers.items())
47
47
        self.start_response(response_code, response_headers.items())
 
48
        return [response]
48
49
 
49
50
    def do_PUT(self):
50
51
        """Create an archive or message on PUT."""
52
53
            # This expected path is /archive/archive_id.
53
54
            try:
54
55
                self.store.put_archive(self.path[0])
55
 
                self.send_response(httplib.CREATED)
 
56
                return self.send_response(httplib.CREATED)
56
57
            except Exception, error:
57
 
                self.send_response(
 
58
                return self.send_response(
58
59
                    httplib.BAD_REQUEST, reason=error.__doc__)
59
60
        elif len(self.path) == 2:
60
61
            # This expected path is /archive/archive_id/message_id.
62
63
                put_input = self.environ['wsgi.input']
63
64
                message = put_input.read(int(self.environ['CONTENT_LENGTH']))
64
65
                self.store.put_message(self.path[0], self.path[1], message)
65
 
                self.send_response(httplib.CREATED)
 
66
                return self.send_response(httplib.CREATED)
66
67
            except Exception, error:
67
 
                self.send_response(
 
68
                return self.send_response(
68
69
                    httplib.BAD_REQUEST, reason=error.__doc__)
69
 
        return []
70
70
 
71
71
    def do_POST(self):
72
72
        """Change a message on POST."""
76
76
                # This expected path is /archive/archive_id/message_id.
77
77
                response = self.store.hide_message(
78
78
                    self.path[0], self.path[1], self.query_string)
79
 
                self.send_response(httplib.OK)
80
 
                return [simplejson.dumps(response)]
 
79
                response = simplejson.dumps(response)
 
80
                return self.send_response(httplib.OK, response=response)
81
81
            except Exception, error:
82
 
                self.send_response(httplib.BAD_REQUEST, reason=error.__doc__)
83
 
                return []
 
82
                return self.send_response(
 
83
                    httplib.BAD_REQUEST, reason=error.__doc__)
84
84
 
85
85
    def do_GET(self):
86
86
        """Retrieve a list of messages on GET."""
87
87
        try:
88
88
            response = self.store.get_messages(
89
89
                self.path[0], self.query_string)
90
 
            self.send_response(httplib.OK)
91
 
            return [simplejson.dumps(response)]
 
90
            response = simplejson.dumps(response)
 
91
            return self.send_response(httplib.OK, response=response)
92
92
        except Exception, error:
93
 
            self.send_response(httplib.BAD_REQUEST, reason=error.__doc__)
94
 
            return []
 
93
            return self.send_response(
 
94
                httplib.BAD_REQUEST, reason=error.__doc__)
95
95
 
96
96
    def log_message(self, format, *args):
97
97
        """Override log_message to use standard Python logging."""