| 1 | package WWW::OneAll; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Mojo::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Mojo::Util qw(b64_encode); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use vars qw/$errstr/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 0 | ▶ | sub errstr { return $errstr } | ||||||||||||||||||||||||||
|
Subroutine
errstr: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub new { ## no critic (ArgUnpacking) | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 16 | 0 | ▶ | my %args = @_ % 2 ? %{$_[0]} : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | for (qw/subdomain public_key private_key/) { | |||||||||||||||||||||||||||
| 19 | 0 | ▶ | $args{$_} || croak "Param $_ is required."; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | $args{endpoint} ||= "https://" . $args{subdomain} . ".api.oneall.com"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{"endpoint"} ||= 'https://' . $args{"subdomain"} . '.api.oneall.com'
MC/DC0%0/1
$args{"endpoint"} ||= 'https://' . $args{"subdomain"} . '.api.oneall.com'
$args{"endpoint"}
Truth table0%0/2
A
A= $args{"endpoint"}
| |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | $args{timeout} ||= 60; # for ua timeout | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{"timeout"} ||= 60
MC/DC0%0/1
$args{"timeout"} ||= 60
$args{"timeout"}
Truth table0%0/2
A
A= $args{"timeout"}
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | return bless \%args, $class; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub __ua { | ||||||||||||||||||||||||||
|
Subroutine
__ua: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 29 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | return $self->{ua} if exists $self->{ua}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | my $ua = Mojo::UserAgent->new; | |||||||||||||||||||||||||||
| 34 | 0 | $ua->max_redirects(3); | |||||||||||||||||||||||||||
| 35 | 0 | $ua->inactivity_timeout($self->{timeout}); | |||||||||||||||||||||||||||
| 36 | 0 | $ua->proxy->detect; # env proxy | |||||||||||||||||||||||||||
| 37 | # $ua->cookie_jar(0); | ||||||||||||||||||||||||||||
| 38 | 0 | $ua->max_connections(100); | |||||||||||||||||||||||||||
| 39 | 0 | $self->{ua} = $ua; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | return $ua; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub connections { | ||||||||||||||||||||||||||
|
Subroutine
connections: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 0 | return (shift)->request('GET', "/connections"); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub connection { | ||||||||||||||||||||||||||
|
Subroutine
connection: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 0 | my ($self, $connection_token) = @_; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | return $self->request('GET', "/connection/$connection_token"); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub request { | ||||||||||||||||||||||||||
|
Subroutine
request: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 0 | my ($self, $method, $url, %params) = @_; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | $errstr = ''; # reset | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Format the query params if exist | ||||||||||||||||||||||||||||
| 60 | 0 | my $ref_url_params = delete $params{query_params}; | |||||||||||||||||||||||||||
| 61 | 0 | my $query_params = "?"; | |||||||||||||||||||||||||||
| 62 | 0 | ▶ | $query_params = $query_params . join("&", @$ref_url_params) if $ref_url_params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | my $ua = $self->__ua; | |||||||||||||||||||||||||||
| 65 | 0 | my $header = {Authorization => 'Basic ' . b64_encode($self->{public_key} . ':' . $self->{private_key}, '')}; | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | $header->{'Content-Type'} = 'application/json' if %params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | my @extra = %params ? (json => \%params) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | my $tx = $ua->build_tx($method => $self->{endpoint} . $url . '.json' . $query_params => $header => @extra); | |||||||||||||||||||||||||||
| 69 | 0 | $tx->req->headers->accept('application/json'); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | $tx = $ua->start($tx); | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ($tx->res->headers->content_type and $tx->res->headers->content_type =~ 'application/json') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$tx->res->headers->content_type and $tx->res->headers->content_type =~ m[application/json]
MC/DC0%0/2
$tx->res->headers->content_type and $tx->res->headers->content_type =~ m[application/json]
$tx->res->headers->content_type
$tx->res->headers->content_type =~ m[application/json]
Truth table0%0/3
A and B
A= $tx->res->headers->content_typeB= $tx->res->headers->content_type =~ m[application/json]
| |||||||||||||||||||||||||||||
| 73 | 0 | return $tx->res->json; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if (!$tx->success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | $errstr = "Failed to fetch $url: " . $tx->error->{message}; | |||||||||||||||||||||||||||
| 77 | 0 | return; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | $errstr = "Unknown Response."; | |||||||||||||||||||||||||||
| 81 | 0 | return; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2 | 1; | |||||||||||||||||||||||||||
| 85 | __END__ | ||||||||||||||||||||||||||||