There seem to be growing interest in Python in the R cummunity. While there can be a range of opinions about using R over Python (or vice versa) for exploratory data analysis, fitting statistical/machine learning algorithms and so on, I consider one of the strongest attractions of using Python comes from the fact that Python is a general purpose programming language. As more developers are involved in, it can provide a way to get jobs done easily, which can be tricky in R. In this article, an example is introduced by illustrating how to connect to SOAP (Simple Object Access Protocol) web services.
Web service (or API) is a popular way to connect to a server programmatically and SOAP web service is one type. For those who are interested in it, please see this article. Although R has good packages to connect to a newer type of web service, which is based on REST (Representational state transfer) (eg, httr package), I haven’t found a good R package that can be used as a comprehensive SOAP client, which means I have to use the RCurl package at best. On the other hand, as ‘batteries included’, one of Python’s philosophies, assures, it has a number of SOAP client libraries. Among those, I’ve chosen the suds library.
In this demo, I’m going to connect to Sizmek MDX API where online campaign data can be pulled from it. I’ve used the PyDev plugin of Eclipse and the source of this demo can be found in my GitHub repo. It has 4 classes that connect to the API (Authentication, Advertiser, ConvTag and Campaign) and they are kept in the sizmek package. Also 2 extra classes are set up in the utils package (Soap and Helper), which keep common methods for the 4 classes. The advertiser class can be seen as following.
The 4 classes have a number of common methods (GetItemRes(), GetItem(), GetItemPgn(), GetFilter()) to retrieve data from the relevant sections of the API and these methods are not related to an instance of the classes so that they are set to be static (@staticmethod). In R, this class may be constructed as following.
While it is relatively straightforward to set up corresponding S3 classes, the issue is that there is no comprehensive SOAP client in R. In Python, the client library helps create a proxy class based on the relevant WSDL file so that a request/response can be handled entirely in a ‘Pythonic’ way. For example, below shows how to retrieve advertiser details from the API.
On the other hand, if I use the RCurl package, I have to send the following SOAP message by hacking the relevant WSDL file and its XML response has to be parsed accordingly. Although it is possible, life will be a lot harder.
I guess most R users are not programmers but many of them are quite good at understanding how a program works. Therefore, if there is an area that R is not strong, it’d be alright to consider another language to make life easier. Among those, I consider Python is easy to learn and it can provide a range of good tools. If you’re interested, please see my next article about some thoughts on Python.