#####################################################################################
#
#  Copyright (c) Microsoft Corporation. All rights reserved.
#
# This source code is subject to terms and conditions of the Microsoft Public License. A 
# copy of the license can be found in the License.html file at the root of this distribution. If 
# you cannot locate the  Microsoft Public License, please send an email to 
# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
# by the terms of the Microsoft Public License.
#
# You must not remove this notice, or any other, from this software.
#
#
#####################################################################################

# sample calling Amazon WSDL-based web service

# requires subscription id from amazon.com
#from sys import argv
#if len(argv)==1:
    #print "This sample needs an Amazon.com subscription ID passed to it from the command line!"
    #from sys import exit
    #exit(1)
#else:
    #subscriptionId = argv[1]
#

subscriptionId = '1H9C60WQFVHY75DPGWG2'
searchString = 'Python Programming'

if subscriptionId is None:
    raise RuntimeError, 'Amazon subscription id is required to run this sample'

import clr
clr.AddReference("DynamicWebServiceHelpers")
from DynamicWebServiceHelpers import *

class AmazonService:
    def __init__(self, key):
        self.ws = WebService.Load('http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl')
        self.key = key
        
    def SearchBooks(self, search):
        # print 'composing data for web service request'
        sr = self.ws.ServiceNamespace.ItemSearchRequest()
        sr.Keywords = search
        sr.SearchIndex = 'Books'
        sr.ResponseGroup = tuple(['ItemAttributes'])
        sr.ItemPage = '1'
        s = self.ws.ServiceNamespace.ItemSearch()
        s.SubscriptionId = self.key
        s.AssociateTag = ''
        s.Request = tuple([ sr ])

        # print 'calling web service'
        r = self.ws.ItemSearch(s)

        # print 'search for "%s": %s results (%s pages):' % (searchString, r.Items[0].TotalResults, r.Items[0].TotalPages)
        page = 1
        while r is not None and r.Items[0] is not  None and r.Items[0].Item is not None:
            for book in r.Items[0].Item:
                yield book
            page += 1
            sr.ItemPage = str(page)
            r = self.ws.ItemSearch(s)
        
print 'loading web service'
ws = AmazonService(subscriptionId)

print 'searching for "%s"' % searchString
for book in ws.SearchBooks(searchString):
    print '   %s' % book.ItemAttributes.Title
