Commits

Yorgos Pagles committed ed17391

Critsend backend connects

Comments (0)

Files changed (4)

email_services/services/critsend.py

+import logging
+import random
+import datetime
+import hmac
+import hashlib
+
+from suds import WebFault
+from suds.client import Client
+from suds.transport.http import HttpTransport
+
+from email_services import settings
+
+logging.getLogger('suds.client').setLevel(logging.DEBUG)
+logging.getLogger('suds.transport').setLevel(logging.DEBUG)
+logging.getLogger('suds.xsd.schema').setLevel(logging.DEBUG)
+logging.getLogger('suds.wsdl').setLevel(logging.DEBUG)
+
 class CritsendEmailService(object):
     """
     The email service that supports the critsend backend. Besides performing
     the actual email sending it also provides statistics on email sending.
     """
-    
+
+    hosts = ['http://mail1.messaging-master.com',
+             'http://mail3.messaging-master.com',
+             'http://mail4.messaging-master.com',
+             'http://mail5.messaging-master.com',
+             'http://mail8.messaging-master.com',
+             'http://mail9.messaging-master.com',
+             'http://mail10.messaging-master.com',
+             'http://mail11.messaging-master.com',
+             'http://mail12.messaging-master.com',
+             'http://mail13.messaging-master.com',
+             'http://mail14.messaging-master.com',
+             'http://mail15.messaging-master.com',
+             'http://mail16.messaging-master.com']
+    wsdl = '/api_2.php?wsdl'
+
     def __init__(self, *args, **kwargs):
         """
         Initializes the Critsend email service. 
         """
-        pass
+        self.client = None
+        self.user = settings.EMAIL_SERVICES_CLIENT_ID
+        self.key = settings.EMAIL_SERVICES_CLIENT_KEY
 
+    def open(self):
+        """
+        Creates the client that will interact with the Critsend SOAP API
+        """
 
-    def open():
-        """
-        Opens the connection to the Critsend server
-        """
-        pass
+        if self.client:
+            return
 
-    def close():
+        t = HttpTransport(timeout=2400)
+        which = random.randint(0, len(self.hosts) - 1)
+        host = self.hosts[which]
+        self.client = Client(host + self.wsdl, transport=t)
+
+    def close(self):
         """
         Closes the connection to the Critsend server
         """
-        pass
+        self.client = None
 
     def send_messages(self, messages):
         """
         Sends the email messages using the critsend api
         """
         pass
+
+    @property
+    def credentials(self):
+        """
+        Generates the credentials for using the Critsend SOAP API
+        """
+        timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
+
+        msg = "http://mxmaster.net/campaign/0.1#doCampaign" + self.user + \
+              timestamp
+
+        signature = hmac.new(self.key, msg, hashlib.sha256)
+        return {'user': self.user,
+                'timestamp': timestamp,
+                'signature': signature.hexdigest()}
         

email_services/settings.py

+from django.conf import settings
+
+EMAIL_SERVICES_CLIENT_ID = getattr(settings, 'EMAIL_SERVICES_CLIENT_ID', "")
+EMAIL_SERVICES_CLIENT_KEY = getattr(settings, 'EMAIL_SERVICES_CLIENT_KEY', "")

email_services/tests.py

 """
 Tests for the email_services django application
 """
+import urlparse
 
 import mock
 from django.test import TestCase
+
 from email_services.backends import CritsendEmailBackend
 from email_services.services import CritsendEmailService
+from email_services import settings
+
 
 class CritsendBackendTest(TestCase):
 
 class  CritsendServiceTest(TestCase):
 
     def setUp(self):
+        self.fake_user = "QWERTY"
+        self.fake_key = "QWERTY12345"
+        self.default_user = settings.EMAIL_SERVICES_CLIENT_ID
+        self.default_key = settings.EMAIL_SERVICES_CLIENT_KEY
+        settings.EMAIL_SERVICES_CLIENT_ID = self.fake_user
+        settings.EMAIL_SERVICES_CLIENT_KEY = self.fake_key
         self.service = CritsendEmailService()
-#        self.service.connection = 
+        
+    def tearDown(self):
+        settings.EMAIL_SERVICES_CLIENT_ID = self.default_user
+        settings.EMAIL_SERVICES_CLIENT_KEY = self.default_key
+        
+    def test_creadentials(self):
+        self.assertEquals(self.service.credentials['user'],
+                          settings.EMAIL_SERVICES_CLIENT_ID)
+        self.assertTrue(self.service.credentials['timestamp'])
+        self.assertTrue(self.service.credentials['signature'])
+        
+    def test_connection_open(self):
+        self.service.open()
+        u = urlparse.urlparse(self.service.client.wsdl.url)
+        self.assertTrue("%s://%s" % (u.scheme, u.netloc) in self.service.hosts)
+        self.assertEquals(u.path, self.service.wsdl.split('?')[0])
+        self.assertEquals(u.query, self.service.wsdl.split('?')[1])
+
+        # Check that it is not recreated
+        client = self.service.client
+        self.service.open()
+        self.assertTrue(client is self.service.client)
+        
+    def test_connection_close(self):
+        self.service.close()
+        self.assertEquals(self.service.client, None)
+
       packages=find_packages(exclude=['test_project']),
       include_package_data=True,
       zip_safe=False,
-      install_requires=[])
+      install_requires=['suds',])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.