Package pamfax
[hide private]
[frames] | no frames]

Source Code for Package pamfax

  1  """ 
  2  This module implements the PamFax API. It has been based heavily off of 
  3  Tropo's pamfaxr and tropo-webapi-python projects on GitHub. 
  4   
  5  See the following link for more details on the PamFax API: 
  6   
  7  http://www.pamfax.biz/en/extensions/developers/ 
  8   
  9  NOTE: This module has only been tested with python 2.7. 
 10  """ 
 11   
 12  from httplib import HTTPSConnection 
 13  from urllib import urlencode 
 14   
 15  from processors import Common, FaxHistory, FaxJob, NumberInfo, OnlineStorage, Session, Shopping, UserInfo, _get, _get_url 
 16   
 17  import time 
 18  import types 
 19   
20 -class PamFax:
21 """Class encapsulating the PamFax API. Actions related to the sending of faxes are called on objects of this class. 22 For example, the 'create' action resides in the FaxJob class, but you can just use the following 'shortcut' logic: 23 24 from pamfax import PamFax 25 p = PamFax(<args>) 26 p.create() 27 28 """ 29
30 - def __init__(self, username, password, host='api.pamfax.biz', apikey='', apisecret=''):
31 """Creates an instance of the PamFax class and initiates an HTTPS session.""" 32 print "Connecting to %s" % host 33 http = HTTPSConnection(host) 34 api_credentials = '?%s' % urlencode({'apikey': apikey, 'apisecret': apisecret, 'apioutputformat': 'API_FORMAT_JSON'}) 35 usertoken = self.get_user_token(http, api_credentials, username, password) 36 api_credentials = '%s&%s' % (api_credentials, urlencode({'usertoken': usertoken})) 37 common = Common(api_credentials, http) 38 fax_history = FaxHistory(api_credentials, http) 39 fax_job = FaxJob(api_credentials, http) 40 number_info = NumberInfo(api_credentials, http) 41 online_storage = OnlineStorage(api_credentials, http) 42 session = Session(api_credentials, http) 43 shopping = Shopping(api_credentials, http) 44 user_info = UserInfo(api_credentials, http) 45 attrs = dir(self) 46 for processor in (session, common, fax_history, fax_job, number_info, online_storage, shopping, user_info): 47 for attr_key in dir(processor): 48 if attr_key not in attrs: 49 attr_value = getattr(processor, attr_key) 50 if isinstance(attr_value, types.MethodType): 51 setattr(self, attr_key, attr_value)
52
53 - def verify_user(self, http, api_credentials, username, password):
54 """Verifies a user via username/password""" 55 url = _get_url('/Session', 'VerifyUser', api_credentials, username=username, password=password) 56 return _get(http, url)
57
58 - def get_user_token(self, http, api_credentials, username, password):
59 """Gets the user token to use with subsequent requests.""" 60 result = self.verify_user(http, api_credentials, username, password) 61 if result['result']['code'] == 'success': 62 return result['UserToken']['token'] 63 else: 64 raise Exception(result['result']['message'])
65 66 # ------------------------------------------------------------------------ 67 # Convenient helper methods 68 # ------------------------------------------------------------------------ 69
70 - def get_state(self, blocking=False, interval=1):
71 """Obtains the state of the FaxJob build, may block until a state is received, or just return immediately""" 72 if blocking: 73 state = None 74 result = None 75 while state is None: 76 result = self.get_fax_state() 77 time.sleep(interval) 78 return result 79 else: 80 return self.get_fax_state()
81
82 - def is_converting(self, fax_state):
83 """Returns whether or not a file in the fax job is still in a converting state.""" 84 converting = False 85 files = fax_state['Files'] 86 if 'content' in files: 87 for file in files['content']: 88 state = file['state'] 89 if state == '' or state == 'converting': 90 converting = True 91 return converting
92 93 if __name__ == '__main__': 94 print """ 95 96 This is the Python implementation of the PamFax API. 97 98 To run the test suite, please run: 99 100 cd test 101 python test.py 102 103 """ 104