<commit_before>
# influenced by https://github.com/JeremyJones/dotmailer-client/blob/master/dotmailer.py
# dotMailer docs at http://www.dotmailer.co.uk/api/

import urllib
import urllib2
import sys

import base64, time
from datetime import datetime, timedelta

from urlparse import urlparse
import suds
from suds.client  import Client as SOAPClient

__version__ = '0.1.1'

try:
    import simplejson as json
except ImportError:
    import json # fall back to traditional json module. 
    
import logging
logger = logging.getLogger(__name__)


class PyDotMailer(object):
    version = '0.1'
    
    # Cache the information on the API location on the server
    api_url = ''
    
    # Default to a 300 second timeout on server calls. Not used at present. 
    timeout = 300
    
    def __init__(self, api_username='', api_password='', secure=True, timeout=300):
        '''
        Connect to the dotMailer API for a given list.
        
        @param string $apikey Your dotMailer apikey
        @param string $secure Whether or not this should use a secure connection
        '''
        
        # Default to a 300 second timeout on server calls. Not used at present. TODO - implement.  
        self.timeout = timeout
        
        # Define whether we use SSL
        self.secure = secure or False
        if secure:
            self.api_url = 'https://apiconnector.com/API.asmx?WSDL'
        else:
            self.api_url = 'http://apiconnector.com/API.asmx?WSDL'
            
        self.client = SOAPClient(self.api_url)
        self.api_username = api_username
        self.api_password = api_password
 
 
    def add_contacts_to_address_book(self, address_book_id, s_contacts, wait_to_complete_seconds = False):
        """
        Add a list of contacts to the address book
        
        @param int the id of the address book
        @param string containing the contacts to be added. You may upload either a .csv or .xls file. It must contain one column with the heading "Email". Other columns must will attempt to map to your custom data fields.
        @param  seconds to wait.  
        @return dict  e.g. {'progress_id': 15edf1c4-ce5f-42e3-b182-3b20c880bcf8, 'ok': True, 'result': Finished}
        
        http://www.dotmailer.co.uk/api/address_books/add_contacts_to_address_book_with_progress.aspx
        """
        return_code=None
        base64_data  = base64.b64encode(s_contacts)
    
        progress_id  = self.client.service.AddContactsToAddressBookWithProgress(username=self.api_username, password=self.api_password, 
                                                                                addressbookID=address_book_id, data=base64_data, dataType='CSV')
        dict_result = {'ok':True}
        if wait_to_complete_seconds:<commit_msg>added error trapping on empty username/password<commit_after>
# influenced by https://github.com/JeremyJones/dotmailer-client/blob/master/dotmailer.py
# dotMailer docs at http://www.dotmailer.co.uk/api/

import urllib
import urllib2
import sys

import base64, time
from datetime import datetime, timedelta

from urlparse import urlparse
import suds
from suds.client  import Client as SOAPClient

__version__ = '0.1.1'

try:
    import simplejson as json
except ImportError:
    import json # fall back to traditional json module. 
    
import logging
logger = logging.getLogger(__name__)


class PyDotMailer(object):
    version = '0.1'
    
    # Cache the information on the API location on the server
    api_url = ''
    
    # Default to a 300 second timeout on server calls. Not used at present. 
    timeout = 300
    
    def __init__(self, api_username='', api_password='', secure=True, timeout=300):
        '''
        Connect to the dotMailer API for a given list.
        
        @param string $apikey Your dotMailer apikey
        @param string $secure Whether or not this should use a secure connection
        '''
        
        # Default to a 300 second timeout on server calls. Not used at present. TODO - implement.  
        self.timeout = timeout
        
        # Define whether we use SSL
        self.secure = secure or False
        if secure:
            self.api_url = 'https://apiconnector.com/API.asmx?WSDL'
        else:
            self.api_url = 'http://apiconnector.com/API.asmx?WSDL'
            
        self.client = SOAPClient(self.api_url)
        self.api_username = api_username
        self.api_password = <PASSWORD>
        if (not api_username) or (not api_password):
            raise Exception('Bad username or password')
 
 
    def add_contacts_to_address_book(self, address_book_id, s_contacts, wait_to_complete_seconds = False):
        """
        Add a list of contacts to the address book
        
        @param int the id of the address book
        @param string containing the contacts to be added. You may upload either a .csv or .xls file. It must contain one column with the heading "Email". Other columns must will attempt to map to your custom data fields.
        @param  seconds to wait.  
        @return dict  e.g. {'progress_id': 15edf1c4-ce5f-42e3-b182-3b20c880bcf8, 'ok': True, 'result': Finished}
        
        http://www.dotmailer.co.uk/api/address_books/add_contacts_to_address_book_with_progress.aspx
        """
        return_code=None
        base64_data  = base64.b64encode(s_contacts)
    
        progress_id  = self.client.service.AddContactsToAddressBookWithProgress(username=self.api_username, password=self.api_password, 
                                                                                addressbookID=address_book_id, data=base64_data, dataType='CSV')
        dict_result = {'ok':True}
        if wait_to_complete_seconds:
{'fraction_non_alphanumeric': 0.07564349500963054, 'fraction_numerical': 0.016109262826125022, 'mean_word_length': 2.440963855421687, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '55734', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1632, 'n_words': 505}