File Coverage

blib/lib/WebService/Slack/WebApi/Client.pm
Criterion Covered Total %
statement 64 71 90.1
branch 16 24 66.6
condition 6 8 75.0
subroutine 13 13 100.0
pod 0 3 0.0
total 99 119 83.1


line stmt bran cond sub pod time code
1             package WebService::Slack::WebApi::Client;
2 5     5   69 use strict;
  5         11  
  5         249  
3 5     5   35 use warnings;
  5         10  
  5         367  
4 5     5   30 use utf8;
  5         10  
  5         39  
5              
6 5     5   3864 use HTTP::AnyUA;
  5         133089  
  5         266  
7 5     5   2608 use JSON;
  5         39130  
  5         46  
8 5     5   904 use WebService::Slack::WebApi::Exception;
  5         14  
  5         270  
9              
10             use Class::Accessor::Lite::Lazy (
11 5         73 new => 1,
12             rw => [qw/ team_domain token opt useragent /],
13             ro_lazy => [qw/ ua /],
14 5     5   57 );
  5         10  
15              
16             sub _build_ua {
17 7     7   168 my $self = shift;
18 7         16 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 100       41 if( $self->useragent ) {
22 4         40 $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 50   2   593 eval 'use Furl; 1;' or do {
  2     1   1725  
  2         51543  
  2         53  
  1         11  
  1         3  
  1         14  
27 0         0 my $msg = 'Illegal parameters. Unable to use package Furl.'
28             . ' If no \'ua\' is defined, we use Furl by default';
29 0         0 WebService::Slack::WebApi::Exception::IllegalParameters->throw(
30             message => $msg,
31             );
32             };
33 3   100     8 my %opt = %{ $self->opt // +{} };
  3         19  
34 3         44 my $env_proxy = delete $opt{env_proxy};
35 3         15 my $furl = Furl->new(%opt);
36 3 100       214 $furl->env_proxy if $env_proxy;
37 3         43 $ua = HTTP::AnyUA->new( ua => $furl );
38             }
39 7         444 return $ua;
40             }
41              
42             sub base_url {
43 115     115 0 975 my $self = shift;
44 115 50       363 my $team_domain = $self->team_domain ? $self->team_domain . '.' : '';
45 115         1673 return sprintf 'https://%sslack.com/api', $team_domain;
46             }
47              
48             sub request {
49 114     114 0 1249 my ($self, $path, $params) = @_;
50              
51 114         190 my %headers;
52 114 100 100     336 if( $self->token && $params->{'http_auth'} ) {
53 1         11 my $msg = 'Illegal parameters. You have defined \'token\' but the '
54             . ' method you are using defines its own HTTP Authorization header.';
55 1         21 WebService::Slack::WebApi::Exception::IllegalParameters->throw(
56             message => $msg,
57             );
58             }
59 113 100       1247 if( $self->token ) {
    50          
60 112         577 $headers{ 'Authorization' } = 'Bearer ' . $self->token;
61             } elsif( $params->{'http_auth'} ) {
62 1         10 $headers{ 'Authorization' } = $params->{'http_auth'};
63             }
64 113         883 my %options = ( headers => \%headers );
65             my $response = $self->ua->post_form(
66             $self->base_url . $path,
67             [
68 113         418 %{ $params },
  113         1055  
69             ],
70             \%options,
71             );
72 113 100       46844 return decode_json $response->{content} if $response->{success};
73              
74 1         22 WebService::Slack::WebApi::Exception::FailureResponse->throw(
75             message => 'request failed.',
76             response => $response,
77             );
78             }
79              
80             sub request_json {
81 2     2 0 19 my ($self, $path, $params) = @_;
82              
83 2         9 my %headers = ( 'Content-Type' => 'application/json' );
84 2 50 33     27 if( $self->token && $params->{'http_auth'} ) {
85 0         0 my $msg = 'Illegal parameters. You have defined \'token\' but the '
86             . ' method you are using defines its own HTTP Authorization header.';
87 0         0 WebService::Slack::WebApi::Exception::IllegalParameters->throw(
88             message => $msg,
89             );
90             }
91 2 50       25 if( $self->token ) {
    0          
92 2         12 $headers{ 'Authorization' } = 'Bearer ' . $self->token;
93             } elsif( $params->{'http_auth'} ) {
94 0         0 $headers{ 'Authorization' } = $params->{'http_auth'};
95 0         0 delete $params->{'http_auth'}; # slack will not allow this in the json body
96             }
97              
98             # should really add error handling here.
99 2         79 my $json_body = JSON->new->utf8(1)->pretty(0)->canonical->encode($params);
100 2         21 my %options = ( headers => \%headers, content => $json_body );
101            
102 2         11 my $response = $self->ua->request(
103             'POST',
104             $self->base_url . $path,
105             \%options,
106             );
107 2 50       126 return decode_json $response->{content} if $response->{success};
108              
109 0         0 WebService::Slack::WebApi::Exception::FailureResponse->throw(
110             message => 'request_json failed.',
111             response => $response,
112             );
113             }
114              
115             1;
116