Bb-Collaborate-Ultra
view release on metacpan or search on metacpan
lib/Bb/Collaborate/Ultra/Connection.pm view on Meta::CPAN
catch {
die "[$response_code] $response_content";
};
die "[$response_code] $response_data->{errorKey} : $response_data->{errorMessage}\n"
if $response_data->{errorKey};
}
die "bad HTTP response code: $response_code"
unless $response_code == 200;
$response_data;
}
use constant JWS_RSA_256 => 'HS256';
use constant JWT_EXPIRY => 4 * 60; # 4 minutes
=head2 connect
This method should be called once, with a newly created L<Bb::Collaborate::Ultra::Connection> object to contact the server and authorize the credentials.
my %credentials = (
issuer => 'OUUK-REST-API12340ABCD',
secret => 'ABCDEF0123456789AA',
host => 'https://xx-csa.bbcollab.com',
);
# connect to server
my $connection = Bb::Collaborate::Ultra::Connection->new(\%credentials);
$connection->connect;
=cut
sub connect {
my $self = shift;
my $client = $self->client;
$self->renew_lease
unless $self->auth;
}
=head2 client
Returns the underlying client connection of type L<REST::Client>.
=cut
sub client {
my $self = shift;
my $client = $self->_client;
unless ($client) {
$client= REST::Client->new;
$client->setHost($self->host);
$self->_client($client);
}
$client;
}
=head2 renew_lease
if ($connection->auth->expires_in < time() + 60) {
# connection is about to expire; keep it alive.
$connection->renew_lease;
}
A authorization token typically remains valid for several minutes. This method
can be used to extend the lease, whilst keeping the current connection.
=cut
sub renew_lease {
my $self = shift;
my $expiry = shift || time() + JWT_EXPIRY;
my $class = 'Bb::Collaborate::Ultra::Connection::Token';
my $client = $self->client;
my $claims = {
iss => $self->issuer,
sub => $self->issuer,
exp => $expiry,
};
my $jwt = encode_jwt( payload => $claims, key => $self->secret, alg => JWS_RSA_256);
my $query = $client->buildQuery({
grant_type => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion => $jwt,
});
my $path = $class->path;
warn "POST: $path$query\n" if $self->debug;
$client->POST($path . $query, '', { 'Content-Type' => 'application/x-www-form-urlencoded' });
my $auth_msg = $self->_response($client);
my $auth = $class->construct($auth_msg, connection => $self);
$auth->_leased( time() );
$self->auth( $auth );
}
=head2 POST
Low level method. Posts JSON data formatted data, along with
appropriate authorization headers.
my $response = $connection->POST('sessions', '{"startTime":"2016-09-27T05:10:04Z","endTime":"2016-09-27T05:25:04Z","name":"Test Session"}');
my $session = Bb::Collaborate::Ultra::Session->construct($response, connection => $connection);
Generally, you should be using higher level class-specific `post` methods:
my $session = Bb::Collaborate::Ultra::Session->post({startTime => time(), endTime => time() + 900, name => 'Test Session'});
=cut
sub POST {
my $self = shift;
my $path = shift;
my $json = shift;
warn "POST: $path $json\n" if $self->debug;
$self->renew_lease unless $self->auth; # auto-connect
$self->client->POST($path, $json, {
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $self->auth->access_token,
},);
( run in 1.328 second using v1.01-cache-2.11-cpan-df04353d9ac )