Merge lp:~jml/launchpadlib/cred-cache into lp:~launchpad-pqm/launchpadlib/devel

Proposed by Jonathan Lange
Status: Rejected
Rejected by: Jonathan Lange
Proposed branch: lp:~jml/launchpadlib/cred-cache
Merge into: lp:~launchpad-pqm/launchpadlib/devel
Diff against target: None lines
To merge this branch: bzr merge lp:~jml/launchpadlib/cred-cache
Reviewer Review Type Date Requested Status
Jonathan Lange (community) Disapprove
Review via email: mp+7608@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Jonathan Lange (jml) wrote :

This branch is a draft attempt at adding an API to launchpadlib to make it really easy to cache credentials.

Putting it up to get thoughts, happy to revise.

Revision history for this message
Jonathan Lange (jml) wrote :

Already implemented in login_with.

review: Disapprove

Unmerged revisions

42. By Jonathan Lange

Docstring. Woot.

41. By Jonathan Lange

Catch the other kinds of errors that we can get when opening a file.

40. By Jonathan Lange

Initial pass at a nicer interface for logging in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/launchpadlib/launchpad.py'
2--- src/launchpadlib/launchpad.py 2009-03-23 21:50:35 +0000
3+++ src/launchpadlib/launchpad.py 2009-05-11 09:02:22 +0000
4@@ -158,6 +158,49 @@
5 return cls(credentials, service_root, cache, timeout, proxy_info)
6
7 @classmethod
8+ def login_from_cache(cls, consumer_name,
9+ service_root=STAGING_SERVICE_ROOT, cache=None,
10+ timeout=None, proxy_info=None):
11+ """Use cached credentials if they exist, log in otherwise.
12+
13+ Looks for cached credentials in a particular file in the 'cache'
14+ directory ('creds-<service-host>-<consumer_name>'). If they exist,
15+ create a Launchpad instance using those, if not, delegate to
16+ `Launchpad.get_token_and_login`.
17+
18+ Once credentials are established, cache them in the 'cache' in a
19+ predictable location, so that this method can acquire them on future
20+ invocations.
21+
22+ :param consumer_name: The consumer name, as appropriate for the
23+ `Consumer` constructor
24+ :type consumer_name: string
25+ :param service_root: The URL to the root of the web service.
26+ :type service_root: string
27+ :return: The web service root
28+ :rtype: `Launchpad`
29+ """
30+ if cache is None:
31+ return cls.get_token_and_login(
32+ consumer_name, service_root, cache, timeout, proxy_info)
33+ web_root_uri = URI(service_root)
34+ credential_name = 'creds-%s-%s' % (web_root_uri.host, consumer_name)
35+ credential_path = os.path.join(cache, credential_name)
36+ try:
37+ credentials = Credentials.load_from_path(credential_path)
38+ except (OSError, IOError):
39+ launchpad = cls.get_token_and_login(
40+ consumer_name, service_root, cache, timeout, proxy_info)
41+ launchpad.credentials.save_to_path(credential_path)
42+ else:
43+ access_key = credentials.access_token.key
44+ access_secret = credentials.access_token.secret
45+ launchpad = cls.login(
46+ consumer_name, access_key, access_secret, service_root,
47+ cache, timeout, proxy_info)
48+ return launchpad
49+
50+ @classmethod
51 def login_with(cls, consumer_name,
52 service_root=STAGING_SERVICE_ROOT,
53 launchpadlib_dir=None, timeout=None, proxy_info=None):

Subscribers

People subscribed via source and target branches