|
OLD | NEW |
---|---|
(Empty) | |
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 from oauth2client.client import SignedJwtAssertionCredentials | |
6 import httplib2 | |
7 import model.app_config | |
8 import urllib | |
9 import util | |
10 | |
11 | |
12 EMAIL_SCOPE = 'https://www.googleapis.com/auth/userinfo.email' | |
13 | |
14 | |
15 class RietveldRequestError(Exception): | |
16 """Raised on request errors.""" | |
17 pass | |
18 | |
19 | |
20 class Rietveld(object): | |
agable
2013/08/05 17:38:00
Either have a class docstring here or a module doc
Mattias Nissler (ping if slow)
2013/08/05 18:40:47
Done.
| |
21 def __init__(self): | |
22 self.app_config = model.app_config.get() | |
23 | |
24 @util.lazy_property | |
25 def http(self): | |
26 http = httplib2.Http() | |
27 | |
28 creds = SignedJwtAssertionCredentials(self.app_config.client_id, | |
29 self.app_config.service_account_key, | |
30 EMAIL_SCOPE) | |
31 creds.authorize(http) | |
32 return http | |
33 | |
34 @util.lazy_property | |
35 def xsrf_token(self): | |
36 return self.make_request('xsrf_token', | |
37 headers = {'X-Requesting-XSRF-Token': 1}) | |
38 | |
39 def make_request(self, req, *args, **kwargs): | |
40 resp, response = self.http.request( | |
41 '%s/%s' % (self.app_config.server_url, req), *args, **kwargs) | |
42 if resp.status != 200: | |
43 raise RietveldRequestError( | |
44 'Rietveld %s request failed: %s\n%s' % | |
45 (req, resp.status, str(resp)), resp, response) | |
46 | |
47 return response | |
48 | |
49 def post_data(self, req, payload = None): | |
50 actual_payload = dict(payload or {}) | |
51 actual_payload['xsrf_token'] = self.xsrf_token | |
52 | |
53 return self.make_request(req, method = 'POST', | |
54 body = urllib.urlencode(actual_payload)) | |
55 | |
56 def post_issue_data(self, issue, req, payload): | |
57 return self.post_data('%s/%s' % (issue, req), payload) | |
58 | |
59 def post_comment(self, issue, comment, submit_inline_comments = False): | |
60 publish_payload = { | |
61 'message_only': 0 if submit_inline_comments else 1, | |
agable
2013/08/05 17:38:00
This is the equivalent of differentiating between
Mattias Nissler (ping if slow)
2013/08/05 18:40:47
Correct.
| |
62 'send_mail': 1, | |
63 'add_as_reviewer': 0, | |
64 'message': comment, | |
65 'no_redirect': 1, | |
66 } | |
67 self.post_issue_data(issue, 'publish', publish_payload) | |
68 | |
69 def add_inline_comment(self, issue_id, patchset_id, patch_id, line, a_or_b, | |
70 comment): | |
71 comment_payload = { | |
72 'snapshot': 'old' if a_or_b is 'a' else 'new', | |
73 'lineno': line, | |
74 'side': a_or_b, | |
75 'issue': issue_id, | |
76 'patchset': patchset_id, | |
77 'patch': patch_id, | |
78 'text': comment, | |
79 } | |
80 self.post_data('inline_draft', comment_payload) | |
OLD | NEW |