blib/lib/Echo360.pm untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 228 CC 29 SCAR 36.2
CC
29
Cov
0%
get
110.0
update
30.0
delete
30.0
CRAP 37.4
1 package Echo360;
2  
3 use strict;
4 use warnings;
5 use Carp;
6 use Net::OAuth;
7 use LWP::UserAgent;
8 use HTTP::Headers;
9 use HTML::Parser;
10 use XML::Simple;
11 use Data::Dumper;
12  
13 $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
14  
15 my %attr_map = (
16 'organizations' => 'organization',
17 'terms' => 'term',
18 'courses' => 'course',
19 'people' => 'person',
20 'rooms' => 'room',
21 'campuses' => 'campus',
22 'buildings' => 'building',
23 'product-groups' => 'product-group',
24 );
25  
26 sub new {
27 my ($self, %args) = @_;
28 my @required = qw(USERNAME KEY URL SECRET);
29
30 my %cfg = map {uc $_ => $args{$_}} keys %args;
31
32 for my $req (qw(USERNAME KEY URL SECRET)) {
33 croak "Required argument $req not found"
34 unless exists $cfg{$req};
35 }
36
37 return bless \%cfg, $self;
38 }
39  
40 sub get {
41 my ($self, %options) = @_;
42 my %opt = map {uc $_ => $options{$_}} keys %options;
43 $opt{URI} = "/$opt{URI}" unless $opt{URI} =~/^\//;
44  
45 print "GET URI: $opt{URI}\n" if $self->{DEBUG};
46  
47 for my $req (qw(URI)) {
48 croak "Required argument $req not found"
49 unless exists $opt{$req};
50 }
51
52 my $resp = $self->_send('get', "$opt{URI}". (exists $opt{PARAMS} ? "?$opt{PARAMS}" : ''));
53  
54 if ($resp and $resp->code >= 400) {
55 $self->{_ERROR} = [$self->_parse_error($resp)];
56 croak $self->errstr;
57 }
58  
59 my $ref = XML::Simple->new()->XMLin($resp->content);
60
61 $opt{URI} =~ s/^\///;
62  
63 if (exists $opt{MATCH}) {
64 my $ret;
65 if ($opt{URI} eq 'people') {
66 for my $id (keys $ref->{$attr_map{$opt{URI}}}) {
67 for (keys $ref->{$attr_map{$opt{URI}}}->{$id}) {
68 if ($ref->{$attr_map{$opt{URI}}}->{$id}{$_} =~ /$opt{MATCH}/) {
69 $ret = {
70 id => $id,
71 map {$_ => $ref->{$attr_map{$opt{URI}}}->{$id}{$_}} keys $ref->{$attr_map{$opt{URI}}}->{$id}
72 };
73 }
74 }
75 }
76
77 } else {
78 for my $match (grep /$opt{MATCH}/, keys $ref->{$attr_map{$opt{URI}}}) {
79 $ret->{$match} = $ref->{$attr_map{$opt{URI}}}->{$match};
80 }
81 }
82
83 return $ret;
84 }
85
86 if ($opt{URI} =~ /.*\/.*/) {
87 return $ref;
88 } else {
89 return $ref->{$attr_map{$opt{URI}}};
90 }
91
92 }
93  
94 sub add {
95 my ($self, $uri, $xml) = @_;
96 $uri = "/$uri" unless $uri =~/^\//;
97  
98 print "POST URI: $uri\n" if $self->{DEBUG};
99  
100 my $resp = $self->_send('POST', "$uri", $xml);
101  
102 if ($resp and $resp->code >= 400) {
103 $self->{_ERROR} = [$self->_parse_error($resp)];
104 croak $self->errstr;
105 }
106  
107 return XML::Simple->new()->XMLin($resp->content)
108 }
109  
110 sub update {
111 my ($self, $uri, $xml) = @_;
112 $uri = "/$uri" unless $uri =~/^\//;
113  
114 print "PUT URI: $uri\n" if $self->{DEBUG};
115
116 my $resp = $self->_send('PUT', "$uri", $xml);
117  
118 if ($resp and $resp->code >= 400) {
119 $self->{_ERROR} = [$self->_parse_error($resp)];
120 croak $self->errstr;
121 }
122  
123 my $ret = XML::Simple->new()->XMLin($resp->content) if $resp->content;
124 return $ret;
125 }
126  
127 sub delete {
128 my ($self, $uri, $xml) = @_;
129 $uri = "/$uri" unless $uri =~/^\//;
130  
131 print "DELETE URI: $uri\n" if $self->{DEBUG};
132
133 my $resp = $self->_send('DELETE', $uri);
134  
135 if ($resp and $resp->code >= 400) {
136 $self->{_ERROR} = [$self->_parse_error($resp)];
137 croak $self->errstr;
138 }
139  
140 my $ret = XML::Simple->new()->XMLin($resp->content) if $resp->content;
141 return $ret;
142 }
143  
144 sub _send {
145 my ($self, $method, $uri, $xml) = @_;
146 my $timestamp = time();
147 my $nonce = int rand 99999999;
148
149 my $oauth_request = Net::OAuth->request('consumer')->new(
150 consumer_key => $self->{'KEY'},
151 consumer_secret => $self->{'SECRET'},
152 request_url => $self->{'URL'}. $uri,
153 request_method => uc $method,
154 signature_method => "HMAC-SHA1",
155 timestamp => $timestamp,
156 nonce => $nonce,
157 extra_params => {
158 'xoauth_requestor_id' => $self->{'USERNAME'} . '@' . $self->{'KEY'},
159 },
160 );
161  
162 $oauth_request->sign;
163  
164 my $req = HTTP::Request->new(uc $method => $self->{'URL'}.$uri.($uri =~ /\?/ ? '&': '?').'xoauth_requestor_id='.$self->{'USERNAME'}.'@'.$self->{'KEY'});
165 $req->header('Content-type' => 'application/xml');
166 $req->header('Authorization' => $oauth_request->to_authorization_header);
167 $req->content($xml) if $xml;
168
169 my $ua = LWP::UserAgent->new;
170 my $oauth_response = $ua->request($req);
171
172 return ($oauth_response) ? $oauth_response : 0;
173 }
174  
175 sub _parse_error {
176 my ($self, $resp) = @_;
177 my @errors;
178 my $pre_found = 0;
179 my $html_parser = HTML::Parser->new(
180 start_h => [sub {
181 for (@_) {$pre_found++ if $_ eq 'pre'}
182 }, 'tag'],
183 text_h => [sub {
184 for (@_) { s/\n//; s/\s\s//g; push @errors, $_ if $pre_found;}
185 }, 'text'],
186 end_h => [sub {
187 for (@_) {$pre_found = 0 if $_ eq '/pre'}
188 }, 'tag']
189 );
190 my $content = $resp->content;
191 $html_parser->parse($content);
192 return @errors;
193 }
194  
195 sub errstr {
196 my $self = shift;
197 return (ref $self->{_ERROR}) ? join ':', @{$self->{_ERROR}} : $self->_ERROR;
198 }
199  
200 1;
201 # ABSTRACT: Echo 360 API