Skip to Content

In this Blog I would like to explain the python code used to access the web service in the user-Define transform. For the detail step to be followed for accessing the web service using the user defined transform refer blog : http://scn.sap.com/community/data-services/blog/2013/05/10/use-of-user-defined-transform-in-data-service-4x-for-accessing-web-service-part-1

Here I will be concentrating on the python code used for accessing a web service or to connect to a web site. Also will be explaining a string function to search a string between two matching string.

/wp-content/uploads/2013/05/1_215323.jpg

Ø  First part in the code is the used to import the desired library files that we need to perform various library functions in the

/wp-content/uploads/2013/05/2_215327.jpg

Urllib2 Library

The urllib2 module defines functions and classes which help in opening URLs. The function available in the urllib2 modules which used in our scenario is:

urllib2.urlopen(url,[data],[timeout])

Open the URL, url which can be either a string or a Request object. Optional data may be a string specifying additional data to send to the server, or None if no such data is needed and optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used).

re Library

The re module provides regular expression matching operations. A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression (or if a given regular expression matches a particular string, which comes down to the same thing).

Ø  Next part in the code specifies the variable declaration the local variable in need are declared this is an optional step we can use variable straight without the declaration.

/wp-content/uploads/2013/05/3_215328.jpg

Ø  For fetching the input field into the code we using the GetField method in the FlDataRecord class .This method retrieve the contents of the specified input field. This method can be used with defined input fields only and Returns a new string with the contents of the specified field.

Syntax: fieldVal = GetField(u’fieldName’)

/wp-content/uploads/2013/05/4_215329.jpg

Ø  The target url is formed by appending the input field with the required server path details in the correct format which is acceptable for accessing the web service. And urlopen function available in urllib2 module is used to open the url or for  accessing access the web service. Read function is used to read the data it returns to us and the resultant data is stored in the result variable. 

/wp-content/uploads/2013/05/5_215339.jpg

Ø  Regular expression search function is used to extract the desired output from the response of the web service. Here we have to extract the data coming between the name tag of the web service response.

re.search(pattern, string, flags=0)

Scan through string looking for a location where the regular expression pattern produces a match, and return a corresponding MatchObject instance. Return None if no position in the string matches the pattern.Here the pattern that to be searched is the content coming between the name tag. Use of below mentioned special character in the regular expression is used to extract the desired data.

‘.'(Dot.): In the default mode, this matches any character except a newline. If the DOTALL flag has been specified, this matches any character including a newline.

‘+’ (plus+):Causes the resulting RE to match 1 or more repetitions of the preceding RE. ab+ will match ‘a’ followed by any non-zero number of ‘b’s; it will not match just ‘a’.

‘?’:(Causes the resulting RE to match 0 or 1 repetitions of the preceding RE. ab? will match either ‘a’ or ‘ab’

In our case it will search for the contents in the result string which is the web service response and returns the data which satisfy the search pattern.

/wp-content/uploads/2013/05/6_215340.jpg

Ø  Final step in our code is to Store a output  in the specified output field. we using the SetField method in the FlDataRecord class. This method Stores a value in the specified field.

/wp-content/uploads/2013/05/7_215341.jpg

All Python methods require Unicode input and all return values are in Unicode. In Python syntax, you must alert Python that you are processing Unicode data, for example (where “u” indicates Unicode). So we should be able to encode every input and return values in Unicode. if we fails in that may raise a Unicode decode error during runtime.

Above process describes a simple method to access a web service using the python code.

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply