SFDC/SessionManager.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 94 CC 4 SCAR 15.1
CC
4
Cov
0%
call
12.0
_login
6.0
_doCall
2.0
CRAP 4.5
1 package WWW::SFDC::SessionManager;
2 # ABSTRACT: Manages auth and SOAP::Lite interactions for WWW::SFDC modules
3  
4 use 5.12.0;
5 use strict;
6 use warnings;
7  
8 use Data::Dumper;
9 use Log::Log4perl ':easy';
10 use Module::Loaded;
11 use SOAP::Lite
12 +trace => [debug => sub { TRACE 'SOAP Request' . Dumper $_[0] }],
13 readable => 1;
14  
15 use Moo;
16 with 'MooX::Singleton';
17  
18 $SOAP::Transport::HTTP::Client::USERAGENT_CLASS = "AnyEvent::HTTP::LWP::UserAgent" if is_loaded "AnyEvent::HTTP::LWP::UserAgent";
19  
20  
21 has 'username',
22 is => 'ro',
23 required => 1;
24  
25 has 'password',
26 is => 'ro',
27 required => 1;
28  
29 has 'url',
30 is => 'ro',
31 default => "https://test.salesforce.com",
32 isa => sub { $_[0] and $_[0] =~ s/\/$// or 1; }; #remove trailing slash
33  
34 has 'apiVersion',
35 is => 'ro',
36 isa => sub { LOGDIE "The API version must be >= 31" unless $_[0] and $_[0] >= 31},
37 default => '33.0';
38  
39 has 'loginResult',
40 is => 'rw',
41 lazy => 1,
42 builder => '_login';
43  
44 sub _login {
45 my $self = shift;
46  
47 INFO "Logging in...\t";
48  
49 $SOAP::Constants::PATCH_HTTP_KEEPALIVE=1;
50 my $request = SOAP::Lite
51 ->proxy($self->url()."/services/Soap/u/".$self->apiVersion())
52 ->readable(1)
53 ->ns("urn:partner.soap.sforce.com","urn")
54 ->call(
55 'login',
56 SOAP::Data->name("username")->value($self->username()),
57 SOAP::Data->name("password")->value($self->password())
58 );
59  
60 TRACE "request " => Dumper $request;
61 LOGDIE "Login Failed: ".$request->faultstring if $request->fault;
62 return $request->result();
63 }
64  
65  
66 sub _doCall {
67 my $self = shift;
68 my ($URL, $NS, @stuff) = @_;
69  
70 INFO "Starting $stuff[0] request";
71  
72 return SOAP::Lite
73 ->proxy($URL, timeout => 300)
74 ->readable(1)
75 ->default_ns($NS)
76 ->call(
77 @stuff,
78 SOAP::Header->name("SessionHeader" => {
79 "sessionId" => $self->loginResult()->{"sessionId"}
80 })->uri($NS)
81 );
82 }
83  
84 sub call {
85 my $self = shift;
86 my $req = $self->_doCall(@_);
87  
88 if ($req->fault && $req->faultstring =~ /INVALID_SESSION_ID/) {
89 $self->loginResult($self->_login());
90 $req = $self->_doCall(@_);
91 }
92  
93 TRACE "Operation request " => Dumper $req;
94 LOGDIE "$_[0] Failed: " . $req->faultstring if $req->fault;
95  
96 return $req;
97 };
98  
99  
100 sub isSandbox {
101 my $self = shift;
102 return $self->loginResult->{sandbox} eq "true";
103 }
104  
105 1;
106  
107 __END__
108  
109 =pod
110  
111 =head1 NAME
112  
113 WWW::SFDC::SessionManager - Manages auth and SOAP::Lite interactions for WWW::SFDC modules
114  
115 =head1 VERSION
116  
117 version 0.22
118  
119 =head1 SYNOPSIS
120  
121 my $sessionId = WWW::SFDC::Login->instance({
122 username => "foo",
123 password => "bar",
124 url => "baz",
125 })->loginResult()->{"sessionId"};
126  
127 =head1 METHODS
128  
129 =head2 call
130  
131 =head2 isSandbox
132  
133 Returns 1 if the org associated with the given credentials are a sandbox. Use to
134 decide whether to sanitise metadata or similar.
135  
136 =head1 BUGS
137  
138 Please report any bugs or feature requests at L<https://github.com/alexander-brett/WWW-SFDC/issues>.
139  
140 =head1 SUPPORT
141  
142 You can find documentation for this module with the perldoc command.
143  
144 perldoc WWW::SFDC::SessionManager
145  
146 You can also look for information at L<https://github.com/alexander-brett/WWW-SFDC>
147  
148 =head1 AUTHOR
149  
150 Alexander Brett <alexander.brett@sophos.com>
151  
152 =head1 COPYRIGHT AND LICENSE
153  
154 This software is Copyright (c) 2015 by Sophos Limited.
155  
156 This is free software, licensed under:
157  
158 The MIT (X11) License
159  
160 The full text of the license can be found in the
161 F<LICENSE> file included with this distribution.
162  
163 =cut