diff --git a/marketo/__init__.py b/pymarketo/__init__.py similarity index 100% rename from marketo/__init__.py rename to pymarketo/__init__.py diff --git a/marketo/client.py b/pymarketo/client.py similarity index 77% rename from marketo/client.py rename to pymarketo/client.py index 7436de9..93dedbe 100644 --- a/marketo/client.py +++ b/pymarketo/client.py @@ -48,31 +48,45 @@ def patchEnvelope(envelope): # TODO: Get patch accepted in SUDS or rewrite to use DOM level code class MarketoSignaturePlugin(MessagePlugin): authfragment = """ - %(userid)s %(signature)s %(timestamp)s - """ def __init__(self, userid, secret, *args, **kwargs): self.userid = userid self.secret = secret self.debug = kwargs.get("debug", True) - def sending(self, context): + + def marshalled(self, context): userid = self.userid timestamp = rfc3339(datetime.datetime.now()) secret = self.secret signature = sign(secret, timestamp+userid) auth = self.authfragment % locals() envelope = context.envelope - envelope = envelope.replace("", auth) - envelope = patchEnvelope(envelope) + + #Set the right ns prefixes + envelope.nsprefixes[ 'ns1' ] = envelope.nsprefixes[ 'ns0' ] + envelope.clearPrefix( 'ns0' ) + + #Add our auth to the header element + header = envelope.getChild('Header') + authns = Element( 'ns1:AuthenticationHeader' ) + authns.append( Element( 'mktowsUserId' ).setText(self.userid) ) + authns.append( Element( 'requestSignature' ).setText(signature) ) + authns.append( Element( 'requestTimestamp' ).setText(timestamp) ) + header.append( authns ) + + #Set the proper body prefixes + body = envelope.getChild( 'Body' ) + body.prefix = 'SOAP-ENV' + body.children[0].prefix = 'ns1' + if self.debug: - with open("/tmp/envelope.txt","w") as f: f.write(envelope) - context.envelope = envelope - return context + with open("/tmp/envelope.txt","w") as f: f.write(envelope.str()) + def MarketoClientFactory(ini, **kwargs): import json @@ -80,18 +94,23 @@ def MarketoClientFactory(ini, **kwargs): ini = json.loads(f.read()) for key, item in ini.items(): ini[key] = str(item) + ini.update(kwargs) wsdl = ini["wsdl"] + + cache = None if '://' not in wsdl: if os.path.isfile(wsdl): wsdl = 'file://' + os.path.abspath(wsdl) - cache = None # TODO: Evaluate using cache - client = Client(wsdl, cache=cache, plugins=[MarketoSignaturePlugin(ini["userid"],ini["encryption_key"])]) + client = Client(wsdl, location=ini['endpoint'], cache=cache, plugins=[MarketoSignaturePlugin(ini["userid"],ini["encryption_key"])]) + headers = {} client.set_options(headers=headers) + if kwargs.has_key('proxy'): if kwargs['proxy'].has_key('https'): raise NotImplementedError('Connecting to a proxy over HTTPS not supported.') client.set_options(proxy=kwargs['proxy']) + return client diff --git a/marketo/rfc3339.py b/pymarketo/rfc3339.py similarity index 100% rename from marketo/rfc3339.py rename to pymarketo/rfc3339.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fd69de1 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +suds==0.4 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..dd74cfa --- /dev/null +++ b/setup.py @@ -0,0 +1,13 @@ +from setuptools import setup + +setup( + name='pymarketo', + author='WebReply', + author_email='', + maintainer='Scott Griffin', + maintainer_email='scott@loggly.net', + version='0.1', + packages=['pymarketo', 'tests'], + long_description=open( 'README.rst' ).read(), + install_requires=open( 'requirements.txt' ).read().split() + ) diff --git a/tests/simpletests.py b/tests/simpletests.py index 3099e64..54051cd 100644 --- a/tests/simpletests.py +++ b/tests/simpletests.py @@ -1,4 +1,4 @@ -from marketo.client import MarketoClientFactory +from pymarketo.client import MarketoClientFactory import os import sys #@UnusedImport import time #@UnusedImport