Types

A SOAP-based web service might encapsulate a response in a type. This allows the service to define value types for properties. In the case of the periodic table, this might have been used to convert values intended to be numeric into Int32 (or another appropriate type).

The sunsetrises service is an example of a SOAP interface that uses a defined type. A proxy for this service is created as follows:

New-WebServiceProxy http://www.webservicex.net/sunsetriseservice.asmx?WSDL 

The GetSunSetRiseTime method expects an object of type LatLonDate as an argument. The method returns an object of the same type. The assumption is that supplying latitude and longitude on this object will let the method fill in the rest of the details.

The Google geocoding service demonstrated when looking at REST was able to return latitude and longitude for an address:

# Use Google's service to find a latitude and longitude 
$body = @{ 
address = '221b Baker St, Marylebone, London NW1 6XE' 
} 
$response = Invoke-RestMethod -Uri "https://maps.googleapis.com/maps/api/geocode/json" -Body $body 
 
# Connect to the SOAP service 
$sun = New-WebServiceProxy http://www.webservicex.net/sunsetriseservice.asmx?WSDL -Namespace Sun 
 
# Create an instance of LatLonDate 
$latLonDate = New-Object Sun.LatLonDate 
# Populate the Latitude and Longitude 
$latLonDate.Latitude = $response.results.geometry.location.lat 
$latLonDate.Longitude = $response.results.geometry.location.ln 

Once the properties have been filled in, the method can be called:

PS> $sun.GetSunSetRiseTime($latLonDate) 
 
Latitude    : 51.52377 
Longitude   : -0.1585369 
SunSetTime  : 17.65245 
SunRiseTime : 5.9284 
TimeZone    : 0 
Day         : 0 
Month       : 0 
Year        : 0 

The SunSetTime and SunRiseTime might be considered to be hours expressed as a decimal. These can be speculatively converted to hours and minutes as follows:

$sunSetRiseTime = $sun.GetSunSetRiseTime($latLonDate) 
$hour = [Math]::Floor($sunSetRiseTime.SunRiseTime) 
$minute = [Math]::Round(60 * ($sunSetRiseTime.SunRiseTime % 1)) 

Using Get-Date turns that into a DateTime object:

PS> Write-Host "Sun rise:" (Get-Date -Hour $hour -Minute $minute) 
 
Sun rise: 12/03/2017 05:56:50  

This demonstrates that it is possible to work with SOAP methods that expect objects of a specific type.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.10.32