~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to opensource/launchpadlib/launchpadlib/_browser.py

  • Committer: Barry Warsaw
  • Date: 2008-06-23 12:19:26 UTC
  • mfrom: (6527.6.6 3-licenses)
  • mto: This revision was merged to the branch mainline in revision 6561.
  • Revision ID: barry@canonical.com-20080623121926-8ovafx2axv4ud8je
license branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2008 Canonical Ltd.  All rights reserved.
 
1
# Copyright 2008 Canonical Ltd.
 
2
 
 
3
# This file is part of launchpadlib.
 
4
#
 
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)
 
8
# any later version.
 
9
#
 
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
 
13
# more details.
 
14
#
 
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/>.
2
17
 
3
18
"""Browser object to make requests of Launchpad web service.
4
19
 
32
47
        self.credentials = credentials
33
48
        self._connection = httplib2.Http()
34
49
 
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.
 
61
        headers = {}
 
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
53
71
 
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)
58
76
 
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')
 
82
 
 
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'})