| 1 | package SOAP::Simple::Trait; | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | |||
| 5 | use XML::Compile::WSDL11; | ||
| 6 | use XML::Compile::SOAP11; | ||
| 7 | use XML::Compile::Transport::SOAPHTTP; | ||
| 8 | |||
| 9 | sub add_wsdl { | ||
| 10 | my ($self,$wsdl,%options) = @_; | ||
| 11 | |||
| 12 | $wsdl = $self->_coerce_wsdl ($wsdl); | ||
| 13 | |||
| 14 | my $service = $self->_wsdl_pick_one ('service',[ $wsdl->findDef ('service') ],$options{ service }); | ||
| 15 | |||
| 16 | my $port = $self->_wsdl_pick_one ('port',$service->{ wsdl_port },$options{ port }); | ||
| 17 | |||
| 18 | my $binding = $wsdl->findDef (binding => $port->{ binding }); | ||
| 19 | |||
| 20 | foreach my $operation (@{ $binding->{ wsdl_operation } }) { | ||
| 21 | my $client = $wsdl->compileClient ( | ||
| 22 | operation => $operation->{ name }, | ||
| 23 | service => $service->{ name }, | ||
| 24 | port => $port->{ name }, | ||
| 25 | interpret_nillable_as_optional => 1, | ||
| 26 | ); | ||
| 27 | |||
| 28 | $self->add_method ($operation->{ name } => sub { shift; goto $client }); | ||
| 29 | } | ||
| 30 | |||
| 31 | return; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _wsdl_pick_one { | ||
| 35 | my ($self,$description,$items,$key) = @_; | ||
| 36 | |||
| 37 | confess "No $description found in the WSDL" unless @$items; | ||
| 38 | |||
| 39 | my $item; | ||
| 40 | if (defined $key) { | ||
| 41 | ($item) = grep { $_->{ name } eq $key } @$items; | ||
| 42 | |||
| 43 | confess "No $description by the name ($key), but try one of (" . join (", ",map { $_->{ name } } @$items) . ") instead" unless defined $item; | ||
| 44 | } else { | ||
| 45 | confess "Multiple choices for $description found, please pick from (" . join (", ",map { $_->{ name } } @$items) . ")" if @$items > 1; | ||
| 46 | |||
| 47 | ($item) = @$items; | ||
| 48 | } | ||
| 49 | |||
| 50 | return $item; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub _coerce_wsdl { | ||
| 54 | my ($self,$wsdl) = @_; | ||
| 55 | |||
| 56 | # Obviously wsdl can't be undefined | ||
| 57 | |||
| 58 | confess "First argument must be defined (And preferably related to a WSDL)" unless defined $wsdl; | ||
| 59 | |||
| 60 | # Feed whatever we're fed to XML::Compile::WSDL11 if it's not | ||
| 61 | # already an instance of that. | ||
| 62 | |||
| 63 | return $wsdl if blessed $wsdl && $wsdl->isa ('XML::Compile::WSDL11'); | ||
| 64 | |||
| 65 | # Except if it's a non-ref string starting with https?:// | ||
| 66 | |||
| 67 | $wsdl = $self->_fetch_wsdl ($wsdl) if ! ref $wsdl && $wsdl =~ m{^https?://}; | ||
| 68 | |||
| 69 | return XML::Compile::WSDL11->new ($wsdl); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _fetch_wsdl { | ||
| 73 | confess "Remote fetching not implemented yet"; | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; | ||
| 77 |