[MOBY-l] Perl API: complexResponse() in CommonSubs.pm ?
Pieter Neerincx
Pieter.Neerincx at wur.nl
Thu May 19 14:38:02 UTC 2005
Dear all,
I'm working on a service that takes simple & secondary articles as
input and has simple & secondary articles as output. In CommonSubs.pm I
did find:
genericServiceInputParser (doesn't handle collections nor secondaries)
complexServiceInputParser (does handle collections and secondaries)
simpleResponse (doesn't handle collections nor secondaries)
collectionResponse (does handle collections but not secondaries)
But I couldn't find a sub that handles secondary output articles. Based
on some of the subs mentioned above and
MOBY::Client::Service::execute() I compiled (copy & paste) the code
below and added it to my CommonSubs.pm. Works fine for me :). Would
this be something for the main CVS tree as well or did I miss
something?
Cheers,
Pieter
Wageningen University and Research Centre
Laboratory of Bioinformatics
Transitorium (Building 312) room 1038
Dreijenlaan 3
6703 HA Wageningen
The Netherlands
Phone: +31 (0)317-484 706
Fax: +31 (0)317-483 584
####### Start section added by Pieter @ WUR #######
=head2 complexResponse
name : complexResponse
function : wraps articles in the appropriate (mobyData) structure
usage : $resp .= &complexResponse(\@data, $queryID);
args : (in order)
\@data - (optional) a listref of arrays containing MOBY
articleNames and raw XML.
each element of @data is itself a listref of
[articleName, $XML]
$queryID - (optional, but strongly recommended) the
queryID value for the
mobyData block to which you are responding
notes : as required by the API you must return a response for
every input.
If one of the inputs was invalid, you return a valid
(empty) MOBY
response by calling &complexResponse(undef, $queryID) with
no arguments.
=cut
sub complexResponse {
my ( $data, $qID ) = @_;
#return 'ERROR: expected listref [element1, element2, ...]
for data' unless ( ref( $data ) =~ /array/i );
return "<moby:mobyData moby:queryID='$qID'/>\n" unless ( ref(
$data ) =~ /array/i );
$qID = &_getQueryID( $qID )
if ref( $qID ) =~ /XML::LibXML/; # in case they send the
DOM instead of the ID
my @inputs = @{$data};
my $output;
foreach ( @inputs ) {
#return 'ERROR: expected listref [articleName, XML]
for data element' unless ( ref( $_ ) =~ /array/i );
return "<moby:mobyData moby:queryID='$qID'/>\n" unless
( ref( $_ ) =~ /array/i );
$output .= "<moby:mobyData queryID='$qID'>";
while ( my ( $articleName, $XML ) = splice( @{$_}, 0,
2 ) ) {
if ( !( ref( $XML ) =~ /array/i ) ) {
$articleName ||= "";
$XML ||= "";
if ( ( $XML =~ /\<Value\>/ ) || ( $XML
=~ /\<moby\:Value\>/ ) )
{
$output .=
"<moby:Parameter
moby:articleName='$articleName'>$XML</moby:Parameter>";
} else {
$output .=
"<moby:Simple
moby:articleName='$articleName'>\n$XML\n</moby:Simple>\n";
}
# need to do this for collections
also!!!!!!
} elsif ( ref( $XML ) =~ /array/i ) {
my @objs = @{$XML};
$output .= "<moby:Collection
moby:articleName='$articleName'>\n";
foreach ( @objs ) {
$output .=
"<moby:Simple>$_</moby:Simple>\n";
}
$output .= "</moby:Collection>\n";
}
}
$output .= "</moby:mobyData>\n";
}
return $output;
}
####### End section added by Pieter @ WUR #######
More information about the moby-l
mailing list