2009-04-06 22:23:18 +00:00
#!/usr/bin/env python
import getpass
import sys
from optparse import OptionParser
try :
from ZSI . client import AUTH
except ImportError :
print ' ==> Failed to import ZSI '
print ' ===> Please make sure you have it installed and locatable from PYTHONPATH '
print ' http://pywebsvcs.sourceforge.net/ '
print
2009-04-08 02:21:51 +00:00
raise
2009-04-06 22:23:18 +00:00
class GetTokenError ( Exception ) :
pass
Add a quick ReverseGeocode test to verify that we're getting proper Address objects back from the API.
Sample run output:
tyler@grapefruit:~/source/github/pyvirtualearth> VE_USER=XXXXX VE_PASSWORD=XXXX python tests/ReverseGeocode.py
[<Address at 0xc92990> confidence=Medium, entityType=Address, displayName=Infinite Loop, Cupertino, California 95014, United States, address={'AddressLine': 'Infinite Loop', 'District': '', 'Locality': 'Cupertino', 'PostalTown': '', 'FormattedAddress': 'Infinite Loop, Cupertino, California 95014, United States', 'PostalCode': '95014', 'AdminDistrict': 'California', 'CountryRegion': 'United States'}
, <Address at 0xc929d0> confidence=Medium, entityType=Address, displayName=1 Infinite Loop, Cupertino, CA 95014-2084, address={'AddressLine': '1 Infinite Loop', 'District': '', 'Locality': 'Cupertino', 'PostalTown': '', 'FormattedAddress': '1 Infinite Loop, Cupertino, CA 95014-2084', 'PostalCode': '95014-2084', 'AdminDistrict': 'CA', 'CountryRegion': 'United States'}
]
.
----------------------------------------------------------------------
Ran 1 test in 0.734s
OK
tyler@grapefruit:~/source/github/pyvirtualearth>
Signed-off-by: R. Tyler Ballance <tyler@slide.com>
2009-04-06 23:49:35 +00:00
class OptionsProxy ( object ) :
def __init__ ( self , * args , * * kwargs ) :
self . __dict__ . update ( kwargs )
2009-04-06 22:23:18 +00:00
def getToken ( opts , password ) :
if not opts . production :
from staging import CommonService_client
from staging import CommonService_types
else :
from production import CommonService_client
from production import CommonService_types
locator = CommonService_client . CommonServiceLocator ( )
service = locator . getCommonServiceSoap ( auth = ( 4 , opts . user , password ) )
request = CommonService_client . GetClientTokenSoapIn ( )
request . _request = CommonService_types . ns0 . GetClientToken_Dec ( )
request . _specification = CommonService_types . ns0 . TokenSpecification_Def ( opts . user )
request . _specification . _ClientIPAddress = opts . clientip
request . _specification . _TokenValidityDurationMinutes = int ( opts . tokenvalidity )
result = service . GetClientToken ( request )
if result . __class__ . __name__ == ' GetClientTokenResponse_Holder ' :
return result . _GetClientTokenResult
raise GetTokenError ( ' Web service failed to return a proper response! %s ' % result )
def main ( ) :
_op = OptionParser ( )
_op . add_option ( ' --user ' , dest = ' user ' , help = ' User ID for Virtual Earth \' s Web Services ' )
_op . add_option ( ' --production ' , dest = ' production ' , action = ' store_true ' , help = ' Generate a token for production use ' )
_op . add_option ( ' --clientip ' , dest = ' clientip ' , default = ' 0.0.0.0 ' , help = ' Specify the ClientIPAddress argument for the CommonService.GetClientToken() API ' )
_op . add_option ( ' --tokenvalidity ' , dest = ' tokenvalidity ' , default = ' 480 ' , help = ' Specify to TokenValidityDurationInMinutes argument for CommonService.GetClientToken(), must be between 15 and 480 ' )
_op . add_option ( ' --password ' , dest = ' password ' , default = None , help = ' Password for Virtual Earth \' s Web Services ' )
opts , args = _op . parse_args ( )
if not opts . user :
print ' ==> Missing " user " argument '
print
_op . print_help ( )
return - 1
password = opts . password
if not opts . password :
password = getpass . getpass ( prompt = ' Virtual Earth Password: ' )
token = getToken ( opts , password )
print ' ==> Generated token: %s ' % token
return 0
if __name__ == ' __main__ ' :
rc = main ( )
sys . exit ( rc )
2009-04-07 23:13:20 +00:00
# vim: set expandtab: