lib/WebService/Whistle/Pet/Tracker/API.pm

filter: Statementstmt 46.6% 35 / 75 Branchbran 19.5% 9 / 46 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 47.0% 8 / 17 Podpod 100% 13 / 13 totaltotal 43.0% 65 / 151 CC 24 SCAR 34.4
CC
24
Cov
36%
api
110.0
pet_daily_items
12.0
login
12.0
CRAP 31.2
↑ Summary script/perl-WebService-Whistle-Pet-Tracker-API-device.pl »
1 package WebService::Whistle::Pet::Tracker::API;
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 JSON::XS qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use HTTP::Tiny qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = '0.03';
8 5 our $PACKAGE = __PACKAGE__;
9 5 our $API_URL = 'https://app.whistle.com/api';
10  
11 =head1 NAME
12  
13 WebService::Whistle::Pet::Tracker::API - Perl interface to access the Whistle Pet Tracker Web Service
14  
15 =head1 SYNOPSIS
16  
17 use WebService::Whistle::Pet::Tracker::API;
18 my $ws = WebService::Whistle::Pet::Tracker::API->new(email=>$email, password=>$password);
19 my $pets = $ws->pets; #isa ARRAY of HASHes
20 foreach my $pet (@$pets) {
21 print JSON::XS->new->pretty->encode($pet);
22 }
23  
24 =head1 DESCRIPTION
25  
26 Perl interface to access the Whistle Pet Tracker Web Service. All methods return JSON payloads that are converted to Perl data structures. Methods that require authentication will request a token and cache it for the duration of the object.
27  
28 =head1 CONSTRUCTORS
29
30 =head2 new
31
32 my $ws = WebService::Whistle::Pet::Tracker::API->new(email=>$email, password=>$password);
33
34 =cut
35
36 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
37 1 my $this = shift;
38 1 ▶ my $class = ref($this) ? ref($this) : $this;
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 0 1 1
39 1 my $self = {};
40 1 bless $self, $class;
41 1 ▶ %$self = @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
42 1 return $self;
43 }
44  
45 =head1 PROPERTIES
46  
47 =head2 email
48  
49 Sets and returns the registered Whistle account email
50  
51 =cut
52  
53 2 ▶sub email {
Subroutine email: called CC=3 SCAR=11.0
Pod documented
54 2 my $self = shift;
55 2 ▶ $self->{'email'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 1 2
56 2 ▶ die("Error: Whistle API: email required") unless $self->{'email'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"email"} 0 2 2
57 2 return $self->{'email'};
58 }
59  
60 =head2 password
61  
62 Sets and returns the registered Whistle account password
63  
64 =cut
65  
66 2 ▶sub password {
Subroutine password: called CC=3 SCAR=11.0
Pod documented
67 2 my $self = shift;
68 2 ▶ $self->{'password'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 1 2
69 2 ▶ die("Error: Whistle API: password required") unless $self->{'password'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"password"} 0 2 2
70 2 return $self->{'password'};
71 }
72  
73 =head1 METHODS
74  
75 =head2 pets
76  
77 Returns a list of pets as an array reference
78  
79 my $pets = $ws->pets;
80  
81 =cut
82  
83 0 ▶sub pets {
Subroutine pets: not called CC=1 SCAR=6.9
Pod documented
84 0 my $self = shift;
85 0 return $self->api('/pets')->{'pets'};
86 }
87  
88 =head2 device
89  
90 Returns device data for the given device id
91  
92 my $device = $ws->device('WXX-ABC123');
93 my $battery_level = $device->{'battery_level'}; #0-100 charge level
94  
95 =cut
96  
97 0 ▶sub device {
Subroutine device: not called CC=2 SCAR=17.9
Pod documented
98 0 my $self = shift;
99 0 ▶ my $serial_number = shift or die("Error: Whistle API: Device serial number required.");
Branch: 1 branch
BranchTrueFalseTotal
my $serial_number = shift() or die "Error: Whistle API: Device serial number required." 0 0 0
100 0 return $self->api("/devices/$serial_number")->{'device'};
101 }
102  
103 =head2 pet_dailies
104  
105 Returns dailies for the given pet id
106  
107 my $pet_dailies = $ws->pet_dailies($pet_id);
108  
109 =cut
110  
111 0 ▶sub pet_dailies {
Subroutine pet_dailies: not called CC=2 SCAR=17.9
Pod documented
112 0 my $self = shift;
113 0 ▶ my $pet_id = shift or die("Error: Whistle API: pet id required");
Branch: 1 branch
BranchTrueFalseTotal
my $pet_id = shift() or die "Error: Whistle API: pet id required" 0 0 0
114 0 return $self->api("/pets/$pet_id/dailies")->{'dailies'}; #https://app.whistle.com/api/pets/123456789/dailies
115 }
116  
117 =head2 pet_daily_items
118  
119 Returns the daily items for the given pet id and day number
120  
121 my $pet_daily_items = $ws->pet_daily_items($pet_id, $day_number);
122  
123 =cut
124  
125 0 ▶sub pet_daily_items {
Subroutine pet_daily_items: not called CC=3 SCAR=24.8
Pod documented
126 0 my $self = shift;
127 0 ▶ my $pet_id = shift or die("Error: Whistle API: pet id required");
Branch: 1 branch
BranchTrueFalseTotal
my $pet_id = shift() or die "Error: Whistle API: pet id required" 0 0 0
128 0 ▶ my $day_number = shift or die("Error: Whistle API: day number required");
Branch: 1 branch
BranchTrueFalseTotal
my $day_number = shift() or die "Error: Whistle API: day number required" 0 0 0
129 0 return $self->api("/pets/$pet_id/dailies/$day_number/daily_items")->{'daily_items'};
130 }
131  
132 =head2 pet_stats
133  
134 Returns pet stats for the given pet id
135  
136 my $pet_stats = $ws->pet_stats(123456789);
137  
138 =cut
139  
140 0 ▶sub pet_stats {
Subroutine pet_stats: not called CC=2 SCAR=17.9
Pod documented
141 0 my $self = shift;
142 0 ▶ my $pet_id = shift or die("Error: Whistle API: pet id required");
Branch: 1 branch
BranchTrueFalseTotal
my $pet_id = shift() or die "Error: Whistle API: pet id required" 0 0 0
143 0 return $self->api("/pets/$pet_id/stats")->{'stats'};
144 }
145  
146 =head2 places
147  
148 Returns registered places as an array reference
149  
150 my $places = $ws->places;
151  
152 =cut
153  
154 0 ▶sub places {shift->api('/places')}; #this api call returns an array instead of a hash like other calls
Subroutine places: not called CC=1 SCAR=6.9
Pod documented
155  
156 =head1 METHODS (API)
157  
158 =head2 api
159  
160 Returns the decoded JSON data from the given web service end point
161  
162 my $data = $ws->api('/end_point');
163  
164 =cut
165  
166 0 ▶sub api {
Subroutine api: not called CC=10 SCAR=47.0
Pod documented
167 0 my $self = shift;
168 0 ▶ my $api_destination = shift or die("Error: Whistle API: api destination required");
Branch: 1 branch
BranchTrueFalseTotal
my $api_destination = shift() or die "Error: Whistle API: api destination required" 0 0 0
169 0 my $url = $API_URL. $api_destination;
170 0 ▶ my $response = $api_destination eq '/login'
Branch: 1 branch
BranchTrueFalseTotal
$api_destination eq '/login' ? : 0 0 0
171 ? $self->ua->post_form($url, [email => $self->email, password => $self->password])
172 : $self->ua->get($url, {
173 headers => {
174 Accept => 'application/vnd.whistle.com.v6+json',
175 Authorization => 'Bearer '. $self->auth_token,
176 },
177 }
178 );
179 0 ▶ print JSON::XS->new->pretty->encode({response => $response}) if $self->{'DEBUG'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 0 0
180 0 my $status = $response->{'status'};
181 0 my $reason = $response->{'reason'};
182 0 ▶ if ($api_destination eq '/login') {
Branch: 1 branch
BranchTrueFalseTotal
if ($api_destination eq '/login') { } 0 0 0
183 0 ▶ die("Error: Whistle API: login failed\n") if $status eq 422;
Branch: 1 branch
BranchTrueFalseTotal
if $status eq 422 0 0 0
184 0 ▶ die("Error: Whistle API: request unsuccessful - request: $api_destination, status: $status $reason\n") unless $status eq 201;
Branch: 1 branch
BranchTrueFalseTotal
unless $status eq 201 0 0 0
185 } else {
186 0 ▶ die("Error: Whistle API: request unsuccessful - request: $api_destination, status: $status $reason\n") unless $status eq 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $status eq 200 0 0 0
187 }
188 0 my $response_content = $response->{'content'};
189 0 local $@;
190 0 my $response_decoded = eval{JSON::XS::decode_json($response_content)};
191 0 my $error = $@;
192 0 ▶ die("Error: Whistle API: invalid JSON - request: $api_destination, status: $status $reason, content: $response_content\n") if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
193 0 ▶ print JSON::XS->new->pretty->encode({response_decoded => $response_decoded}) if $self->{'DEBUG'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 0 0
194 0 return $response_decoded;
195 }
196  
197 =head2 login
198  
199 Calls the login service, caches, and returns the response.
200  
201 =cut
202  
203 0 ▶sub login {
Subroutine login: not called CC=3 SCAR=24.8
Pod documented
204 0 my $self = shift;
205 0 ▶ $self->{'login'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
206 0 ▶ $self->{'login'} = $self->api('/login') unless defined $self->{'login'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"login"} 0 0 0
207 0 return $self->{'login'};
208 }
209  
210 =head2 auth_token
211  
212 Retrieves the authentication token from the login end point
213  
214 =cut
215  
216 0 ▶sub auth_token {shift->login->{'auth_token'}};
Subroutine auth_token: not called CC=1 SCAR=6.9
Pod documented
217  
218 =head1 ACCESSORS
219
220 =head2 ua
221
222 Returns an L<HTTP::Tiny> web client user agent
223
224 =cut
225
226 1 ▶sub ua {
Subroutine ua: called CC=2 SCAR=7.0
Pod documented
227 1 my $self = shift;
228 1 ▶ unless ($self->{'ua'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ua"}) 1 0 1
229 1 my %settinges = (
230 keep_alive => 0,
231 agent => "Mozilla/5.0 (compatible; $PACKAGE/$VERSION; See rt.cpan.org 35173)",
232 );
233 1 $self->{'ua'} = HTTP::Tiny->new(%settinges);
234 }
235 1 return $self->{'ua'};
236 }
237  
238 =head1 SEE ALSO
239  
240 =over
241  
242 =item Python - L<https://github.com/RobertD502/whistleaio>
243  
244 =item NodeJS (old api) - L<https://github.com/martzcodes/node-whistle>
245  
246 =back
247  
248 =head1 AUTHOR
249  
250 Michael R. Davis
251  
252 =head1 COPYRIGHT AND LICENSE
253  
254 MIT License
255  
256 Copyright (c) 2023 Michael R. Davis
257  
258 =cut
259  
260 5 1;
↑ Summary script/perl-WebService-Whistle-Pet-Tracker-API-device.pl »