From 70c32cc00fcd9f965ced950f162654e7376d7e54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarek=20Ziad=C3=A9?= Date: Wed, 13 Apr 2011 11:15:57 +0200 Subject: [PATCH] now using the Services class everywhere --- development.ini | 3 +++ linkdrop/controllers/__init__.py | 35 ++++++++++++++++++++++++++++++++ linkdrop/controllers/contacts.py | 23 ++++++++++++--------- linkdrop/controllers/send.py | 22 +++++++++++--------- test.ini | 3 +++ 5 files changed, 66 insertions(+), 20 deletions(-) diff --git a/development.ini b/development.ini index ad487fb..ea0bc7a 100644 --- a/development.ini +++ b/development.ini @@ -57,6 +57,9 @@ oauth.linkedin.com.request = https://api.linkedin.com/uas/oauth/requestToken oauth.linkedin.com.access = https://api.linkedin.com/uas/oauth/accessToken oauth.linkedin.com.authorize = https://api.linkedin.com/uas/oauth/authorize +sstatus.servers = 127.0.0.1:11211 +sstatus.domains = google.com,twitter.com,facebook.com,linkedin.com + [server:main] use = egg:Paste#http host = 127.0.0.1 diff --git a/linkdrop/controllers/__init__.py b/linkdrop/controllers/__init__.py index e69de29..06828e8 100644 --- a/linkdrop/controllers/__init__.py +++ b/linkdrop/controllers/__init__.py @@ -0,0 +1,35 @@ +# ***** BEGIN LICENSE BLOCK ***** +# Version: MPL 1.1 +# +# The contents of this file are subject to the Mozilla Public License Version +# 1.1 (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License +# for the specific language governing rights and limitations under the +# License. +# +# The Original Code is Raindrop. +# +# The Initial Developer of the Original Code is +# Mozilla Messaging, Inc.. +# Portions created by the Initial Developer are Copyright (C) 2009 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): Tarek Ziade +# + +# XXX services instance to be moved in the future application +# object once Pylons gets removed + +from linkoauth import Services +from pylons import config + +servers = config['sstatus.servers'].split(',') +domains = config['sstatus.domains'].split(',') +ttl = int(config.get('sstatus.ttl', '60')) + +services = Services(domains, servers, ttl) + diff --git a/linkdrop/controllers/contacts.py b/linkdrop/controllers/contacts.py index d99dd9f..91046d7 100644 --- a/linkdrop/controllers/contacts.py +++ b/linkdrop/controllers/contacts.py @@ -30,8 +30,9 @@ from pylons.controllers.util import abort, redirect from pylons.decorators import jsonify from pylons.decorators.util import get_pylons -from linkoauth import get_provider from linkoauth.base import OAuthKeysException, ServiceUnavailableException +from linkoauth.errors import DomainNotRegisteredError +from linkdrop.controllers import services from linkdrop.lib.base import BaseController, render from linkdrop.lib.helpers import json_exception_response, api_response, api_entry, api_arg @@ -94,15 +95,8 @@ Name of the group to return. startIndex = int(request.POST.get('startindex','0')) maxResults = int(request.POST.get('maxresults','25')) account_data = request.POST.get('account', None) - provider = get_provider(domain) - if provider is None: - error = { - 'message': "'domain' is invalid", - 'code': constants.INVALID_PARAMS - } - return {'result': None, 'error': error} - acct = None + if account_data: acct = json.loads(account_data) if not acct: @@ -114,7 +108,16 @@ Name of the group to return. return {'result': None, 'error': error} try: - result, error = provider.api(acct).getcontacts(startIndex, maxResults, group) + #result, error = provider.api(acct).getcontacts(startIndex, maxResults, group) + result, error = services.getcontacts(domain, acct, startIndex, + maxResults, group) + except DomainNotRegisteredError: + error = { + 'message': "'domain' is invalid", + 'code': constants.INVALID_PARAMS + } + return {'result': None, 'error': error} + except OAuthKeysException, e: # more than likely we're missing oauth tokens for some reason. error = {'provider': domain, diff --git a/linkdrop/controllers/send.py b/linkdrop/controllers/send.py index a798941..f13154a 100644 --- a/linkdrop/controllers/send.py +++ b/linkdrop/controllers/send.py @@ -36,8 +36,10 @@ from pylons import config, request, response from pylons.controllers.util import abort, redirect from pylons.decorators.util import get_pylons -from linkoauth import get_provider +#from linkoauth import get_provider from linkoauth.base import OAuthKeysException, ServiceUnavailableException +from linkdrop.controllers import services +from linkoauth.errors import DomainNotRegisteredError from linkdrop.lib.base import BaseController from linkdrop.lib.helpers import json_exception_response, api_response, api_entry, api_arg @@ -129,13 +131,6 @@ Site provided description of the shared item, not supported by all services. 'code': constants.INVALID_PARAMS } return {'result': result, 'error': error} - provider = get_provider(domain) - if provider is None: - error = { - 'message': "'domain' is invalid", - 'code': constants.INVALID_PARAMS - } - return {'result': result, 'error': error} if account_data: acct = json.loads(account_data) @@ -160,9 +155,16 @@ Site provided description of the shared item, not supported by all services. acct_hash = hashlib.sha1("%s#%s" % ((username or '').encode('utf-8'), (userid or '').encode('utf-8'))).hexdigest() timer = metrics.start_timer(request, domain=domain, message_len=len(message), long_url=longurl, short_url=shorturl, acct_id=acct_hash) - # send the item. + + # send the item try: - result, error = provider.api(acct).sendmessage(message, args) + result, error = services.sendmessage(domain, acct, message, args) + except DomainNotRegisteredError: + error = { + 'message': "'domain' is invalid", + 'code': constants.INVALID_PARAMS + } + return {'result': result, 'error': error} except OAuthKeysException, e: # XXX - I doubt we really want a full exception logged here? #log.exception('error providing item to %s: %s', domain, e) diff --git a/test.ini b/test.ini index 0b056a9..a55a231 100644 --- a/test.ini +++ b/test.ini @@ -67,6 +67,9 @@ oauth.yahoo.com.app_id = FILL_ME_IN # set to true if you have completed domain verification with Yahoo oauth.yahoo.com.verified = 0 +sstatus.servers = 127.0.0.1:11211 +sstatus.domains = google.com,twitter.com,facebook.com,linkedin.com + [server:main] use = egg:Paste#http host = 127.0.0.1