RCPPlus.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 92 CC 6 SCAR 21.7
CC
6
Cov
0%
cmd
20.0
request
12.0
new
2.0
CRAP 8.8
1 package Bosch::RCPPlus;
2  
3 our $VERSION = '1.2';
4  
5 =head1 NAME
6  
7 Bosch::RCPPlus.pm -- Perl 5 implementation of the Bosch RCP+ remote procedure call.
8  
9 =head1 SYNOPSIS
10  
11 This package has been developed following Bosch guidelines on implemeting L<RCP+ over CGI|https://media.boschsecurity.com/fs/media/pb/media/partners_1/integration_tools_1/developer/rcpplus-over-cgi.pdf>.
12 Most command specification were taken from debugging the Web UI.
13  
14 # Create Bosch API client
15 my $client = new Bosch::RCPPlus(
16 host => $Host,
17 username => $Username,
18 password => $Password,
19 );
20  
21 # Call a comman (see lib/Commands.pm for full command list)
22 my $name = $client->cmd(Bosch::RCPPlus::Commands::name());
23  
24 # Check if command is actually an error
25 if ($name->error) {
26 print "name failed\n";
27 return -1;
28 }
29  
30 # Print command result
31 print 'Name: ' . $name->result . "\n";
32  
33 =cut
34  
35 use strict;
36  
37 use URI;
38 use HTTP::Request;
39 use LWP::UserAgent;
40 use Bosch::RCPPlus::Response;
41 use Bosch::RCPPlus::AuthError;
42  
43 sub new
44 {
45 my $proto = shift;
46 my $class = ref($proto) || $proto;
47 my %args = @_;
48  
49 my $self = {
50 ua => LWP::UserAgent->new(),
51 protocol => $args{protocol} || 'http',
52 host => $args{host} || 'localhost',
53 username => $args{username},
54 password => $args{password},
55 path => $args{path} || '/rcp.xml',
56 };
57  
58 bless ($self, $class);
59 return $self;
60 }
61  
62 sub uri
63 {
64 my ($proto) = @_;
65 return URI->new($proto->{path})->abs($proto->{protocol} . '://' . $proto->{host});
66 }
67  
68 sub request
69 {
70 my ($proto, %args) = @_;
71 my @headers = ();
72  
73 push @headers, @{$args{headers}} if ($args{headers});
74  
75 my $uri = $proto->uri;
76 $uri->query_form($args{query}) if ($args{query});
77  
78 my $request = HTTP::Request->new(
79 $args{method} || 'GET',
80 $uri,
81 \@headers,
82 $args{content}
83 );
84  
85 return $proto->{ua}->request($request);
86 }
87  
88 sub cmd
89 {
90 my ($proto, %args) = @_;
91  
92 my $format = $args{format};
93 delete $args{format};
94  
95 my $r = $proto->request(query => \%args);
96  
97 if ($r->code eq 401) {
98 my $authenticate = $r->header('www-authenticate');
99  
100 if ($authenticate and $authenticate =~ /realm="([^"]+)"/i) {
101 my $realm = $1;
102 $proto->{ua}->credentials($proto->{host}, $realm, $proto->{username}, $proto->{password});
103 $r = $proto->request(query => \%args);
104  
105 return new Bosch::RCPPlus::AuthError($r->content) if ($r->code eq 401);
106 } else {
107 return new Bosch::RCPPlus::AuthError($r->content);
108 }
109 }
110  
111 return new Bosch::RCPPlus::Response($r->content, \%args, $format);
112 }
113  
114 1;