~bjornt/launchpad/architect-vision

« back to all changes in this revision

Viewing changes to lib/canonical/launchpad/pagetests/webservice/launchpadlib.txt

  • Committer: Bjorn Tillenius
  • Date: 2010-05-12 12:52:09 UTC
  • mfrom: (10599.1.251 launchpad)
  • Revision ID: bjorn@canonical.com-20100512125209-pip7jav1a7xq52d8
Merge RF.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
*******************************
 
2
Using launchpadlib in pagetests
 
3
*******************************
 
4
 
 
5
As an alternative to crafting HTTP requests with the 'webservice'
 
6
object, you can write pagetests using launchpadlib.
 
7
 
 
8
Two helper functions make it easy to set up Launchpad objects that
 
9
can access the web service. With launchpadlib_for() you can set up a
 
10
Launchpad object for a given user with a single call.
 
11
 
 
12
    >>> launchpad = launchpadlib_for(
 
13
    ...     'launchpadlib test', 'salgado', 'WRITE_PUBLIC')
 
14
    >>> print launchpad.me.name
 
15
    salgado
 
16
 
 
17
    # XXX leonardr 2010-03-31 bug=552732
 
18
    # launchpadlib doesn't work with a credential scoped to a context
 
19
    # like 'firefox', because the service root resource is considered
 
20
    # out of scope. This test should pass, but it doesn't.
 
21
    #
 
22
    # When you fix this, be sure to show that an attempt to access
 
23
    # something that really is out of scope (like launchpad.me.name)
 
24
    # yields a 401 error.
 
25
    #
 
26
    #>>> launchpad = launchpadlib_for(
 
27
    #...     'launchpadlib test', 'no-priv', 'READ_PRIVATE', 'firefox',
 
28
    #...      version="devel")
 
29
    #>>> print launchpad.projects['firefox'].name
 
30
    #firefox
 
31
 
 
32
With launchpadlib_credentials_for() you can get a launchpadlib
 
33
Credentials object.
 
34
 
 
35
    >>> from lp.testing import launchpadlib_credentials_for
 
36
    >>> credentials = launchpadlib_credentials_for(
 
37
    ...     'launchpadlib test', 'no-priv', 'READ_PUBLIC')
 
38
    >>> credentials
 
39
    <launchpadlib.credentials.Credentials object ...>
 
40
 
 
41
    >>> print credentials.consumer.key
 
42
    launchpadlib test
 
43
    >>> print credentials.access_token
 
44
    oauth_token_secret=...&oauth_token=...
 
45
 
 
46
This can be used to create your own Launchpad object.  Note you cannot
 
47
use launchpadlib.uris.DEV_SERVICE_ROOT as the URL as it uses the https
 
48
scheme which does not work in the test environment.
 
49
 
 
50
    >>> from launchpadlib.launchpad import Launchpad
 
51
    >>> launchpad = Launchpad(credentials, 'http://api.launchpad.dev/')
 
52
    >>> print launchpad.me.name
 
53
    no-priv
 
54
 
 
55
Anonymous access
 
56
================
 
57
 
 
58
    >>> lp_anon = Launchpad.login_anonymously('launchpadlib test',
 
59
    ...                                       'http://api.launchpad.dev/')
 
60
 
 
61
The Launchpad object for the anonymous user can be used to access
 
62
public information.
 
63
 
 
64
    >>> apache_results = lp_anon.project_groups.search(text="Apache")
 
65
    >>> print apache_results[0].name
 
66
    apache
 
67
 
 
68
But trying to access information that requires a logged in user
 
69
results in an error.
 
70
 
 
71
    >>> print lp_anon.me.name
 
72
    Traceback (most recent call last):
 
73
      ...
 
74
    HTTPError: HTTP Error 401: Unauthorized...
 
75
 
 
76
Caching
 
77
=======
 
78
 
 
79
Let's make sure Launchpad serves caching-related headers that make
 
80
launchpadlib work correctly. First, we set up a temporary directory to
 
81
store the cache.
 
82
 
 
83
    >>> import tempfile
 
84
    >>> cache = tempfile.mkdtemp()
 
85
 
 
86
Then we make it possible to view the HTTP traffic between launchpadlib
 
87
and Launchpad.
 
88
 
 
89
    >>> import httplib2
 
90
    >>> old_debug_level = httplib2.debuglevel
 
91
    >>> httplib2.debuglevel = 1
 
92
 
 
93
Now create a Launchpad object and observe how it populates the cache.
 
94
 
 
95
    >>> launchpad = Launchpad(
 
96
    ...     credentials, 'http://api.launchpad.dev/', cache=cache)
 
97
    send: 'GET /1.0/ ...accept: application/vnd.sun.wadl+xml...'
 
98
    reply: 'HTTP/1.0 200 Ok\n'
 
99
    ...
 
100
    send: 'GET /1.0/ ...accept: application/json...'
 
101
    reply: 'HTTP/1.0 200 Ok\n'
 
102
    ...
 
103
 
 
104
Create a second Launchpad object, and the cached documents will be
 
105
used instead of new HTTP requests being used.
 
106
 
 
107
    >>> launchpad = Launchpad(
 
108
    ...     credentials, 'http://api.launchpad.dev/', cache=cache)
 
109
 
 
110
Cleanup.
 
111
 
 
112
    >>> import shutil
 
113
    >>> shutil.rmtree(cache)
 
114
    >>> httplib2.debuglevel = old_debug_level
 
115