Logging In Our First SOAP Call

At this point, no actual API calls have been made yet; what we've done was just preparation and initialization work. The first thing we need to do before we can start requesting performance data or making configuration changes is to authenticate with the load balancer. So our first API call is going to be the login method.

Performing SOAP requests with a generated helper library always follows the same pattern:

1. Create a request object.

2. Initialize the request object with parameters; this is your argument list to the SOAP function.

3. Call the binder method representing the appropriate SOAP method and pass the request object to it.

4. The binder method returns an API response (or raises an exception if it fails to contact the web service).

As we have already seen, the binding object returned by the Locator is of NSStatBindingSOAP class. Methods of this class represent all functions available on the web service. One of them is the login function, shown in Listing 2-13, which we are going to use to identify ourselves to the load balancer.

Listing 2-13. The definition of a login method # op: login def login(self, request):

if isinstance(request, login) is False:

raise TypeError, "%s incorrect request type" % (request.__class__)

# no input wsaction self.binding.Send(None, None, request, soapaction="urn:NSConfigAction", encodingStyle="http://schemas.xmlsoap.org/soap/encoding/",

# no output wsaction typecode = Struct(pname=None, ofwhat=loginResponse.typecode.ofwhat, pyclass=loginResponse.typecode.pyclass) response = self.binding.Receive(typecode) return response like other methods of the NSStatBindingSOAP class, the login method accepts only one parameter, the request object.

A request object must be constructed from the login class, which is available from the same helper module. The easiest way to find out what the request object must contain is to look at its definition; Listing 2-14 shows what we have in our instance.

Listing 2-14. The login request class class login:

self._username = None self._password = None return

So when initializing the new request object, we must set both _username and _password before we pass it to our binding object.

Now let's create these objects and make a login SOAP call. Listing 2-15 shows the code.

Listing 2-15. A wrapper around the default login method class NSSoapApi(object): [...]

def login(self):

# create request object and assign default values req = self.module.login() req._username = self.username req._password = self.password [...]

res = self.soap.login(req)._return if res._rc != 0:

# an error has occurred

Just as with all other requests, making the SOAP login call is a two-step process:

1. Create and initialize the request object; this object contains data we are going to send to the web service. In the following example, req is our login request object, which we are initializing by setting a username and password for the login call:

req = self.module.login() req._username = self.username req._password = self.password

2. Call the appropriate proxy function from the Binding object and pass the request object to it. The following steps are condensed into a single line of code:

1. Call the login method of our Binding object.

2. Pass the request object constructed in the previous step.

3. Read the response.

When all steps are complete, res will contain the return object, with variables as defined in the NSStat_services_types. py module (or the WSDL datatype section):

res = self.soap.login(req)._return

Was this article helpful?

0 0

Post a comment