Information in this document is subject to change without notice. The example companies, organizations, products, people, and events depicted herein are fictitious. No association with any real company, organization, product, person or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.

 

Microsoft may have patents, patent applications, trademarked, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

 

© Microsoft Corporation. All rights reserved.

 

Microsoft, MS-DOS, MS, Windows, Windows NT, MSDN, Active Directory, BizTalk, SQL Server, SharePoint, Outlook, PowerPoint, FrontPage, Visual Basic, Visual C++, Visual J++, Visual InterDev, Visual SourceSafe, Visual C#, Visual J#,  and Visual Studio are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.

 

Other product and company names herein may be the trademarks of their respective owners.


 

This source code is subject to terms and conditions of the Apache License, Version 2.0. A copy of the license can be found in the License.html file at the root of this distribution. If you cannot locate the Apache License, Version 2.0, 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 Apache License, Version 2.0.

 

 

Dynamic Web Service Helpers

 

 

Prerequisites

The prerequisites to successfully run this application are:

 

Getting Started

Please make sure the folder structure in the DynamicWebServiceHelpers directory is:

.\bing.py
.\flickr.py
.\injectors.py
.\mathservice.py
.\rss.py
.\sources\DynamicWebServiceHelpers.csproj
.\stocks.py
.\weather.py

At this point, it will be necessary to build DynamicWebServiceHelpers.dll.  This can be accomplished by building DynamicWebServiceHelpers.csproj using nothing more than .NET 3.5 and a command prompt (in the DynamicWebServiceHelpers directory) as shown below:

  1. copy "%ProgramFiles%\IronPython 2.6\*.dll" .

  2. %WINDIR%\Microsoft.NET\Framework\v3.5\MSBuild.exe sources\DynamicWebServiceHelpers.csproj

Next, you should verify that the newly built DynamicWebServiceHelpers.dll has indeed been installed into the DynamicWebServiceHelpers directory.

 

Overview

The Dynamic Web Service Helpers library is intended to simplify the consumption of web services for IronPython developers. This package includes the DynamicWebServiceHelpers sources and a set of samples that show how to utilize them.

 

Consuming web services from code usually requires help from a tool and involves the following steps:

  1. Download WSDL

  2. Generate proxy code

  3. Include proxy code within the project

  4. Instantiate web service proxy class from code

  5. Call the web method

 

The Dynamic Web Service Helpers library allows IronPython users to significantly simplify access to web services. All it takes is loading the web service given the URL and calling the web method:

>>> ws = DynamicWebServiceHelpers.WebService.Load(web_service_url)
>>> result = ws.web_method(args)

The Dynamic Web Service Helpers library supports the consumption of WSDL, REST/XML web services, and RSS feeds. It also introduces the IronPython concept of attribute injectors, the ability to extend .NET classes with dynamic attributes on a per-instance basis.


Examples

Generic Usage

To use Dynamic Web Service Helpers in IronPython simply load the assembly and import DynamicWebServiceHelpers:

>>> import clr
>>> clr.AddReference('DynamicWebServiceHelpers.dll')
>>> import DynamicWebServiceHelpers

The library contains a public static class, DynamicWebServiceHelpers, that has public static properties (for example, WebService). You can use the Python command 'dir' to view all public properties from this module:

>>> dir(DynamicWebServiceHelpers)
['Equals', 'GetHashCode', 'GetType', 'MemberwiseClone', 'Pluralizer', 'ReferenceEquals', 'Register', 'SimpleXml', 'ToString', 'WebService',
'__all__', '__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__sizeof__', '__str__', '__subclasshook__']

A beneficial side effect of this design is the ability to execute code while importing the static class (static constructor is run at that time). This is a good place to perform operations like registering attribute injectors.

 

Simple WSDL Web Service -- mathservice.py

This sample calls a simple web service that adds two numbers.

 

WSDL Web Service With Complex Return Values -- weather.py

This sample calls a weather web service that returns the weather forecast as a complex object containing a collection of per-day weather data. Please note that it is not necessary to know the type names of the data being returned in order to process the result from a dynamic language like IronPython.

 

Attribute Injectors -- injectors.py

This sample illustrates attribute injectors: English noun string pluralization and 'natural' access to XML.

 

WSDL Web Service Returning XML -- stocks.py

This example calls a stock quote web service that returns the data as XML, and XML attribute injectors are then used to access the returned data in a 'natural' way.

 

RSS Feeds -- rss.py

This example downloads an RSS feed and accesses it using XML attribute injectors.

 

Bing API Web Service to perform a web search -- bing.py

This example calls the Bing API web service to perform a web search.

Important Note: the bing.py script requires you to pass your own Bing Web Service AppId to it from the command line.  Information on obtaining this key can be obtained from here.

 

Flickr.com REST/XML Web Service -- flickr.py

This example calls the Flickr REST web service to perform a photo search. The returned XML is accessed via XML attribute injectors.

Important Note: the flickr.py script requires you to pass your own flickr.com Web Service API key to it from the command line.  Information on obtaining this key can be obtained from here.