| 1 | package WWW::SFDC; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Wrappers around the Salesforce.com APIs. | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use 5.12.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | our $VERSION = '0.36'; # TRIAL VERSION | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Log::Log4perl ':easy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Method::Signatures; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use WWW::SFDC::CallResult; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | has 'apiVersion', | ||||||||||||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||||||||||
| 20 | 3 | ▶ | isa => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | LOGDIE "The API version ($_[0]) must be >= 31." | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_[0] and $_[0] >= 31
MC/DC50%1/2
$_[0] and $_[0] >= 31
$_[0]
$_[0] >= 31
Truth table66%2/3
A and B
A= $_[0]B= $_[0] >= 31
| |||||||||||||||||||||||||||||
| 22 | unless $_[0] and $_[0] >= 31 | ||||||||||||||||||||||||||||
| 23 | }, | ||||||||||||||||||||||||||||
| 24 | 5 | default => '33.0'; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 5 | has 'loginResult', | |||||||||||||||||||||||||||
| 28 | is => 'rw', | ||||||||||||||||||||||||||||
| 29 | lazy => 1, | ||||||||||||||||||||||||||||
| 30 | builder => '_login'; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 5 | has 'password', | |||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||
| 35 | required => 1; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 5 | has 'pollInterval', | |||||||||||||||||||||||||||
| 39 | is => 'rw', | ||||||||||||||||||||||||||||
| 40 | default => 15; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 5 | has 'attempts', | |||||||||||||||||||||||||||
| 44 | is => 'rw', | ||||||||||||||||||||||||||||
| 45 | default => 3; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | has 'url', | ||||||||||||||||||||||||||||
| 49 | is => 'ro', | ||||||||||||||||||||||||||||
| 50 | default => "https://test.salesforce.com", | ||||||||||||||||||||||||||||
| 51 | 2 | ▶ | isa => sub { $_[0] and $_[0] =~ s/\/$// or 1; }; #remove trailing slash | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
Condition66%2/3
$_[0] and $_[0] =~ s[/$][]u
MC/DC50%1/2
$_[0] and $_[0] =~ s[/$][]u
$_[0]
$_[0] =~ s[/$][]u
Truth table66%2/3
A and B
A= $_[0]B= $_[0] =~ s[/$][]u
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 5 | has 'username', | |||||||||||||||||||||||||||
| 55 | is => 'ro', | ||||||||||||||||||||||||||||
| 56 | required => 1; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | INIT: { | ||||||||||||||||||||||||||||
| 59 | # Import each API module by reflection. This enables calling them using | ||||||||||||||||||||||||||||
| 60 | # $session->$API->$method, but it means that when packing you must | ||||||||||||||||||||||||||||
| 61 | # manually add the right modules, since there aren't 'compile'-time | ||||||||||||||||||||||||||||
| 62 | # static C<use> statements. | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | for my $module (qw' | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
Subroutine
_login: not called CC=3 SCAR=16.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | Apex Constants Metadata Partner Tooling | ||||||||||||||||||||||||||||
| 65 | 0 | '){ | |||||||||||||||||||||||||||
| 66 | has $module, | ||||||||||||||||||||||||||||
| 67 | 0 | is => 'ro', | |||||||||||||||||||||||||||
| 68 | lazy => 1, | ||||||||||||||||||||||||||||
| 69 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 70 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 71 | 1 | require "WWW/SFDC/$module.pm"; ## no critic | |||||||||||||||||||||||||||
| 72 | 0 | "WWW::SFDC::$module"->new(session => $self); | |||||||||||||||||||||||||||
| 73 | 25 | }; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | method _login { | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | INFO "Logging in...\t"; | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | |||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | my $request = SOAP::Lite | ||||||||||||||||||||||||||||
| 82 | ->proxy( | ||||||||||||||||||||||||||||
| 83 | $self->url()."/services/Soap/u/".$self->apiVersion() | ||||||||||||||||||||||||||||
| 84 | ) | ||||||||||||||||||||||||||||
| 85 | 0 | ->readable(1) | |||||||||||||||||||||||||||
| 86 | ->ns("urn:partner.soap.sforce.com","urn") | ||||||||||||||||||||||||||||
| 87 | ->call( | ||||||||||||||||||||||||||||
| 88 | 'login', | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | SOAP::Data->name("username")->value($self->username), | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=5.5
Subroutine
_doCall: not called CC=7 SCAR=38.6
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 90 | SOAP::Data->name("password")->value($self->password) | ||||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | TRACE "Request: " . Dumper $request; | ||||||||||||||||||||||||||||
| 94 | WWW::SFDC::CallException->throw( | ||||||||||||||||||||||||||||
| 95 | 0 | message => "Login failed: " . $request->faultstring, | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | request => $request | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 97 | ) if $request->fault; | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | return $request->result(); | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | method _doCall ($attempts, $URL, $NS, $method, @params) { | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # This is the utility method behind call(). It orchestrates the actual | ||||||||||||||||||||||||||||
| 106 | # SOAP::Lite call with the correct parameters and detects connection vs | ||||||||||||||||||||||||||||
| 107 | # SOAP errors. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | INFO "Starting $method request"; | |||||||||||||||||||||||||||
| 110 | if ( | ||||||||||||||||||||||||||||
| 111 | my $result = eval { | ||||||||||||||||||||||||||||
| 112 | # SOAP::Lite dies when there's a connection error; if there's an API | ||||||||||||||||||||||||||||
| 113 | # error it lives but $result->fault is set. This allows us to detect | ||||||||||||||||||||||||||||
| 114 | 0 | # network errors and retry. | |||||||||||||||||||||||||||
| 115 | SOAP::Lite | ||||||||||||||||||||||||||||
| 116 | ->proxy($URL) | ||||||||||||||||||||||||||||
| 117 | ->readable(1) | ||||||||||||||||||||||||||||
| 118 | 0 | ->default_ns($NS) | |||||||||||||||||||||||||||
| 119 | 0 | ->call( | |||||||||||||||||||||||||||
| 120 | 0 | $method, | |||||||||||||||||||||||||||
| 121 | @params, | ||||||||||||||||||||||||||||
| 122 | SOAP::Header->name("SessionHeader" => { | ||||||||||||||||||||||||||||
| 123 | 0 | "sessionId" => $self->loginResult->{"sessionId"} | |||||||||||||||||||||||||||
| 124 | })->uri($NS) | ||||||||||||||||||||||||||||
| 125 | ); | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | ) { | ||||||||||||||||||||||||||||
| 128 | return $result; | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | |||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.4
Subroutine
call: not called CC=3 SCAR=21.6
| |||||||||||||||||||||||||||||
| 130 | 0 | } elsif ($attempts) { | |||||||||||||||||||||||||||
| 131 | # Looping by recursion makes it easier to write this bit. | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | INFO "$method failed: $@"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | INFO "Retrying ($attempts attempts remaining)"; | ||||||||||||||||||||||||||||
| 134 | return $self->_doCall($attempts-1, $URL, $NS, @params); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | } else { | |||||||||||||||||||||||||||
| 137 | WWW::SFDC::CallException->throw( | ||||||||||||||||||||||||||||
| 138 | 0 | ▶ | message => "$method failed: " . $@ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | ); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 0 | } | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | method call (@_) { | ||||||||||||||||||||||||||||
| 144 | my $result; | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | until ( | ||||||||||||||||||||||||||||
| 147 | # CallResult is falsy when the call failed | ||||||||||||||||||||||||||||
| 148 | 0 | $result = $self->_doCall($self->attempts, @_) | |||||||||||||||||||||||||||
| 149 | ) { | ||||||||||||||||||||||||||||
| 150 | TRACE "Operation request " => Dumper $result; | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ($result->faultstring =~ /INVALID_SESSION_ID/) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Subroutine
isSandbox: not called CC=2 SCAR=12.4
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | $self->loginResult($self->_login()); | |||||||||||||||||||||||||||
| 154 | } else { | ||||||||||||||||||||||||||||
| 155 | WWW::SFDC::CallException->throw( | ||||||||||||||||||||||||||||
| 156 | 5 | message => "$_[2] failed: " . $result->faultstring, | |||||||||||||||||||||||||||
| 157 | request => $result | ||||||||||||||||||||||||||||
| 158 | ); | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | return WWW::SFDC::CallResult->new(request => $result); | ||||||||||||||||||||||||||||
| 163 | }; | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 5 | ▶ | |||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 166 | 5 | ▶ | method isSandbox { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 167 | 5 | ▶ | return $self->loginResult->{sandbox} eq "true"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 168 | 5 | } | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 5 | 1; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 5 | package WWW::SFDC::CallException; | |||||||||||||||||||||||||||
| 174 | # CallException allows sensible handling of API errors; | ||||||||||||||||||||||||||||
| 175 | # L<WWW::SFDC::Role::Exception> provides overloaded stringification so that | ||||||||||||||||||||||||||||
| 176 | # when you C<die $exception;> there's a sensible error message, but you can | ||||||||||||||||||||||||||||
| 177 | # still examine the request and response in a consumer library. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | use strict; | ||||||||||||||||||||||||||||
| 180 | use warnings; | ||||||||||||||||||||||||||||
| 181 | use Moo; | ||||||||||||||||||||||||||||
| 182 | with 'WWW::SFDC::Role::Exception'; | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | has 'request', | ||||||||||||||||||||||||||||
| 185 | is => 'ro'; | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 1; | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | __END__ | ||||||||||||||||||||||||||||