Atom/Client.pm

filter: Statementstmt 36.3% 61 / 168 Branchbran 2.5% 1 / 40 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 52.9% 18 / 34 Podpod 56.2% 9 / 16 totaltotal 32.0% 89 / 278 CC 27 SCAR 27.6
CC
27
Cov
28%
munge_request
72.0
createEntry
20.0
getFeed
20.0
CRAP 15.9
« Atom/Feed.pm ↑ Summary Atom/ErrorHandler.pm »
1 # $Id$
2  
3 package XML::Atom::Client;
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use XML::Atom;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use base qw( XML::Atom::ErrorHandler );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use XML::Atom::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use XML::Atom::Feed;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use XML::Atom::Util qw( first textValue );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Digest::SHA qw( sha1 );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use MIME::Base64 qw( encode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use constant NS_SOAP => 'http://schemas.xmlsoap.org/soap/envelope/';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
19 1 my $class = shift;
20 1 my $client = bless { }, $class;
21 1 ▶ $client->init(@_) or return $class->error($client->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$client->init(@_) or return $class->error($client->errstr) 1 0 1
22 1 $client;
23 }
24  
25 1 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
26 1 my $client = shift;
27 1 my %param = @_;
28 1 $client->{ua} = LWP::UserAgent::AtomClient->new($client);
29 1 $client->{ua}->agent('XML::Atom/' . XML::Atom->VERSION);
30 1 $client->{ua}->parse_head(0);
31 1 $client;
32 }
33  
34 0 ▶sub username {
Subroutine username: not called CC=2 SCAR=17.9
Pod documented
35 0 my $client = shift;
36 0 ▶ $client->{username} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
37 0 $client->{username};
38 }
39  
40 0 ▶sub password {
Subroutine password: not called CC=2 SCAR=17.9
Pod documented
41 0 my $client = shift;
42 0 ▶ $client->{password} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
43 0 $client->{password};
44 }
45  
46 0 ▶sub use_soap {
Subroutine use_soap: not called CC=2 SCAR=17.9
Pod documented
47 0 my $client = shift;
48 0 ▶ $client->{use_soap} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
49 0 $client->{use_soap};
50 }
51  
52 0 ▶sub auth_digest {
Subroutine auth_digest: not called CC=2 SCAR=17.9
Pod undocumented
53 0 my $client = shift;
54 0 ▶ $client->{auth_digest} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
55 0 $client->{auth_digest};
56 }
57  
58 0 ▶sub getEntry {
Subroutine getEntry: not called CC=2 SCAR=17.9
Pod documented
59 0 my $client = shift;
60 0 my($url) = @_;
61 0 my $req = HTTP::Request->new(GET => $url);
62 0 my $res = $client->make_request($req);
63 0 ▶ return $client->error("Error on GET $url: " . $res->status_line)
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 200 0 0 0
64 unless $res->code == 200;
65 0 XML::Atom::Entry->new(Stream => \$res->content);
66 }
67  
68 0 ▶sub createEntry {
Subroutine createEntry: not called CC=4 SCAR=30.0
Pod documented
69 0 my $client = shift;
70 0 my($uri, $entry) = @_;
71 0 ▶ return $client->error("Must pass a PostURI before posting")
Branch: 1 branch
BranchTrueFalseTotal
unless $uri 0 0 0
72 unless $uri;
73 0 my $req = HTTP::Request->new(POST => $uri);
74 0 $req->content_type($entry->content_type);
75 0 my $xml = $entry->as_xml;
76 0 _utf8_off($xml);
77 0 $req->content_length(length $xml);
78 0 $req->content($xml);
79 0 my $res = $client->make_request($req);
80 0 ▶ return $client->error("Error on POST $uri: " . $res->status_line)
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 201 0 0 0
81 unless $res->code == 201;
82 0 ▶ $res->header('Location') || 1;
Branch: 1 branch
BranchTrueFalseTotal
$res->header("Location") or 1 0 0 0
83 }
84  
85 0 ▶sub updateEntry {
Subroutine updateEntry: not called CC=2 SCAR=17.9
Pod documented
86 0 my $client = shift;
87 0 my($url, $entry) = @_;
88 0 my $req = HTTP::Request->new(PUT => $url);
89 0 $req->content_type($entry->content_type);
90 0 my $xml = $entry->as_xml;
91 0 _utf8_off($xml);
92 0 $req->content_length(length $xml);
93 0 $req->content($xml);
94 0 my $res = $client->make_request($req);
95 0 ▶ return $client->error("Error on PUT $url: " . $res->status_line)
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 200 0 0 0
96 unless $res->code == 200;
97 0 1;
98 }
99  
100 0 ▶sub deleteEntry {
Subroutine deleteEntry: not called CC=2 SCAR=17.9
Pod documented
101 0 my $client = shift;
102 0 my($url) = @_;
103 0 my $req = HTTP::Request->new(DELETE => $url);
104 0 my $res = $client->make_request($req);
105 0 ▶ return $client->error("Error on DELETE $url: " . $res->status_line)
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 200 0 0 0
106 unless $res->code == 200;
107 0 1;
108 }
109  
110 0 ▶sub getFeed {
Subroutine getFeed: not called CC=4 SCAR=30.0
Pod documented
111 0 my $client = shift;
112 0 my($uri) = @_;
113 0 ▶ return $client->error("Must pass a FeedURI before retrieving feed")
Branch: 1 branch
BranchTrueFalseTotal
unless $uri 0 0 0
114 unless $uri;
115 0 my $req = HTTP::Request->new(GET => $uri);
116 0 my $res = $client->make_request($req);
117 0 ▶ return $client->error("Error on GET $uri: " . $res->status_line)
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 200 0 0 0
118 unless $res->code == 200;
119 0 ▶ my $feed = XML::Atom::Feed->new(Stream => \$res->content)
Branch: 1 branch
BranchTrueFalseTotal
my $feed = "XML::Atom::Feed"->new("Stream", \$res->content) or return $client->error("XML::Atom::Feed"->errstr) 0 0 0
120 or return $client->error(XML::Atom::Feed->errstr);
121 0 $feed;
122 }
123  
124 0 ▶sub make_request {
Subroutine make_request: not called CC=1 SCAR=6.9
Pod undocumented
125 0 my $client = shift;
126 0 my($req) = @_;
127 0 $client->munge_request($req);
128 0 my $res = $client->{ua}->request($req);
129 0 $client->munge_response($res);
130 0 $client->{response} = $res;
131 0 $res;
132 }
133  
134 0 ▶sub munge_request {
Subroutine munge_request: not called CC=8 SCAR=42.8
Pod undocumented
135 0 my $client = shift;
136 0 my($req) = @_;
137 0 $req->header(
138 Accept => 'application/atom+xml, application/x.atom+xml, application/xml, text/xml, */*',
139 );
140 0 my $nonce = $client->make_nonce;
141 0 my $nonce_enc = encode_base64($nonce, '');
142 0 my $now = DateTime->now->iso8601 . 'Z';
143 0 ▶ my $digest = encode_base64(sha1($nonce . $now . ($client->password || '')), '');
Condition0%0/2
$client->password || ''
l!l
00
MC/DC0%0/1
$client->password || ''
$client->password
Truth table0%0/2
A
A= $client->password
Aresult
11
00
144 0 ▶ if ($client->use_soap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client->use_soap) { } 0 0 0
145 0 ▶ my $xml = $req->content || '';
Condition0%0/2
$req->content || ''
l!l
00
MC/DC0%0/1
$req->content || ''
$req->content
Truth table0%0/2
A
A= $req->content
Aresult
11
00
146 0 $xml =~ s!^(<\?xml.*?\?>)!!;
147 0 my $method = $req->method;
148 0 ▶ $xml = ($1 || '') . <<SOAP;
Condition0%0/2
$1 || ''
l!l
00
MC/DC0%0/1
$1 || ''
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
149 <soap:Envelope
150 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
151 xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility"
152 xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">
153 <soap:Header>
154 <wsse:Security>
155 <wsse:UsernameToken>
156 0 ▶ <wsse:Username>@{[ $client->username || '' ]}</wsse:Username>
Condition0%0/2
$client->username || ''
l!l
00
MC/DC0%0/1
$client->username || ''
$client->username
Truth table0%0/2
A
A= $client->username
Aresult
11
00
157 <wsse:Password Type="wsse:PasswordDigest">$digest</wsse:Password>
158 <wsse:Nonce>$nonce_enc</wsse:Nonce>
159 <wsu:Created>$now</wsu:Created>
160 </wsse:UsernameToken>
161 </wsse:Security>
162 </soap:Header>
163 <soap:Body>
164 <$method xmlns="http://schemas.xmlsoap.org/wsdl/http/">
165 $xml
166 </$method>
167 </soap:Body>
168 </soap:Envelope>
169 SOAP
170 0 $req->content($xml);
171 0 $req->content_length(length $xml);
172 0 $req->header('SOAPAction', 'http://schemas.xmlsoap.org/wsdl/http/' . $method);
173 0 $req->method('POST');
174 0 $req->content_type('text/xml');
175 } else {
176 0 ▶ if ($client->username) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client->username) 0 0 0
177 0 ▶ $req->header('X-WSSE', sprintf
Condition0%0/2
$client->username || ''
l!l
00
MC/DC0%0/1
$client->username || ''
$client->username
Truth table0%0/2
A
A= $client->username
Aresult
11
00
178 qq(UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"),
179 $client->username || '', $digest, $nonce_enc, $now);
180 0 $req->header('Authorization', 'WSSE profile="UsernameToken"');
181 }
182 }
183 }
184  
185 0 ▶sub munge_response {
Subroutine munge_response: not called CC=4 SCAR=30.0
Pod undocumented
186 0 my $client = shift;
187 0 my($res) = @_;
188 0 ▶ if ($client->use_soap && (my $xml = $res->content)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client->use_soap and my $xml = $res->content) 0 0 0
Condition0%0/3
$client->use_soap and my $xml = $res->content
!ll&&!rl&&r
000
MC/DC0%0/2
$client->use_soap and my $xml = $res->content
$client->use_soap my $xml = $res->content
Truth table0%0/3
A and B
A= $client->use_soapB= my $xml = $res->content
ABresult
0X0
100
111
189 0 my $doc;
190 0 if (LIBXML) {
191 0 my $parser = $client->libxml_parser;
192 0 $doc = $parser->parse_string($xml);
193 } else {
194 my $xp = XML::XPath->new(xml => $xml);
195 $doc = ($xp->find('/')->get_nodelist)[0];
196 }
197 0 my $body = first($doc, NS_SOAP, 'Body');
198 0 ▶ if (my $fault = first($body, NS_SOAP, 'Fault')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fault = first($body, 'http://schemas.xmlsoap.org/soap/envelope/', 'Fault')) { } 0 0 0
199 0 $res->code(textValue($fault, undef, 'faultcode'));
200 0 $res->message(textValue($fault, undef, 'faultstring'));
201 0 $res->content('');
202 0 $res->content_length(0);
203 } else {
204 0 $xml = join '', map $_->toString(LIBXML ? 1 : 0),
205 LIBXML ? $body->childNodes : $body->getChildNodes;
206 0 $res->content($xml);
207 0 $res->content_length(1);
208 }
209 }
210 }
211  
212 0 ▶sub make_nonce { sha1(sha1(time() . {} . rand() . $$)) }
Subroutine make_nonce: not called CC=1 SCAR=6.9
Pod undocumented
213  
214 0 ▶sub _utf8_off {
Subroutine _utf8_off: not called CC=2 SCAR=17.9
215 0 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 0 0 0
216 0 require Encode;
217 0 Encode::_utf8_off($_[0]);
218 }
219 }
220  
221 0 ▶sub libxml_parser { XML::Atom->libxml_parser }
Subroutine libxml_parser: not called CC=1 SCAR=6.9
Pod undocumented
222  
223 package LWP::UserAgent::AtomClient;
224 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
225 2 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
226  
227 2 ▶use base qw( LWP::UserAgent );
Subroutine BEGIN: called CC=1 SCAR=0.0
228  
229 2 my %ClientOf;
230 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
231 1 my($class, $client) = @_;
232 1 my $ua = $class->SUPER::new;
233 1 $ClientOf{$ua} = $client;
234 1 Scalar::Util::weaken($ClientOf{$ua});
235 1 $ua;
236 }
237  
238 0 ▶sub get_basic_credentials {
Subroutine get_basic_credentials: not called CC=2 SCAR=17.9
239 0 my($ua, $realm, $url, $proxy) = @_;
240 0 ▶ my $client = $ClientOf{$ua} or die "Cannot find $ua";
Branch: 1 branch
BranchTrueFalseTotal
my $client = $ClientOf{$ua} or die "Cannot find $ua" 0 0 0
241 0 return $client->username, $client->password;
242 }
243  
244 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
245 1 my $self = shift;
246 1 delete $ClientOf{$self};
247 }
248  
249 2 1;
250 __END__
« Atom/Feed.pm ↑ Summary Atom/ErrorHandler.pm »