/
pyresponse.py
96 lines (77 loc) · 3.81 KB
/
pyresponse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
# coding=utf-8
#
from lib.core import Core
from lib.translator import Translator
from lib.helpers import Helpers
from suds.client import Client as Suds
import types
class PureResponseClient(object):
version = '1.2.8'
api_client = None
api_account_level = None
api_core = None
api_translator = None
encoding_exceptions = [Core.Entity.ID,
Core.Message.ID,
Core.List.ID,
Core.Api.USERNAME,
Core.Api.PASSWORD]
class AccountLevel:
LITE = 10
PRO = 20
EXPERT = 40
def __init__(self, api_version=Core.Api.RPC_LITERAL_UNBRANDED):
self.api_client = Suds(api_version)
self.api_translator = Translator(self)
self.api_core = Core(self)
self.register_helpers()
def authenticate(self, api_username=None, api_password=None, api_account_level=AccountLevel.LITE):
"""
Authenticates a username and password against the PureResponse API,
must be done before making all other API calls.
------------------------------------------------
@param api_username - username of account from which to make api calls
@param api_password - password of that same account
@param [api_account_level] - your api account level indicates how many custom
fields you are allowed to use in contact lists
if you indicate a level higher than that of your
account a pyresponse.Core.StoreErrors will be raised
when your actual limit is met
going over the limit indicated by this parameter will
raise a pyresponse.Translator.AccountLevelError in
those same cases
pyresponse.AccountLevel.LITE offers 10 custom fields
pyresponse.AccountLevel.PRO offers 20 custom fields
pyresponse.AccountLevel.EXPERT offers 40 custom fields
@return - bean id for a context bean in pureresponse, will be
used to identify the API session
"""
self.api_account_level = api_account_level
if (not api_username) or (not api_password):
message = ('Invalid authentication details: api_username=%s, api_password=%s' %
(api_username, api_password))
raise Core.AuthenticationException(message)
return self.api_core.authenticate(api_username, api_password)
def invalidate(self):
"""
Clears authentication credentials, makes a logout call
to PureResponse. Follow-on calls will fail before calling
as a result of lacking an api context id (Core.context_id).
------------------------------------------------
"""
self.api_core.invalidate()
def register_helpers(self):
"""
Gives access to classes and methods from ./lib/helpers.py and
./lib/core.py from pyresponse.py, making the code a litle easier
to maintain. Separates public facing interfaces from those mainly
intended for internal use.
------------------------------------------------
"""
for key, value in Helpers.__dict__.iteritems():
if callable(value):
setattr(self, key, types.MethodType(value, self))
for key, value in Core.__dict__.iteritems():
if isinstance(value, (type, types.ClassType)):
setattr(self, key, value)