~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
 
            except:
67
 
                self.send_response(httplib.BAD_REQUEST)
68
 
        return []
 
66
                return self.send_response(httplib.CREATED)
 
67
            except Exception, error:
 
68
                return self.send_response(
 
69
                    httplib.BAD_REQUEST, reason=error.__doc__)
69
70
 
70
71
    def do_POST(self):
71
72
        """Change a message on POST."""
75
76
                # This expected path is /archive/archive_id/message_id.
76
77
                response = self.store.hide_message(
77
78
                    self.path[0], self.path[1], self.query_string)
78
 
                self.send_response(httplib.OK)
79
 
                return [simplejson.dumps(response)]
80
 
            except:
81
 
                self.send_response(httplib.BAD_REQUEST)
82
 
                return []
 
79
                response = simplejson.dumps(response)
 
80
                return self.send_response(httplib.OK, response=response)
 
81
            except Exception, error:
 
82
                return self.send_response(
 
83
                    httplib.BAD_REQUEST, reason=error.__doc__)
83
84
 
84
85
    def do_GET(self):
85
86
        """Retrieve a list of messages on GET."""
86
87
        try:
87
88
            response = self.store.get_messages(
88
89
                self.path[0], self.query_string)
89
 
            self.send_response(httplib.OK)
90
 
            return [simplejson.dumps(response)]
 
90
            response = simplejson.dumps(response)
 
91
            return self.send_response(httplib.OK, response=response)
91
92
        except Exception, error:
92
 
            self.send_response(httplib.BAD_REQUEST, reason=error.__doc__)
93
 
            return []
 
93
            return self.send_response(
 
94
                httplib.BAD_REQUEST, reason=error.__doc__)
94
95
 
95
96
    def log_message(self, format, *args):
96
97
        """Override log_message to use standard Python logging."""