| 1 | package WebService::Slack::WebApi::Client; | ||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use HTTP::AnyUA; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use WebService::Slack::WebApi::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Class::Accessor::Lite::Lazy ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | new => 1, | |||||||||||||||||||||||||||
| 12 | rw => [qw/ team_domain token opt useragent /], | ||||||||||||||||||||||||||||
| 13 | ro_lazy => [qw/ ua /], | ||||||||||||||||||||||||||||
| 14 | 5 | ); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 7 | ▶ | sub _build_ua { | ||||||||||||||||||||||||||
|
Subroutine
_build_ua: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 17 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 18 | 7 | my $ua; | |||||||||||||||||||||||||||
| 19 | # Before introducing the parameter 'ua' to WebService::Slack::WebApi | ||||||||||||||||||||||||||||
| 20 | # we used Furl. So let's keep ourselves backward compatible! | ||||||||||||||||||||||||||||
| 21 | 7 | ▶ | if( $self->useragent ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 4 | $ua = HTTP::AnyUA->new( ua => $self->useragent ); | |||||||||||||||||||||||||||
| 23 | } else { | ||||||||||||||||||||||||||||
| 24 | # Attn. Using expression form of eval because otherwise | ||||||||||||||||||||||||||||
| 25 | # the "use" would be executed before arriving to eval. | ||||||||||||||||||||||||||||
| 26 | 3 | ▶ | eval 'use Furl; 1;' or do { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | my $msg = 'Illegal parameters. Unable to use package Furl.' | |||||||||||||||||||||||||||
| 28 | . ' If no \'ua\' is defined, we use Furl by default'; | ||||||||||||||||||||||||||||
| 29 | 0 | WebService::Slack::WebApi::Exception::IllegalParameters->throw( | |||||||||||||||||||||||||||
| 30 | message => $msg, | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | }; | ||||||||||||||||||||||||||||
| 33 | 3 | ▶ | my %opt = %{ $self->opt // +{} }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt // {}
MC/DC100%1/1
$self->opt // {}
$self->opt
Truth table100%2/2
A
A= $self->opt
| |||||||||||||||||||||||||||||
| 34 | 3 | my $env_proxy = delete $opt{env_proxy}; | |||||||||||||||||||||||||||
| 35 | 3 | my $furl = Furl->new(%opt); | |||||||||||||||||||||||||||
| 36 | 3 | ▶ | $furl->env_proxy if $env_proxy; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 3 | $ua = HTTP::AnyUA->new( ua => $furl ); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 7 | return $ua; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1177 | ▶ | sub base_url { | ||||||||||||||||||||||||||
|
Subroutine
base_url: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 1177 | my $self = shift; | |||||||||||||||||||||||||||
| 44 | 1177 | ▶ | my $team_domain = $self->team_domain ? $self->team_domain . '.' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 1177 | return sprintf 'https://%sslack.com/api', $team_domain; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1161 | ▶ | sub request { | ||||||||||||||||||||||||||
|
Subroutine
request: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 49 | 1161 | my ($self, $path, $params) = @_; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1161 | my %headers; | |||||||||||||||||||||||||||
| 52 | 1161 | ▶ | if( $self->token && $params->{'http_auth'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->token and $params->{"http_auth"}
MC/DC100%2/2
$self->token and $params->{"http_auth"}
$self->token
$params->{"http_auth"}
Truth table100%3/3
A and B
A= $self->tokenB= $params->{"http_auth"}
| |||||||||||||||||||||||||||||
| 53 | 1 | my $msg = 'Illegal parameters. You have defined \'token\' but the ' | |||||||||||||||||||||||||||
| 54 | . ' method you are using defines its own HTTP Authorization header.'; | ||||||||||||||||||||||||||||
| 55 | 1 | WebService::Slack::WebApi::Exception::IllegalParameters->throw( | |||||||||||||||||||||||||||
| 56 | message => $msg, | ||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 1160 | ▶ | if( $self->token ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 60 | 1159 | $headers{ 'Authorization' } = 'Bearer ' . $self->token; | |||||||||||||||||||||||||||
| 61 | } elsif( $params->{'http_auth'} ) { | ||||||||||||||||||||||||||||
| 62 | 1 | $headers{ 'Authorization' } = $params->{'http_auth'}; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 1160 | my %options = ( headers => \%headers ); | |||||||||||||||||||||||||||
| 65 | my $response = $self->ua->post_form( | ||||||||||||||||||||||||||||
| 66 | $self->base_url . $path, | ||||||||||||||||||||||||||||
| 67 | [ | ||||||||||||||||||||||||||||
| 68 | 1160 | %{ $params }, | |||||||||||||||||||||||||||
| 69 | ], | ||||||||||||||||||||||||||||
| 70 | \%options, | ||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | 1160 | ▶ | return decode_json $response->{content} if $response->{success}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | WebService::Slack::WebApi::Exception::FailureResponse->throw( | |||||||||||||||||||||||||||
| 75 | message => 'request failed.', | ||||||||||||||||||||||||||||
| 76 | response => $response, | ||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 17 | ▶ | sub request_json { | ||||||||||||||||||||||||||
|
Subroutine
request_json: called CC=6 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 17 | my ($self, $path, $params) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 17 | my %headers = ( 'Content-Type' => 'application/json' ); | |||||||||||||||||||||||||||
| 84 | 17 | ▶ | if( $self->token && $params->{'http_auth'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->token and $params->{"http_auth"}
MC/DC0%0/2
$self->token and $params->{"http_auth"}
$self->token
$params->{"http_auth"}
Truth table33%1/3
A and B
A= $self->tokenB= $params->{"http_auth"}
| |||||||||||||||||||||||||||||
| 85 | 0 | my $msg = 'Illegal parameters. You have defined \'token\' but the ' | |||||||||||||||||||||||||||
| 86 | . ' method you are using defines its own HTTP Authorization header.'; | ||||||||||||||||||||||||||||
| 87 | 0 | WebService::Slack::WebApi::Exception::IllegalParameters->throw( | |||||||||||||||||||||||||||
| 88 | message => $msg, | ||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 17 | ▶ | if( $self->token ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 92 | 17 | $headers{ 'Authorization' } = 'Bearer ' . $self->token; | |||||||||||||||||||||||||||
| 93 | } elsif( $params->{'http_auth'} ) { | ||||||||||||||||||||||||||||
| 94 | 0 | $headers{ 'Authorization' } = $params->{'http_auth'}; | |||||||||||||||||||||||||||
| 95 | 0 | delete $params->{'http_auth'}; # slack will not allow this in the json body | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # should really add error handling here. | ||||||||||||||||||||||||||||
| 99 | 17 | my $json_body = JSON->new->utf8(1)->pretty(0)->canonical->encode($params); | |||||||||||||||||||||||||||
| 100 | 17 | my %options = ( headers => \%headers, content => $json_body ); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 17 | my $response = $self->ua->request( | |||||||||||||||||||||||||||
| 103 | 'POST', | ||||||||||||||||||||||||||||
| 104 | $self->base_url . $path, | ||||||||||||||||||||||||||||
| 105 | \%options, | ||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||
| 107 | 17 | ▶ | return decode_json $response->{content} if $response->{success}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | WebService::Slack::WebApi::Exception::FailureResponse->throw( | |||||||||||||||||||||||||||
| 110 | message => 'request_json failed.', | ||||||||||||||||||||||||||||
| 111 | response => $response, | ||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 5 | 1; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||