0
@@ -91,13 +91,13 @@ FE_SERVER = 'fireeagle.yahoo.net'
0
API_URL_TEMPLATE = string.Template(
0
- '${
proto}://${server}/api/' + API_VERSION + '/${method}'
0
+ '${
server}/api/' + API_VERSION + '/${method}'
0
OAUTH_URL_TEMPLATE = string.Template(
0
- '${
proto}://${server}/oauth/${method}'
0
+ '${
server}/oauth/${method}'
0
AUTHORIZE_URL_TEMPLATE = string.Template(
0
- '${
proto}://${server}/oauth/${method}?oauth_token=${token}'
0
+ '${
server}/oauth/${method}?oauth_token=${token}'
0
'Content-type': 'application/x-www-form-urlencoded',
0
@@ -247,27 +247,6 @@ FIREEAGLE_METHODS = {
0
-def read_consumer_tokens():
0
- # try reading key and secret from ~/.fireeaglerc
0
- fn = os.path.expanduser("~/.fireeaglerc")
0
- if os.path.exists(fn):
0
- for line in open(fn).readlines():
0
- if p != -1: line = line[:p]
0
- k, v = line.split("=", 1)
0
- info[k.strip()] = v.strip()
0
- # if we couldn't find them from .fireeaglerc, ask
0
- if not info.has_key('consumer_key'): info['consumer_key'] = raw_input("Enter consumer key: ")
0
- if not info.has_key('consumer_secret'): info['consumer_secret'] = raw_input("Enter consumer secret: ")
0
- return (info['consumer_key'], info['consumer_secret'])
0
class FireEagleException( Exception ):
0
@@ -287,21 +266,63 @@ class FireEagleAccumulator:
0
- def __init__( self, consumer_key, consumer_secret ):
0
+ def __init__( self, rc_or_consumer_key, consumer_secret=None ):
0
+ syntax: FireEagle( os.path.expanduser( "~/.fireeaglerc" ) )
0
+ or FireEagle( CONSUMER_KEY, CONSUMER_SECRET )
0
# Prepare object lifetime variables
0
- self.consumer_key = consumer_key
0
- self.consumer_secret = consumer_secret
0
+ self.read_config( rc_or_consumer_key, consumer_secret )
0
self.oauth_consumer = oauth.OAuthConsumer(
0
self.signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
0
- self.http_connection = (API_PROTOCOL == 'https' and httplib.HTTPSConnection or httplib.HTTPConnection)( API_SERVER )
0
+ proto, host, port = re.search(r"^(https?)://([a-z\.0-9]+)(?:\:(\d+))?$", self.api_server).groups()
0
+ self.http_connection = (proto == 'https' and httplib.HTTPSConnection or httplib.HTTPConnection)( host, port )
0
# Prepare the accumulators for each method
0
for method, _ in FIREEAGLE_METHODS.items():
0
if not hasattr( self, method ):
0
setattr( self, method, FireEagleAccumulator( self, method ))
0
+ def read_config( self, rc_or_consumer_key, consumer_secret ):
0
+ if consumer_secret is None:
0
+ for line in open( rc_or_consumer_key ).readlines():
0
+ if p != -1: line = line[:p]
0
+ k, v = line.split("=", 1)
0
+ info[ k.strip() ] = v.strip()
0
+ 'consumer_key': rc_or_consumer_key,
0
+ 'consumer_secret': consumer_secret,
0
+ info.setdefault("api_server", API_SERVER)
0
+ info.setdefault("api_protocol", API_PROTOCOL)
0
+ self.api_server = self._build_server_url(info, 'api')
0
+ info.setdefault("auth_server", FE_SERVER)
0
+ info.setdefault("auth_protocol", FE_PROTOCOL)
0
+ self.auth_server = self._build_server_url(info, 'auth')
0
+ self.consumer_key, self.consumer_secret = info['consumer_key'], info['consumer_secret']
0
+ def _build_server_url( self, info, role ):
0
+ proto = info['%s_protocol' % role]
0
+ default_port = (proto == 'https') and 443 or 80
0
+ port = int(info.get('%s_port' % role, default_port))
0
+ info['%s_server' % role],
0
+ (port != default_port) and (':%d' % port) or '',
0
def fetch_response( self, http_method, url, \
0
body = None, headers = None ):
0
@@ -419,7 +440,7 @@ class FireEagle:
0
# (without a signature) and return it witout executing
0
if 'request_url' == meta['returns']:
0
- return meta['url_template'].substitute( method=method,
proto=FE_PROTOCOL, server=FE_SERVER, token=token.key )
0
+ return meta['url_template'].substitute( method=method,
server=self.auth_server, token=token.key )
0
# Build and sign the oauth_request
0
# NOTE: If ( token == None ), it's handled silently
0
@@ -428,7 +449,7 @@ class FireEagle:
0
http_method = meta['http_method'],
0
- http_url = meta['url_template'].substitute( method=method,
proto=API_PROTOCOL, server=API_SERVER ),
0
+ http_url = meta['url_template'].substitute( method=method,
server=self.api_server ),
0
oauth_request.sign_request(
Comments
No one has commented yet.