Mattermost/V4/API/Response.pm

filter: Statementstmt 97.3% 146 / 150 Branchbran 50.0% 8 / 16 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 40 / 40 Podpod 0.0% 0 / 1 totaltotal 90.7% 197 / 217 CC 12 SCAR 10.0
CC
12
Cov
91%
_build_items
10.6
_looks_like_json
3.1
BUILD
2.1
CRAP 2.7
« Mattermost/V4/API/Role/RequireID.pm ↑ Summary Mattermost/V4/API/Object/Post.pm »
1 package WebService::Mattermost::V4::API::Response;
2  
3 # ABSTRACT: Container for API responses.
4  
5 12 ▶use Mojo::JSON 'decode_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Types::Standard qw(Any ArrayRef Bool InstanceOf Int Maybe Object Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 ▶use WebService::Mattermost::V4::API::Object::Analytics::Old;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use WebService::Mattermost::V4::API::Object::Application;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use WebService::Mattermost::V4::API::Object::Audit;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use WebService::Mattermost::V4::API::Object::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use WebService::Mattermost::V4::API::Object::Channel::Member;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use WebService::Mattermost::V4::API::Object::Channel;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use WebService::Mattermost::V4::API::Object::ChannelStats;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use WebService::Mattermost::V4::API::Object::Compliance::Report;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use WebService::Mattermost::V4::API::Object::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use WebService::Mattermost::V4::API::Object::DataRetentionPolicy;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use WebService::Mattermost::V4::API::Object::Emoji;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 ▶use WebService::Mattermost::V4::API::Object::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 12 ▶use WebService::Mattermost::V4::API::Object::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 12 ▶use WebService::Mattermost::V4::API::Object::Icon;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 12 ▶use WebService::Mattermost::V4::API::Object::Job;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 12 ▶use WebService::Mattermost::V4::API::Object::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 12 ▶use WebService::Mattermost::V4::API::Object::NewLogEntry;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 12 ▶use WebService::Mattermost::V4::API::Object::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 12 ▶use WebService::Mattermost::V4::API::Object::Plugins;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 12 ▶use WebService::Mattermost::V4::API::Object::Post;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 12 ▶use WebService::Mattermost::V4::API::Object::Reaction;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 12 ▶use WebService::Mattermost::V4::API::Object::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 12 ▶use WebService::Mattermost::V4::API::Object::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 12 ▶use WebService::Mattermost::V4::API::Object::Team;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 12 ▶use WebService::Mattermost::V4::API::Object::TeamStats;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 12 ▶use WebService::Mattermost::V4::API::Object::Thread;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 12 ▶use WebService::Mattermost::V4::API::Object::User::Preference;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 12 ▶use WebService::Mattermost::V4::API::Object::User::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 12 ▶use WebService::Mattermost::V4::API::Object::User::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 12 ▶use WebService::Mattermost::V4::API::Object::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 12 ▶use WebService::Mattermost::V4::API::Object::WebRTCToken;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 12 ▶use WebService::Mattermost::Helper::Alias 'view';
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 ################################################################################
43  
44 12 has base_url => (is => 'ro', isa => Str, required => 1);
45 12 has auth_token => (is => 'ro', isa => Str, required => 1);
46 12 has code => (is => 'ro', isa => Int, required => 1);
47 12 has headers => (is => 'ro', isa => InstanceOf['Mojo::Headers'], required => 1);
48 12 has message => (is => 'ro', isa => Str, required => 0);
49 12 has prev => (is => 'ro', isa => InstanceOf['Mojo::Message::Response'], required => 1);
50 12 has raw_content => (is => 'ro', isa => Str, required => 0);
51 12 has item_view => (is => 'ro', isa => Maybe[Str], required => 0);
52 12 has single_item => (is => 'ro', isa => Bool, required => 0);
53  
54 12 has is_error => (is => 'ro', isa => Bool, default => 0);
55 12 has is_success => (is => 'ro', isa => Bool, default => 1);
56  
57 16 ▶has content => (is => 'rw', isa => Any, default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
58  
59 12 has item => (is => 'ro', isa => Maybe[Object], lazy => 1, builder => 1);
60 12 has items => (is => 'ro', isa => Maybe[ArrayRef], lazy => 1, builder => 1);
61  
62 ################################################################################
63  
64 sub BUILD {
65 34 ▶ my $self = shift;
Subroutine BUILD: called CC=2 SCAR=7.6
Pod undocumented
66  
67 34 ▶ if ($self->_looks_like_json($self->raw_content)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_looks_like_json($self->raw_content)) 0 34 34
68 0 $self->content(decode_json($self->raw_content));
69 }
70  
71 34 return 1;
72 }
73  
74 ################################################################################
75  
76 sub _looks_like_json {
77 40 ▶ my $self = shift;
Subroutine _looks_like_json: called CC=3 SCAR=11.4
78 40 my $inp = shift;
79  
80 # Rudimentary "is it JSON?" hack
81 40 ▶ return $inp && $inp =~ /^[\{\[]/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$inp && $inp =~ /^[\{\[]/ ? : 0 40 40
Condition66%2/3
$inp and $inp =~ /^[\{\[]/
!ll&&!rl&&r
3460
MC/DC0%0/2
$inp and $inp =~ /^[\{\[]/
$inp $inp =~ /^[\{\[]/
Truth table66%2/3
A and B
A= $inpB= $inp =~ /^[\{\[]/
ABresult
0X0
100
111
82 }
83  
84 ################################################################################
85  
86 sub _build_item {
87 4 ▶ my $self = shift;
Subroutine _build_item: called CC=2 SCAR=7.0
88  
89 4 my $item;
90  
91 4 ▶ if (scalar @{$self->items}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->items;}) 4 0 4
92 4 $item = $self->items->[0];
93 }
94  
95 4 return $item;
96 }
97  
98 sub _build_items {
99 6 ▶ my $self = shift;
Subroutine _build_items: called CC=8 SCAR=23.6
100  
101 6 my @ret;
102  
103 6 ▶ if ($self->item_view) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->item_view) 6 0 6
104 6 ▶ my @init_items = ref $self->content eq 'ARRAY' ? @{$self->content} : ($self->content);
Branch: 1 branch
BranchTrueFalseTotal
ref $self->content eq 'ARRAY' ? : 0 6 6
105 6 my @items;
106  
107 # Sometimes, for example in GET /logs, a JSON string is returned rather
108 # than a hash.
109 6 foreach (@init_items) {
110 6 $_ =~ s/\n//sg;
111  
112 6 ▶ if ($self->_looks_like_json($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_looks_like_json($_)) { } 0 6 6
113 0 push @items, decode_json($_);
114 } else {
115 6 ▶ push @items, $_ if $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 6 0 6
116 }
117 }
118  
119 6 ▶ if ($items[0]->{status_code} && $items[0]->{status_code} != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($items[0]{'status_code'} and $items[0]{'status_code'} != 200) { } 0 6 6
Condition33%1/3
$items[0]{"status_code"} and $items[0]{"status_code"} != 200
!ll&&!rl&&r
600
MC/DC0%0/2
$items[0]{"status_code"} and $items[0]{"status_code"} != 200
$items[0]{"status_code"} $items[0]{"status_code"} != 200
Truth table33%1/3
A and B
A= $items[0]{"status_code"}B= $items[0]{"status_code"} != 200
ABresult
0X0
100
111
120 # The response is actually an error - create an Error view
121 0 push @ret, view('Error')->new({
122 raw_data => $items[0],
123 auth_token => $self->auth_token,
124 base_url => $self->base_url,
125 });
126 } else {
127 @ret = map {
128 6 view($self->item_view)->new({
129 auth_token => $self->auth_token,
130 base_url => $self->base_url,
131 raw_data => $_,
132 })
133 } @items;
134 }
135 }
136  
137 6 return \@ret;
138 }
139  
140 ################################################################################
141  
142 12 1;
143  
144 __END__
145  
146 =pod
147  
148 =encoding UTF-8
149  
150 =head1 NAME
151  
152 WebService::Mattermost::V4::API::Response - Container for API responses.
153  
154 =head1 VERSION
155  
156 version 0.28
157  
158 =head1 DESCRIPTION
159  
160 A common container for responses from the Mattermost API.
161  
162 =head2 ATTRIBUTES
163  
164 =over 4
165  
166 =item C<code>
167  
168 The HTTP code returned.
169  
170 =item C<headers>
171  
172 Headers from the C<Mojo::Message::Response> object.
173  
174 =item C<message>
175  
176 A message (or undef) from the API (for example if there is a C<code> of 403,
177 the C<message> will be "Forbidden").
178  
179 =item C<prev>
180  
181 The returned C<Mojo::Message::Response> object.
182  
183 =item C<raw_content>
184  
185 JSON-encoded content or undef.
186  
187 =item C<is_error>
188  
189 =item C<is_success>
190  
191 =item C<content>
192  
193 Decoded content in ArrayRef or HashRef form.
194  
195 =item C<item_view>
196  
197 Whether or not the response should try to create a v4::Object object.
198  
199 =item C<single_item>
200  
201 Whether or not the expected v4::Object should be an ArrayRef.
202  
203 =item C<item>
204  
205 The first v4::Object object.
206  
207 =item C<items>
208  
209 All view objects.
210  
211 =back
212  
213 =head1 AUTHOR
214  
215 Mike Jones <mike@netsplit.org.uk>
216  
217 =head1 COPYRIGHT AND LICENSE
218  
219 This software is Copyright (c) 2020 by Mike Jones.
220  
221 This is free software, licensed under:
222  
223 The MIT (X11) License
224  
225 =cut
« Mattermost/V4/API/Role/RequireID.pm ↑ Summary Mattermost/V4/API/Object/Post.pm »