Tuesday, December 2, 2014

Recently, a former colleague of mine asked me to build him a Web Service Client. Fortunately, I have done this before with C# on .NET 2.0 and later versions with WCF, and it was a piece of cake. So, the project didn't worry me much as I know first hand that it is doable. Though, I started programming with C and Pascal, when I was a sophomore, and spent most of my carrier building applications in C# and Java; lately, Python and Ruby became my favorite languages. Now, probably, you can guess how I ended up with this blog post: I decided to use Python for this project and I stumbled upon an easy but tough problem of technology obscurity! I tried every hack suggested on the internet without success. Thanks to the thanksgiving holiday break, I end up having a working code after may trials. If you have faced with a similar problem, please read on. Before going further, if you are an experienced programmer stuck in a line or two of your code, just skip the notes and look for what you need in the code.

Background

For this project, getting the wsdl requires authentication. Please note this is not a standard case. Some services require no authentication to get the wsdl but require authentication to consume the service, and other free public services may not need any authentication at all. The other important background information relevant to this project was the WS-Security Header format. The Web Service owner (or publisher) organization already provided the correct WS-Security Header format for potential developers as shown in Fig. 1. This format is very important as this is the only WS-Security header format that the Service Provider understands.

Conceptual framework for developing the Web Service Client application in Python

For an experienced programmer in the SOA space, any Web Service Client application development is probably a trivial problem. However, the security layer of the SOAP Web Service implementation on the provider side may complicate the whole game depending on what language and related modules you are using. For this reason, before you begin coding, you need to look for a sample of the correct WS-Security Header format from the service provider, which is similar to the one depicted in Fig. 1.

Basic constituent code blocks needed

The following are the required basic code blocks of your client application

Create the Client:

Add WS-Security Header:

...addSecurityHeader(client,username,password)....def addSecurityHeader(client,username,password): security=Security() userNameToken=UsernameToken(username,password) timeStampToken=Timestamp(validity=600) security.tokens.append(userNameToken) security.tokens.append(timeStampToken) client.set_options(wsse=security)Please note that this method creates the security header depicted in Fig.1. So, your implementation may vary depending on the correct security header format provided by the owner of the service you are consuming.

Consume the relevant method (or operation) :

result=client.service.methodName(Inputs)

Logging:

One of the best practices in such implementations as this one is logging to see how the communication is executed. In case there is some issue, it makes debugging easy. The following code does basic logging. However, you can log many aspects of the communication in addition to the ones depicted in the code.

Result:

Here is the result in my case. Note that the server returned HTTP 200. This is the standard success code for HTTP request-response.

(200, (collectionNodeLmp){

timestamp = 2014-12-03 00:00:00-05:00

nodeLmp[] =

(nodeLmp){

pnodeId = 35010357

name = "YADKIN"

mccValue = -0.19

mlcValue = -0.13

price = 36.46

type = "500 KV"

timestamp = 2014-12-03 01:00:00-05:00

errorCodeId = 0

},

(nodeLmp){

pnodeId = 33138769

name = "ZION 1"

mccValue = -0.18

mlcValue = -1.86

price = 34.75

type = "Aggregate"

timestamp = 2014-12-03 01:00:00-05:00

errorCodeId = 0

},

})

Conclusion

For those Python enthusiasts who tried every piece of hack suggested on the net, to do SOAP Web Service Client, and couldn't make it work, hopefully, this post gets you on the way and takes you where you want to go. If you have questions or suggestions, please do not hesitate to say something. If your client couldn't work even if you follow the steps depicted here, please let me know. We can figure it out together. Enjoy!