Snippets

jaehyeon-kim class_example

Created by syd-ano NA last modified
from soap import Soap # some SOAP service related methods are defined in soap.py

class Advertiser:
    '''
    classdocs
    '''

    def __init__(self, pid, name, vertical, useConv):
        '''
        Constructor
        '''
        self.id = pid
        self.name = name
        self.vertical = vertical
        self.useConv = useConv
        
    def __repr__(self):
        return "id: %s|name: %s|use conv: %s" % (self.id, self.name, self.useConv)
    
    def __str__(self):
        return "id: %s|name: %s|use conv: %s" % (self.id, self.name, self.useConv)
    
    @staticmethod
    def GetItemRes(wurl, auth, pageIndex, pageSize, showExtInfo=True):
        client = Soap.SetupClient(wurl, auth, toAddToken=True, toImportMsgSrc=True, toImportArrSrc=False)
        # update paging info
        paging = client.factory.create('ns1:ListPaging')
        paging['PageIndex'] = pageIndex
        paging['PageSize'] = pageSize
        # update filter array - empty
        filterArrary = client.factory.create('ns0:ArrayOfAdvertiserServiceFilter')
        # get response
        response = client.service.GetAdvertisers(filterArrary, paging, showExtInfo)
        return response
    
    @staticmethod
    def GetItem(response):
        objList = []
        for r in response[1]['Advertisers']['AdvertiserInfo']:
            obj = Advertiser(r['ID'], r['AdvertiserName'], r['Vertical'], r['AdvertiserExtendedInfo']['UsesConversionTags'])
            objList.append(obj)
        return objList
    
    @staticmethod
    def GetItemPgn(wurl, auth, pageIndex, pageSize, showExtInfo=True):
        objList = []
        cond = True
        while cond:
            response = Advertiser.GetItemRes(wurl, auth, pageIndex, pageSize, showExtInfo)
            objList = objList + Advertiser.GetItem(response)
            Soap.ShowProgress(response[1]['TotalCount'], len(objList), pageIndex, pageSize)
            if len(objList) < response[1]['TotalCount']:
                pageIndex += 1
            else:
                cond = False
        return objList
            
    @staticmethod
    def GetFilter(objList):
        return [[obj.id] for obj in objList if obj.userConv == True]
from suds.client import Client

class Auth:
    '''
    classdocs
    '''

    def __init__(self, username, password, appkey, url):
        self.username = username
        self.password = password
        self.appkey = appkey
        self.url = url
        
        client = Client(url, faults = False)
        response = client.service.ClientLogin(username, password, appkey)
        
        self.code = response[0]
        if response[0] == 200:
            self.token = response[1]
        else:
            self.token = None
        
    def getStatus(self):
        return self.code
from helper import Helper # some helper methods are defined in helper.py
from uthentication import Auth # authentication object is defined in authentication.py
from advertiser import Advertiser # advertiser object/methods are defined in advertiser.py
import logging # python standard library

logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)

authWSDL = 'https://platform.mediamind.com/Eyeblaster.MediaMind.API/AuthenticationService.svc?wsdl'
username = 'user name'
password = 'password'
appkey = 'application key'

advertiserWSDL = 'https://platform.mediamind.com/Eyeblaster.MediaMind.API/AdvertiserService.svc?wsdl'

## authenticate - authentication token is obtained in the constructor
auth = Auth(username, password, appkey, authWSDL)

## get advertisers - defined methods are executed
advRes = Advertiser.GetItemRes(advertiserWSDL, auth, pageIndex=0, pageSize=50, showExtInfo=True)
advList = Advertiser.GetItem(advRes)
Helper.PrintObjects(advList)
class Helper:
    '''
    classdocs
    '''

    def __init__(self, params):
        '''
        Constructor
        '''
        
    @staticmethod
    def PrintObjects(objList):
        for obj in objList:
            print obj
        print "No of objs: %s" % len(objList)
from suds.client import Client
from suds.sax.element import Element
from suds.xsd.doctor import Import, ImportDoctor

class Soap:
    '''
    classdocs
    '''

    def __init__(self):
        '''
        Constructor
        '''
    @staticmethod
    def SetupClient(wurl, auth, toAddToken = True, toImportMsgSrc = True, toImportArrSrc = False):
        if toImportMsgSrc:
            # update WSDL (xsd)
            msgImport = Import('http://api.eyeblaster.com/message')
            if toImportArrSrc:
                arrImport = Import('http://schemas.microsoft.com/2003/10/Serialization/Arrays')
                doctor = ImportDoctor(msgImport, arrImport)
            else:            
                doctor = ImportDoctor(msgImport)
            client = Client(wurl, doctor = doctor, faults = False)
        else:
            client = Client(wurl, faults = False)
        
        if toAddToken:
            # update token into SOAP header
            header = ('ns1', 'http://api.eyeblaster.com/message')
            element = Element('UserSecurityToken', ns=header).setText(auth.token)
            client.set_options(soapheaders=element)
        return client
    
    @staticmethod
    def ShowProgress(total, current, pageIndex, pageSize):
        print "Total No: %s, Current No: %s, page index: %s, page size: %s" % (total, current, pageIndex, pageSize)

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.