1
# Copyright 2008 Canonical Ltd. All rights reserved.
1
# Copyright 2008 Canonical Ltd.
3
# This file is part of launchpadlib.
5
# launchpadlib is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by the Free
7
# Software Foundation, either version 3 of the License, or (at your option)
10
# launchpadlib is distributed in the hope that it will be useful, but WITHOUT
11
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15
# You should have received a copy of the GNU General Public License along with
16
# launchpadlib. If not, see <http://www.gnu.org/licenses/>.
3
18
"""Browser object to make requests of Launchpad web service.
32
47
self.credentials = credentials
33
48
self._connection = httplib2.Http()
35
def _request(self, url, data=None, method='GET'):
50
def _request(self, url, data=None, method='GET', **extra_headers):
36
51
"""Create an authenticated request object."""
37
52
oauth_request = OAuthRequest.from_consumer_and_token(
38
53
self.credentials.consumer,
42
57
OAuthSignatureMethod_PLAINTEXT(),
43
58
self.credentials.consumer,
44
59
self.credentials.access_token)
60
# Calculate the headers for the request.
62
headers.update(oauth_request.to_header(OAUTH_REALM))
63
headers.update(extra_headers)
45
64
# Make the request.
46
65
response, content = self._connection.request(
47
str(url), method=method, body=data,
48
headers=oauth_request.to_header(OAUTH_REALM))
66
str(url), method=method, body=data, headers=headers)
49
67
# Turn non-2xx responses into exceptions.
50
68
if response.status // 100 != 2:
51
69
raise HTTPError(response, content)
52
70
return response, content
54
72
def get(self, url):
55
"""Get the resource at the requested url."""
73
"""GET the resource at the requested url."""
56
74
response, content = self._request(url)
57
75
return simplejson.loads(content)
59
77
def post(self, url, method_name, **kws):
60
"""Post a request to the web service."""
78
"""POST a request to the web service."""
61
79
kws['ws.op'] = method_name
62
80
data = urlencode(kws)
63
81
return self._request(url, data, 'POST')
83
def patch(self, url, representation):
84
"""PATCH the object at url with the updated representation."""
85
self._request(url, simplejson.dumps(representation), 'PATCH',
86
**{'Content-Type': 'application/json'})