#!/usr/bin/perl

#
# Perl SOAP client example for VitPhe webservice - Mathias Chouet - 2012/01/13
# requires SOAP::Lite : http://www.soaplite.com/
#

use SOAP::Lite;

binmode(STDOUT, ":utf8");

# define your parameters here
my $client_urn = 'urn:VitSecWs'; # put here what is written in the WSDL, in the "name" argument of the "wsdl:definitions" element
my $client_proxy = 'http://bioweb.supagro.inra.fr/vitphe/public/ws/'; # this means something like "endpoint"
my $username = 'myuse.rname';
my $password = 'mypassword';
my $http_proxy = 'http://proxyurl:port';
my $use_http_proxy = 0;
#my $nonce = 'qkldfjhek';
#my $created = '';

# creating the client
if ($use_http_proxy) {
	$service = SOAP::Lite->uri($client_urn)->proxy($client_proxy, proxy => ['http' => $http_proxy]);
} else {
	$service = SOAP::Lite->uri($client_urn)->proxy($client_proxy);
}

#$service->transport->proxy(http =>'http://proxy.lasb:3128');

# WSSE authentication header
my $security = SOAP::Header->name("wsse:Security")->attr({'soapenv:mustUnderstand'=>1,'xmlns:wsse'=>'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'});
my $UsernameToken = SOAP::Header->name("wsse:UsernameToken" => \SOAP::Header->value(
	SOAP::Header->name('wsse:Username')->value($username)->type(''),
	#SOAP::Header->name('wsse:Nonce')->value($nonce)->type(''),
	#SOAP::Header->name('wsse:Created')->value($created)->type(''),
	SOAP::Header->name('wsse:Password')->value($password)->type('')->attr({'Type'=>'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'})
))->attr({'xmlns:wsu'=>'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'});


# example method
$result = $service->getHello('Perl client');
unless ($result->fault) {
	print $result->result();
} else {
	print join ', ', 
		$result->faultcode, 
		$result->faultstring;
}
print "\n";

# get experiments list
$result = $service->getExperiments($security->value(\$UsernameToken));
unless ($result->fault) {
	$result = $result->result();
	foreach my $key (keys %{$result}) {
		print "$key :" . $result->{$key} . "\n";
	}
} else {
	print join ', ', 
		$result->faultcode, 
		$result->faultstring;
}
print "\n";

# get measures by experiment
$result = $service->getOfflineMeasuresByExperiment('2012-LEPSE-01', $security->value(\$UsernameToken));
unless ($result->fault) {
	$result = $result->result();
	foreach my $key (keys %{$result}) {
		print "$key : \n";
		$res2 = $result->{$key};
		foreach my $k2 (keys %{$result->{$key}}) {
			print "$k2 :" . $res2->{$k2} . "\n";
		}
		print "\n";
	}
} else {
	print join ', ', 
		$result->faultcode, 
		$result->faultstring;
}
print "\n";

# add online measure
$result = $service->addOnlineMeasure('2012-05-01 14:12:00', '1.26', 'air-temperature', 'PC20-4', 'cm-01', '2011-LEPSE-04', '<data>myAnnotation</data>', $security->value(\$UsernameToken));
print $result->result();
print "\n";
