Amazon Web Services Support

Dave Kuhlman

http://www.rexx.com/~dkuhlman
Email:

Release 1.00
April 16, 2003

 
Front Matter

Copyright (c) 2003 Dave Kuhlman

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Abstract:

This document describes a small amount of support for using Python with Amazon WS (Amazon Web services).



Contents

 
1. Introduction

This document describes additional support for developing Python access to Amazon WS (Amazon Web services). This support is usable with the "XML over HTTP" style of Amazon WS, which is mostly REST-like.

A distribution file containing this support is at amazon_ws_support-1.0.tar.gz.

This support provides modules containing classes that represent the content of the response to a request to Amazon Web services. These modules are also capable of parsing the response XML document.

This support is intended to be used with the XML over HTTP approach to Amazon Web services. One module is provided for the "lite" response document and another module is provided for the "heavy" response document.

The support modules were initially generated with generateDS.py. A copy of generateDS.py is included with the distribution. You can learn more about generateDS.py at http://www.rexx.com/~dkuhlman/#generateDS.

The distribution also includes a very small, example application get_books.py that uses the support modules.

1.1 What It Does

The parser -- Parses an Amazon Web service response document and creates instances of the representation classes. The support modules (dev_lite_sup.py and dev_lite_sub.py, for example) contain functions parse(inFilename) and parseString(inString).

The super-classes -- These are the representation classes. They provide getter and setter functions for member variables, a function (build) to build an instance from the corresponding DOM object, and export function (export).

The sub-classes -- These sub-classes of the representation classes are where you hang your code.

The source code generator -- A copy of generateDS.py is included. This can be used to re-generate the XML representation classes from the XML Schema documents, which are also included.

1.2 What Is Included

 
2. How-to Use It

2.1 Try it out

Try this test:

python get_books.py -a [my_amazon_dev_token] pruning tree

2.2 Creating sub-classes

You can create a new (empty) sub-class file with the either of the following:

./generateDS.py -s my_lite_sub.py dev-lite.xsd
./generateDS.py -s my_heavy_sub.py dev-heavy.xsd

Why would you want to do this? You can, for example, create a new sub-class file, then "decorate" the classes in it with methods that perform specialized tasks.

2.3 Creating an application/harness

There is a example in get_books.py.

How to do it:

  1. Create a module containing the sub-classes. Use either of the following depending on which response document type you intend to request:

    ./generateDS.py -s mysubclasses.py dev-lite.xsd
    ./generateDS.py -s mysubclasses.py dev-heavy.xsd
    

  2. Decorate the classes in the sub-class module with your application specific behaviors. For example, here is what you might add to mysubclasses.py:

    class ProductInfoSub(supermod.ProductInfo):
        o
        o
        o
        def showProduct(self):
            for detail in self.getDetails():
                detail.showProduct(count)
    supermod.ProductInfo.subclass = ProductInfoSub
    # end class ProductInfoSub
    
    class DetailsSub(supermod.Details):
        o
        o
        o
        def showProduct(self, count):
            name = self.getProductname()
            print '%d. ProductName: %s' % (count, name)
            authors = self.getAuthors()
            if authors:
                authors = authors.getAuthor()
                print 'Authors:'
                for author in authors:
                    print '    %s' % author
            price = self.getOurprice()
            if price:
                print 'Price: %s' % price
            else:
                print 'Price not available'
    supermod.Details.subclass = DetailsSub
    # end class DetailsSub
    

  3. In your application, import the module. For example:

    import mysubclasses
    

  4. Parse the document and call the specialized method in the root object. Here is a simple example of how to do so:

    import mysubclasses
    import urllib
    
    f = urllib.urlopen(url)
    content = f.read()
    f.close()
    doc = mysubclasses.parseString(content)
    doc.showProduct()
    

2.4 The parsing functions

The support modules contain functions that parse an XML document returned by a request to Amazon WS. These functions return an instance of the root object, an instance of class ProductInfo.

2.4.1 parse(inFileName)

Usage:

import dev_lite_sub
root = dev_lite_sub.parse(inFileName)

Or:

import dev_heavy_sub
root = dev_heavy_sub.parse(inFileName)

Where:

Returns -- An instance of class ProductInfo.

2.4.2 parseString(aString)

Usage:

import dev_lite_sub
root = dev_lite_sub.parseString(aString)

Or:

import dev_heavy_sub
root = dev_heavy_sub.parseString(aString)

Where:

Returns -- An instance of class ProductInfo.

 
End Matter

Acknowledgements and Thanks

Thanks to Amazon for the Amazon Web Services API and Web Services toolkit.

See Also:

Amazon Web Services
for more information on Amazon Web services and Amazon WS toolkit

The main Python Web Site
for more information on Python

The Python XML Special Interest Group
for more information on processing XML with Python

Dave's Web Site
for more software and information on using Python for XML and the Web

The RESTWiki
for more information and links on REST

About this document ...

Amazon Web Services Support, April 16, 2003, Release 1.00

This document was generated using the LaTeX2HTML translator.

LaTeX2HTML is Copyright © 1993, 1994, 1995, 1996, 1997, Nikos Drakos, Computer Based Learning Unit, University of Leeds, and Copyright © 1997, 1998, Ross Moore, Mathematics Department, Macquarie University, Sydney.

The application of LaTeX2HTML to the Python documentation has been heavily tailored by Fred L. Drake, Jr. Original navigation icons were contributed by Christopher Petrilli.