SendBird.pm

filter: Statementstmt 88.6% 70 / 79 Branchbran 85.7% 24 / 28 Conditioncond 45.4% 5 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 95.4% 21 / 22 Podpod 100% 12 / 12 totaltotal 84.2% 134 / 159 CC 20 SCAR 13.5
CC
20
Cov
84%
request
8.2
new
7.0
timeout
6.0
CRAP 3.9
↑ Summary SendBird/User.pm »
1 package WebService::SendBird;
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  
6 2 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use WebService::SendBird::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use WebService::SendBird::GroupChat;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '0.002';
15  
16 # ABSTRACT: client to work with SendBird API
17  
18 =head1 NAME
19  
20 WebService::SendBird - unofficial support for the Sendbird API
21  
22 =head1 SYNOPSIS
23  
24 use WebService::SendBird;
25  
26 my $api = WebService::SendBird->new(
27 api_token => 'You_Api_Token_Here',
28 app_id => 'You_App_ID_Here',
29 );
30  
31 my $user = $api->create_user(
32 user_id => 'my_chat_user_1',
33 nickname => 'pumpkin',
34 profile_url => undef,
35 );
36  
37 my $chat = $api->create_group_chat(
38 user_ids => [ $user->user_id ],
39 );
40  
41 =head1 DESCRIPTION
42  
43 Basic implementation for SendBird Platform API client, which helps to create users and group chats.
44  
45 More information at L<Platform API Documentation|https://docs.sendbird.com/platform>
46  
47 =cut
48  
49 2 ▶use constant DEFAULT_API_URL_TEMPLATE => 'https://api-%s.sendbird.com/v3';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use constant DEFAULT_REQUEST_TIMEOUT => 15;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 =head1 METHODS
53  
54 =head2 new
55  
56 Creates an instance of API client
57  
58 =over 4
59  
60 =item * C<api_token> - Master or Secondary API Token.
61  
62 =item * C<app_id> - Sendbird Application ID.
63  
64 =item * C<api_url> - URL to API end point. By default it will be generated from app_id.
65  
66 =item * C<ua> - Custom http client for API requests, should have the same interface like L<Mojo::UserAgent>.
67  
68 =item * C<timeout> - request timeout, default value 15 seconds
69  
70 =back
71  
72 =cut
73  
74 13 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
75 13 my ($cls, %params) = @_;
76  
77 13 ▶ Carp::croak('Missing required argument: api_token') unless $params{api_token};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"api_token"} 1 12 13
78 12 ▶ Carp::croak('Missing required argument: app_id or api_url') unless $params{app_id} || $params{api_url};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"app_id"} or $params{"api_url"} 1 11 12
Condition100%3/3
$params{"app_id"} or $params{"api_url"}
l!l&&r!l&&!r
921
MC/DC100%2/2
$params{"app_id"} or $params{"api_url"}
$params{"app_id"} $params{"api_url"}
Truth table100%3/3
A or B
A= $params{"app_id"}B= $params{"api_url"}
ABresult
1X1
011
000
79  
80 my $self = +{
81 api_token => $params{api_token},
82 $params{ua} ? (ua => $params{ua}) : (),
83 $params{app_id} ? (app_id => $params{app_id}) : (),
84 11 ▶ $params{api_url} ? (api_url => Mojo::URL->new($params{api_url})) : (),
Branch: 3 branches
BranchTrueFalseTotal
$params{'ua'} ? : 1 10 11
$params{'app_id'} ? : 9 2 11
$params{'api_url'} ? : 3 8 11
85 };
86  
87 11 return bless $self, $cls;
88 }
89  
90 =head2 app_id
91  
92 Returns Application ID.
93  
94 =cut
95  
96 5 ▶sub app_id { return shift->{app_id} }
Subroutine app_id: called CC=1 SCAR=0.0
Pod documented
97  
98 =head2 api_token
99  
100 Returns API Token
101  
102 =cut
103  
104 5 ▶sub api_token { return shift->{api_token} }
Subroutine api_token: called CC=1 SCAR=0.0
Pod documented
105  
106 =head2 api_url
107  
108 Returns API endpoint url
109  
110 =cut
111  
112 5 ▶sub api_url {
Subroutine api_url: called CC=2 SCAR=7.0
Pod documented
113 5 my $self = shift;
114  
115 5 ▶ $self->{api_url} //= Mojo::URL->new(sprintf(DEFAULT_API_URL_TEMPLATE, $self->app_id));
Condition66%2/3
$self->{"api_url"} //= "Mojo::URL"->new(sprintf("https://api-%s.sendbird.com/v3", $self->app_id))
l!l&&r!l&&!r
320
MC/DC0%0/2
$self->{"api_url"} //= "Mojo::URL"->new(sprintf("https://api-%s.sendbird.com/v3", $self->app_id))
$self->{"api_url"} "Mojo::URL"->new(sprintf("https://api-%s.sendbird.com/v3", $self->app_id))
Truth table66%2/3
A //= B
A= $self->{"api_url"}B= "Mojo::URL"->new(sprintf("https://api-%s.sendbird.com/v3", $self->app_id))
ABresult
1X1
011
000
116  
117 5 return $self->{api_url};
118 }
119  
120 =head2 timeout
121  
122 Return http request timeout value.
123  
124 =cut
125  
126 0 ▶sub timeout { return shift->{timeout} || DEFAULT_REQUEST_TIMEOUT }
Subroutine timeout: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{'timeout'} || 15
l!l
00
MC/DC0%0/1
+(shift())->{'timeout'} || 15
+(shift())->{'timeout'}
Truth table0%0/2
A
A= +(shift())->{'timeout'}
Aresult
11
00
127  
128 =head2 ua
129  
130 Return User Agent for http request.
131  
132 =cut
133  
134 4 ▶sub ua {
Subroutine ua: called CC=2 SCAR=11.6
Pod documented
135 4 my $self = shift;
136  
137 4 ▶ return $self->{ua} if $self->{ua};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ua"} 4 0 4
138  
139 0 my $ua = Mojo::UserAgent->new();
140  
141 0 $ua->inactivity_timeout($self->timeout);
142 0 $ua->proxy->detect;
143  
144 0 $self->{ua} = $ua;
145 0 return $self->{ua};
146 }
147  
148 =head2 http_headers
149  
150 Returns headers for API request.
151  
152 =cut
153  
154 2 ▶sub http_headers {
Subroutine http_headers: called CC=1 SCAR=0.0
Pod documented
155 2 my $self = shift;
156  
157 return {
158 2 'Content-Type' => 'application/json, charset=utf8',
159 'Api-Token' => $self->api_token,
160 };
161 }
162  
163 =head2 request
164  
165 Sends request to Sendbird API
166  
167 =cut
168  
169 2 ▶sub request {
Subroutine request: called CC=6 SCAR=21.0
Pod documented
170 2 my ($self, $method, $path, $params) = @_;
171  
172 2 ▶ my $resp = $self->ua->start(
Branch: 1 branch
BranchTrueFalseTotal
uc $method eq 'GET' ? : 1 1 2
173 $self->ua->build_tx($method, $self->_url_for($path), $self->http_headers, uc($method) eq 'GET' ? (form => $params) : (json => $params),));
174  
175 2 ▶ if ($resp->result->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->result->is_error) 0 2 2
176 0 ▶ my $details = $resp->result->{content}{asset}{content} // $resp->result->message;
Condition0%0/3
$resp->result->{"content"}{"asset"}{"content"} // $resp->result->message
l!l&&r!l&&!r
000
MC/DC0%0/2
$resp->result->{"content"}{"asset"}{"content"} // $resp->result->message
$resp->result->{"content"}{"asset"}{"content"} $resp->result->message
Truth table0%0/3
A // B
A= $resp->result->{"content"}{"asset"}{"content"}B= $resp->result->message
ABresult
1X1
011
000
177 0 Carp::croak('Fail to make request to SB API: ' . $details);
178 }
179  
180 2 my $data;
181 eval {
182 2 $data = $resp->result->json;
183 2 1;
184 2 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $data = $resp->result->json; 1 } } or do { Carp::croak('Fail to parse response from SB API: ' . $@) } 2 0 2
185 0 Carp::croak('Fail to parse response from SB API: ' . $@);
186 };
187  
188 2 ▶ Carp::croak('Fail to make request to SB API: ' . $data->{message}) if $data->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"error"} 0 2 2
189  
190 2 return $data;
191 }
192  
193 =head2 create_user
194  
195 Creates a user at SendBird
196  
197 =over 4
198  
199 =item * C<user_id> - Unique User Identifier
200  
201 =item * C<nickname> - User nickname
202  
203 =item * C<profile_url> - user profile url. Could be C<undef> or empty.
204  
205 =back
206  
207 Information about extra parameters could be found at L<API Documentation|https://docs.sendbird.com/platform/user#3_create_a_user>
208  
209 Method returns an instance of L<WebService::SendBird::User>
210  
211 =cut
212  
213 4 ▶sub create_user {
Subroutine create_user: called CC=4 SCAR=13.9
Pod documented
214 4 my ($self, %params) = @_;
215  
216 4 ▶ Carp::croak('profile_url is missed') unless exists $params{profile_url};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"profile_url"} 1 3 4
217 3 ▶ $params{$_} or Carp::croak("$_ is missed") for (qw(user_id nickname));
Branch: 1 branch
BranchTrueFalseTotal
$params{$_} or Carp::croak("$_ is missed") 3 2 5
218  
219 1 my $resp = $self->request(
220 POST => 'users',
221 \%params
222 );
223  
224 1 return WebService::SendBird::User->new(%$resp, api_client => $self);
225 }
226  
227 =head2 view_user
228  
229 Gets information about a user from SendBird
230  
231 =over 4
232  
233 =item * C<user_id> - Unique User Identifier
234  
235 =back
236  
237 Information about extra parameters could be found at L<API Documentation|https://docs.sendbird.com/platform/user#3_view_a_user>
238  
239 Method returns an instance of L<WebService::SendBird::User>
240  
241 =cut
242  
243 2 ▶sub view_user {
Subroutine view_user: called CC=2 SCAR=6.9
Pod documented
244 2 my ($self, %params) = @_;
245  
246 2 ▶ my $user_id = delete $params{user_id} or Carp::croak('user_id is missed');
Branch: 1 branch
BranchTrueFalseTotal
my $user_id = delete $params{"user_id"} or Carp::croak("user_id is missed") 1 1 2
247  
248 1 my $resp = $self->request(
249 GET => "users/$user_id",
250 \%params
251 );
252  
253 1 return WebService::SendBird::User->new(%$resp, api_client => $self);
254 }
255  
256 =head2 create_group_chat
257  
258 Creates a group chat room
259  
260 Information about parameters could be found at L<API Documentation|https://docs.sendbird.com/platform/group_channel#3_create_a_channel>
261  
262 Method returns an instance of L<WebService::SendBird::GroupChat>
263  
264 =cut
265  
266 1 ▶sub create_group_chat {
Subroutine create_group_chat: called CC=1 SCAR=0.0
Pod documented
267 1 my ($self, %params) = @_;
268  
269 1 my $resp = $self->request(
270 POST => "group_channels",
271 \%params
272 );
273  
274 1 return WebService::SendBird::GroupChat->new(%$resp, api_client => $self);
275 }
276  
277 =head2 view_group_chat
278  
279 Gets information about a group chat from SendBird
280  
281 =over 4
282  
283 =item * C<channel_url> - Unique Chat Identifier
284  
285 =back
286  
287 Information about parameters could be found at L<API Documentation|https://docs.sendbird.com/platform/group_channel#3_view_a_channel>
288  
289 Method returns an instance of L<WebService::SendBird::GroupChat>
290  
291 =cut
292  
293 3 ▶sub view_group_chat {
Subroutine view_group_chat: called CC=2 SCAR=6.9
Pod documented
294 3 my ($self, %params) = @_;
295 3 ▶ my $channel_url = delete $params{channel_url} or Carp::croak('channel_url is missed');
Branch: 1 branch
BranchTrueFalseTotal
my $channel_url = delete $params{"channel_url"} or Carp::croak("channel_url is missed") 2 1 3
296  
297 2 my $resp = $self->request(
298 GET => "group_channels/$channel_url",
299 \%params
300 );
301  
302 2 return WebService::SendBird::GroupChat->new(%$resp, api_client => $self);
303 }
304  
305 #Private methods
306  
307 #Returns full URL for requested path
308 2 ▶sub _url_for {
Subroutine _url_for: called CC=1 SCAR=0.0
309 2 my ($self, $path) = @_;
310  
311 2 return join q{/} => ($self->api_url, $path);
312 }
313  
314 2 1;
↑ Summary SendBird/User.pm »