lib/Bb/Collaborate/Ultra/Connection.pm

filter: Statementstmt 36.2% 37 / 102 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 50.0% 10 / 20 Podpod 100% 6 / 6 totaltotal 30.9% 53 / 171 CC 19 SCAR 26.9
CC
19
Cov
26%
_response
42.0
DELETE
30.0
renew_lease
12.0
CRAP 14.7
« lib/Bb/Collaborate/Ultra/DAO.pm ↑ Summary lib/Bb/Collaborate/Ultra/Util.pm »
1 package Bb::Collaborate::Ultra::Connection;
2 1 ▶use warnings; use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Crypt::JWT qw(encode_jwt decode_jwt);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use REST::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Bb::Collaborate::Ultra::Connection::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Bb::Collaborate::Ultra::Connection - manage a server connection
13  
14 =head2 DESCRIPTION
15  
16 This class is used to maintain connections to Blackboard Ultra virtual
17 classroom servers.
18  
19 require Bb::Collaborate::Ultra::Connection;
20 my %params = (
21 issuer => 'my-client-key',
22 secret => 'sssh!',
23 host => 'https://xx-csa.bbcollab.com',
24 );
25 my $connection = Bb::Collaborate::Ultra::Connection->new(\%params);
26  
27  
28 =head1 METHODS
29  
30 =cut
31  
32 1 has 'issuer' => (is => 'rw', isa => 'Str', required => 1);
33 1 has 'secret' => (is => 'rw', isa => 'Str', required => 1);
34 1 has 'host' => (is => 'rw', isa => 'Str', required => 1);
35  
36 1 has '_client' => (is => 'rw', isa => 'REST::Client' );
37  
38 =head2 auth
39  
40 Holds the authorization token, obtained from the Collaborate server.
41  
42 The connect method mast be invoked to obtain an C<auth> token. The C<renew_lease> method may be later used to extend the session, obtaining
43 an new C<auth> token.
44  
45 =head2 debug
46  
47 $connection->debug(1); # enable debugging
48  
49 When set, a trace is enabled of requests and responses to and from the Collaborate server
50  
51 =cut
52  
53 1 has 'auth' => (is => 'rw', isa => 'Bb::Collaborate::Ultra::Connection::Token' );
54 1 has 'debug' => (is => 'rw', isa => 'Int' );
55  
56 sub _response {
57 0 ▶ my $self = shift;
Subroutine _response: not called CC=6 SCAR=37.4
58 0 ▶ my $client = shift || $self->client;
Condition0%0/3
shift() || $self->client
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->client
shift() $self->client
Truth table0%0/3
A || B
A= shift()B= $self->client
ABresult
1X1
011
000
59 0 my $response_content = $client->responseContent;
60 0 my $response_code = $client->responseCode;
61 0 ▶ warn "RESPONSE: [$response_code] ". $response_content. "\n\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
62 if $self->debug;
63 0 my $response_data;
64 0 ▶ if ($response_content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response_content) 0 0 0
65 try {
66 0 ▶ $response_data = from_json( $response_content);
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 }
68 catch {
69 0 ▶ die "[$response_code] $response_content";
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 0 };
71  
72 die "[$response_code] $response_data->{errorKey} : $response_data->{errorMessage}\n"
73 0 ▶ if $response_data->{errorKey};
Branch: 1 branch
BranchTrueFalseTotal
if $response_data->{"errorKey"} 0 0 0
74 }
75 0 ▶ die "bad HTTP response code: $response_code"
Branch: 1 branch
BranchTrueFalseTotal
unless $response_code == 200 0 0 0
76 unless $response_code == 200;
77 0 $response_data;
78 }
79  
80 1 ▶use constant JWS_RSA_256 => 'HS256';
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 ▶use constant JWT_EXPIRY => 4 * 60; # 4 minutes
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 =head2 connect
84  
85 This method should be called once, with a newly created L<Bb::Collaborate::Ultra::Connection> object to contact the server and authorize the credentials.
86  
87 my %credentials = (
88 issuer => 'OUUK-REST-API12340ABCD',
89 secret => 'ABCDEF0123456789AA',
90 host => 'https://xx-csa.bbcollab.com',
91 );
92  
93 # connect to server
94 my $connection = Bb::Collaborate::Ultra::Connection->new(\%credentials);
95 $connection->connect;
96  
97 =cut
98  
99 sub connect {
100 0 ▶ my $self = shift;
Subroutine connect: not called CC=2 SCAR=17.9
Pod documented
101  
102 0 my $client = $self->client;
103  
104 0 ▶ $self->renew_lease
Branch: 1 branch
BranchTrueFalseTotal
unless $self->auth 0 0 0
105 unless $self->auth;
106 }
107  
108 =head2 client
109  
110 Returns the underlying client connection of type L<REST::Client>.
111  
112 =cut
113  
114 sub client {
115 0 ▶ my $self = shift;
Subroutine client: not called CC=2 SCAR=17.9
Pod documented
116 0 my $client = $self->_client;
117 0 ▶ unless ($client) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client) 0 0 0
118 0 $client= REST::Client->new;
119 0 $client->setHost($self->host);
120 0 $self->_client($client);
121 }
122 0 $client;
123 }
124  
125 =head2 renew_lease
126  
127 if ($connection->auth->expires_in < time() + 60) {
128 # connection is about to expire; keep it alive.
129 $connection->renew_lease;
130 }
131  
132 A authorization token typically remains valid for several minutes. This method
133 can be used to extend the lease, whilst keeping the current connection.
134  
135 =cut
136  
137 sub renew_lease {
138 0 ▶ my $self = shift;
Subroutine renew_lease: not called CC=3 SCAR=24.8
Pod documented
139 0 ▶ my $expiry = shift || time() + JWT_EXPIRY;
Condition0%0/3
shift() || time + 240
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || time + 240
shift() time + 240
Truth table0%0/3
A || B
A= shift()B= time + 240
ABresult
1X1
011
000
140 0 my $class = 'Bb::Collaborate::Ultra::Connection::Token';
141 0 my $client = $self->client;
142  
143 0 my $claims = {
144 iss => $self->issuer,
145 sub => $self->issuer,
146 exp => $expiry,
147 };
148  
149 0 my $jwt = encode_jwt( payload => $claims, key => $self->secret, alg => JWS_RSA_256);
150  
151 0 my $query = $client->buildQuery({
152 grant_type => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
153 assertion => $jwt,
154 });
155  
156 0 my $path = $class->path;
157 0 ▶ warn "POST: $path$query\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
158 0 $client->POST($path . $query, '', { 'Content-Type' => 'application/x-www-form-urlencoded' });
159 0 my $auth_msg = $self->_response($client);
160 0 my $auth = $class->construct($auth_msg, connection => $self);
161 0 $auth->_leased( time() );
162 0 $self->auth( $auth );
163 }
164  
165 =head2 POST
166  
167 Low level method. Posts JSON data formatted data, along with
168 appropriate authorization headers.
169  
170 my $response = $connection->POST('sessions', '{"startTime":"2016-09-27T05:10:04Z","endTime":"2016-09-27T05:25:04Z","name":"Test Session"}');
171 my $session = Bb::Collaborate::Ultra::Session->construct($response, connection => $connection);
172  
173 Generally, you should be using higher level class-specific `post` methods:
174  
175 my $session = Bb::Collaborate::Ultra::Session->post({startTime => time(), endTime => time() + 900, name => 'Test Session'});
176  
177 =cut
178  
179 sub POST {
180 0 ▶ my $self = shift;
Subroutine POST: not called CC=3 SCAR=24.8
Pod documented
181 0 my $path = shift;
182 0 my $json = shift;
183 0 ▶ warn "POST: $path $json\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
184 0 ▶ $self->renew_lease unless $self->auth; # auto-connect
Branch: 1 branch
BranchTrueFalseTotal
unless $self->auth 0 0 0
185 0 $self->client->POST($path, $json, {
186 'Content-Type' => 'application/json',
187 'Authorization' => 'Bearer ' . $self->auth->access_token,
188 },);
189 0 $self->_response;
190 }
191  
192 =head2 PATCH
193  
194 Low level method. Put JSON data formatted data.
195  
196 my $session_id = $session->id;
197 my $response = $connection->PATCH('sessions/'.$session_id, '{"name":"Test Session - Updated"}');
198  
199 =cut
200  
201 sub PATCH {
202 0 ▶ my $self = shift;
Subroutine PATCH: not called CC=2 SCAR=17.9
Pod documented
203 0 my $path = shift;
204 0 my $json = shift;
205 0 ▶ warn "PATCH: $path $json\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
206 0 $self->client->PATCH($path, $json, {
207 'Content-Type' => 'application/json',
208 'Authorization' => 'Bearer ' . $self->auth->access_token,
209 },);
210 0 $self->_response;
211 }
212  
213 =head2 GET
214  
215 Low level method. Get by path
216  
217 my $session_id = $session->id;
218 my $response = $connection->GET('sessions/'.$session_id);
219 $session = Bb::Collaborate::Ultra::Session->construct($response, connection => $connection);
220  
221 =cut
222  
223 sub GET {
224 0 ▶ my $self = shift;
Subroutine GET: not called CC=3 SCAR=24.8
Pod documented
225 0 my $path = shift;
226 0 ▶ warn "GET: $path\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
227 0 ▶ $self->renew_lease unless $self->auth; # auto-connect
Branch: 1 branch
BranchTrueFalseTotal
unless $self->auth 0 0 0
228 0 $self->client->GET($path, {
229 'Content-Type' => 'application/json',
230 'Authorization' => 'Bearer ' . $self->auth->access_token,
231 },);
232 0 $self->_response;
233 }
234  
235 =head2 DELETE
236  
237 Low level method. Delete by path
238  
239 my $session_id = $session->id;
240 my $response = $connection->DELETE('sessions/'.$session_id);
241  
242 =cut
243  
244 sub DELETE {
245 0 ▶ my $self = shift;
Subroutine DELETE: not called CC=5 SCAR=34.0
246 0 my $path = shift;
247 0 ▶ my $query_data = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
248  
249 die "id required for deletion"
250 0 ▶ unless $query_data->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $query_data->{"id"} 0 0 0
251 0 $path .= '/' . $query_data->{id};
252  
253 0 ▶ warn "DELETE: $path\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
254 0 ▶ $self->renew_lease unless $self->auth; # auto-connect
Branch: 1 branch
BranchTrueFalseTotal
unless $self->auth 0 0 0
255 0 $self->client->DELETE($path, {
256 'Content-Type' => 'application/json',
257 'Authorization' => 'Bearer ' . $self->auth->access_token,
258 },);
259 0 $self->_response;
260 }
261  
262 1 1;
« lib/Bb/Collaborate/Ultra/DAO.pm ↑ Summary lib/Bb/Collaborate/Ultra/Util.pm »