Skip to content
This repository was archived by the owner on Jan 20, 2024. It is now read-only.

Commit 7f8d58b

Browse files
committed
added new options for loading configs
1 parent 1c19eda commit 7f8d58b

File tree

3 files changed

+79
-26
lines changed

3 files changed

+79
-26
lines changed

FuelSDK/client.py

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313
from FuelSDK.objects import ET_DataExtension,ET_Subscriber
1414

15-
########
16-
##
17-
## Setup web service connectivity by getting need config data, security tokens etc.
18-
##
19-
########
15+
2016
class ET_Client(object):
17+
"""
18+
Setup web service connectivity by getting need config data, security tokens etc.
19+
"""
20+
2121
debug = False
2222
client_id = None
2323
client_secret = None
@@ -35,7 +35,7 @@ class ET_Client(object):
3535
## get_server_wsdl - if True and a newer WSDL is on the server than the local filesystem retrieve it
3636
def __init__(self, get_server_wsdl = False, debug = False, params = None):
3737
self.debug = debug
38-
if(debug):
38+
if debug:
3939
logging.basicConfig(level=logging.INFO)
4040
logging.getLogger('suds.client').setLevel(logging.DEBUG)
4141
logging.getLogger('suds.transport').setLevel(logging.DEBUG)
@@ -44,12 +44,39 @@ def __init__(self, get_server_wsdl = False, debug = False, params = None):
4444

4545
## Read the config information out of config.python
4646
config = ConfigParser.RawConfigParser()
47-
config.read('config.python')
48-
self.client_id = config.get('Web Services', 'clientid')
49-
self.client_secret = config.get('Web Services', 'clientsecret')
50-
self.appsignature = config.get('Web Services', 'appsignature')
51-
wsdl_server_url = config.get('Web Services', 'defaultwsdl')
52-
self.auth_url = config.get('Web Services', 'authenticationurl')
47+
if os.path.exists(os.path.expanduser('~/.fuelsdk/config.python')):
48+
config.read(os.path.expanduser('~/.fuelsdk/config.python'))
49+
else:
50+
config.read('config.python')
51+
52+
if config.has_option('Web Services', 'clientid'):
53+
self.client_id = config.get('Web Services', 'clientid')
54+
elif 'FUELSDK_CLIENT_ID' in os.environ:
55+
self.client_id = os.environ['FUELSDK_CLIENT_ID']
56+
57+
if config.has_option('Web Services', 'clientsecret'):
58+
self.client_secret = config.get('Web Services', 'clientsecret')
59+
elif 'FUELSDK_CLIENT_SECRET' in os.environ:
60+
self.client_secret = os.environ['FUELSDK_CLIENT_SECRET']
61+
62+
if config.has_option('Web Services', 'appsignature'):
63+
self.appsignature = config.get('Web Services', 'appsignature')
64+
elif 'FUELSDK_APP_SIGNATURE' in os.environ:
65+
self.appsignature = os.environ['FUELSDK_APP_SIGNATURE']
66+
67+
if config.has_option('Web Services', 'defaultwsdl'):
68+
wsdl_server_url = config.get('Web Services', 'defaultwsdl')
69+
elif 'FUELSDK_DEFAULT_WSDL' in os.environ:
70+
wsdl_server_url = os.environ['FUELSDK_DEFAULT_WSDL']
71+
else:
72+
wsdl_server_url = 'https://webservice.exacttarget.com/etframework.wsdl'
73+
74+
if config.has_option('Web Services', 'authenticationurl'):
75+
self.auth_url = config.get('Web Services', 'authenticationurl')
76+
elif 'FUELSDK_AUTH_URL' in os.environ:
77+
self.auth_url = os.environ['FUELSDK_AUTH_URL']
78+
else:
79+
self.auth_url = 'https://auth.exacttargetapis.com/v1/requestToken?legacy=1'
5380

5481
self.wsdl_file_url = self.load_wsdl(wsdl_server_url, get_server_wsdl)
5582

@@ -67,10 +94,12 @@ def __init__(self, get_server_wsdl = False, debug = False, params = None):
6794
self.refresh_token()
6895

6996

70-
## retrieve the url of the ExactTarget wsdl...either file: or http:
71-
## depending on if it already exists locally or server flag is set and
72-
## server has a newer copy
7397
def load_wsdl(self, wsdl_url, get_server_wsdl = False):
98+
"""
99+
retrieve the url of the ExactTarget wsdl...either file: or http:
100+
depending on if it already exists locally or server flag is set and
101+
server has a newer copy
102+
"""
74103
path = os.path.dirname(os.path.abspath(__file__))
75104
file_location = os.path.join(path, 'ExactTargetWSDL.xml')
76105
file_url = 'file:///' + file_location
@@ -87,12 +116,16 @@ def load_wsdl(self, wsdl_url, get_server_wsdl = False):
87116

88117
return file_url
89118

90-
### get the WSDL from the server and save it locally
119+
91120
def retrieve_server_wsdl(self, wsdl_url, file_location):
121+
"""
122+
get the WSDL from the server and save it locally
123+
"""
92124
r = requests.get(wsdl_url)
93125
f = open(file_location, 'w')
94126
f.write(r.text)
95127

128+
96129
def build_soap_client(self):
97130
if self.endpoint is None:
98131
self.endpoint = self.determineStack()
@@ -113,8 +146,11 @@ def build_soap_client(self):
113146
security.tokens.append(token)
114147
self.soap_client.set_options(wsse=security)
115148

116-
## Called from many different places right before executing a SOAP call
149+
117150
def refresh_token(self, force_refresh = False):
151+
"""
152+
Called from many different places right before executing a SOAP call
153+
"""
118154
#If we don't already have a token or the token expires within 5 min(300 seconds), get one
119155
if (force_refresh or self.authToken is None or (self.authTokenExpiration is not None and time.time() + 300 > self.authTokenExpiration)):
120156
headers = {'content-type' : 'application/json'}
@@ -139,8 +175,11 @@ def refresh_token(self, force_refresh = False):
139175

140176
self.build_soap_client()
141177

142-
##find the correct url that data request web calls should go against for the token we have.
178+
143179
def determineStack(self):
180+
"""
181+
find the correct url that data request web calls should go against for the token we have.
182+
"""
144183
try:
145184
r = requests.get('https://www.exacttargetapis.com/platform/v1/endpoints/soap?access_token=' + self.authToken)
146185
contextResponse = r.json()
@@ -150,8 +189,11 @@ def determineStack(self):
150189
except Exception as e:
151190
raise Exception('Unable to determine stack using /platform/v1/tokenContext: ' + e.message)
152191

153-
##add or update a subscriber with a list
192+
154193
def AddSubscriberToList(self, emailAddress, listIDs, subscriberKey = None):
194+
"""
195+
add or update a subscriber with a list
196+
"""
155197
newSub = ET_Subscriber()
156198
newSub.auth_stub = self
157199
lists = []
@@ -175,8 +217,11 @@ def AddSubscriberToList(self, emailAddress, listIDs, subscriberKey = None):
175217

176218
return postResponse
177219

178-
##write the data extension props to the web service
220+
179221
def CreateDataExtensions(self, dataExtensionDefinitions):
222+
"""
223+
write the data extension props to the web service
224+
"""
180225
newDEs = ET_DataExtension()
181226
newDEs.auth_stub = self
182227

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@ pip install FuelSDK
1818

1919
### Configuring
2020

21-
After downloading the project, rename the `config.python.template` file to `config.python`.
22-
23-
Edit `config.python` so you can input the ClientID and Client Secret values provided when you registered your application. If you are building a HubExchange application for the Interactive Marketing Hub then, you must also provide the Application Signature (appsignature).
24-
The defaultwsdl configuration must be [changed depending on the ExactTarget service](https://code.exacttarget.com/question/there-any-cetificrate-install-our-server-access-et-api "ExactTarget Forum").
25-
The authenticationurl must also be [changed depending on service](https://code.exacttarget.com/question/not-able-create-accesstoken-when-clientidsecret-associated-preproduction-account "ExactTarget Forum").
21+
There are two ways to configure your access tokens and details for the Fuel SDK.
22+
23+
1. Copy the included `config.python.template` file to `config.python` in either `~/.fuelsdk/` or within this python module.
24+
2. Add environment variables:
25+
* `FUELSDK_CLIENT_ID` (required)
26+
* `FUELSDK_CLIENT_SECRET` (required)
27+
* `FUELSDK_APP_SIGNATURE`
28+
* `FUELSDK_DEFAULT_WSDL`
29+
* `FUELSDK_AUTH_URL`
30+
31+
Edit `config.python` or declare environment variables so you can input the ClientID and Client Secret values provided when you registered your application. If you are building a HubExchange application for the Interactive Marketing Hub then, you must also provide the Application Signature (`appsignature` / `FUELSDK_APP_SIGNATURE`).
32+
The `defaultwsdl` / `FUELSDK_DEFAULT_WSDL` configuration must be [changed depending on the ExactTarget service](https://code.exacttarget.com/question/there-any-cetificrate-install-our-server-access-et-api "ExactTarget Forum").
33+
The `authenticationurl` / `FUELSDK_AUTH_URL` must also be [changed depending on service](https://code.exacttarget.com/question/not-able-create-accesstoken-when-clientidsecret-associated-preproduction-account "ExactTarget Forum").
2634

2735
If you have not registered your application or you need to lookup your Application Key or Application Signature values, please go to App Center at [Code@: ExactTarget's Developer Community](http://code.exacttarget.com/appcenter "Code@ App Center").
2836

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
url='https://github.com/ExactTarget/FuelSDK-Python',
1616
license='MIT',
1717
install_requires=[
18-
'pyjwt==0.1.8',
18+
'pyjwt==0.1.9',
1919
'requests==2.2.1',
2020
'suds==0.4',
2121
],

0 commit comments

Comments
 (0)