| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # | 
| 2 |  |  |  |  |  |  | # (c) Jan Gehring | 
| 3 |  |  |  |  |  |  | # | 
| 4 |  |  |  |  |  |  | # vim: set ts=3 sw=3 tw=0: | 
| 5 |  |  |  |  |  |  | # vim: set expandtab: | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | =head1 NAME | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | WebService::ProfitBricks::Connection - Connection object | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | This is the connection object. | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =cut | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | package WebService::ProfitBricks::Connection; | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 1 |  |  | 1 |  | 6 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 20 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 23 |  | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 1 |  |  | 1 |  | 5 | use Data::Dumper; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 51 |  | 
| 23 | 1 |  |  | 1 |  | 5 | use WebService::ProfitBricks::Class; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 85 |  | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 1 |  |  | 1 |  | 6 | use WebService::ProfitBricks::Base; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 21 |  | 
| 26 | 1 |  |  | 1 |  | 6 | use base qw(WebService::ProfitBricks::Base); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 94 |  | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 1 |  |  | 1 |  | 3200 | use SOAP::Lite; # qw(trace); | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | my $soap; | 
| 31 |  |  |  |  |  |  | my %auth = (); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | =head1 OPTIONS | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | If you're connection broke, retry it. Default off. Set to 1 to enable it. | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =cut | 
| 38 |  |  |  |  |  |  | our $RETRY = 0; | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | attrs qw/user password/; | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | sub construct { | 
| 43 |  |  |  |  |  |  | my ($self) = @_; | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | $auth{$self->user} = $self->password; | 
| 46 |  |  |  |  |  |  | sub SOAP::Transport::HTTP::Client::get_basic_credentials { | 
| 47 |  |  |  |  |  |  | return %auth; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | # don't create a new soap object if there is already one | 
| 51 |  |  |  |  |  |  | if(! $soap) { | 
| 52 |  |  |  |  |  |  | $soap = SOAP::Lite->service("https://api.profitbricks.com/1.2/wsdl")->proxy("https://api.profitbricks.com/1.2"); | 
| 53 |  |  |  |  |  |  | $soap->ns("http://ws.api.profitbricks.com/", "tns"); | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | return $self; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | sub auth { | 
| 60 |  |  |  |  |  |  | my ($user, $pass) = @_; | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | sub call { | 
| 65 |  |  |  |  |  |  | my ($self, $call, %data) = @_; | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | my @soap_params; | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | if(exists $data{xml}) { | 
| 70 |  |  |  |  |  |  | push(@soap_params, SOAP::Data->type("xml", $data{xml})); | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  | else { | 
| 73 |  |  |  |  |  |  | for my $key (keys %data) { | 
| 74 |  |  |  |  |  |  | push(@soap_params, SOAP::Data->name($key)->value($data{$key})); | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | my ($resp); | 
| 79 |  |  |  |  |  |  | eval { | 
| 80 |  |  |  |  |  |  | $resp = $soap->call($call, @soap_params); | 
| 81 |  |  |  |  |  |  | } or do { | 
| 82 |  |  |  |  |  |  | if($RETRY) { | 
| 83 |  |  |  |  |  |  | warn "Retrying transaction\n"; | 
| 84 |  |  |  |  |  |  | sleep 2; | 
| 85 |  |  |  |  |  |  | return $self->call($call, %data); | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  | }; | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | #   print Dumper($resp); | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | if(wantarray) { | 
| 92 |  |  |  |  |  |  | my @ret = ($resp->result); | 
| 93 |  |  |  |  |  |  | push(@ret, $resp->paramsout); | 
| 94 |  |  |  |  |  |  | return @ret; | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  | else { | 
| 97 |  |  |  |  |  |  | return $resp->result; | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | 1; |