| 1 | package WebService::GarminConnect; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use WWW::OAuth; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use WWW::OAuth::Util qw( form_urldecode ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | #use LWP::ConsoleLogger::Everywhere (); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 7 | our $VERSION = '1.1.3'; # VERSION | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | WebService::GarminConnect - Access data from Garmin Connect | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 VERSION | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | version 1.1.3 | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | With WebService::GarminConnect, you can search the activities stored on the | ||||||||||||||||||||||||||||
| 28 | Garmin Connect site. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | use WebService::GarminConnect; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | my $gc = WebService::GarminConnect->new( username => 'myuser', | ||||||||||||||||||||||||||||
| 33 | password => 'password' ); | ||||||||||||||||||||||||||||
| 34 | my @activities = $gc->activities( limit => 20 ); | ||||||||||||||||||||||||||||
| 35 | foreach my $a ( @activities ) { | ||||||||||||||||||||||||||||
| 36 | my $name = $a->{name}; | ||||||||||||||||||||||||||||
| 37 | ... | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head2 new( %options ) | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Creates a new WebService::GarminConnect object. One or more options may be | ||||||||||||||||||||||||||||
| 45 | specified: | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =over | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =item username | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | (Required) The Garmin Connect username to use for searches. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =item password | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | (Required) The user's Garmin Connect password. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =item cache_dir | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | (Optional) Directory where the user's authentication token will be cached. | ||||||||||||||||||||||||||||
| 60 | If not specified, defaults to $HOME/.cache/webservice-garminconnect. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =item searchurl | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | (Optional) Override the default search URL for Garmin Connect. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =back | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 72 | 5 | my %options = @_; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # Check for mandatory options | ||||||||||||||||||||||||||||
| 75 | 5 | foreach my $required_option ( qw( username password ) ) { | |||||||||||||||||||||||||||
| 76 | croak "option \"$required_option\" is required" | ||||||||||||||||||||||||||||
| 77 | 8 | ▶ | unless defined $options{$required_option}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | return bless { | ||||||||||||||||||||||||||||
| 81 | username => $options{username}, | ||||||||||||||||||||||||||||
| 82 | password => $options{password}, | ||||||||||||||||||||||||||||
| 83 | cache_dir => $options{cache_dir}, | ||||||||||||||||||||||||||||
| 84 | 2 | ▶ | searchurl => $options{searchurl} || 'https://connectapi.garmin.com/activitylist-service/activities/search/activities', | ||||||||||||||||||||||||||
|
Condition50%1/2
$options{'searchurl'} || 'https://connectapi.garmin.com/activitylist-service/activities/search/activities'
MC/DC0%0/1
$options{'searchurl'} || 'https://connectapi.garmin.com/activitylist-service/activities/search/activities'
$options{'searchurl'}
Truth table50%1/2
A
A= $options{'searchurl'}
| |||||||||||||||||||||||||||||
| 85 | }, $self; | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 3 | ▶ | sub _login { | ||||||||||||||||||||||||||
|
Subroutine
_login: called CC=20 SCAR=59.6
| |||||||||||||||||||||||||||||
| 89 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Bail out if we're already logged in. | ||||||||||||||||||||||||||||
| 92 | 3 | ▶ | return if defined $self->{is_logged_in}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | my $ua = LWP::UserAgent->new(agent => 'GCM-iOS-5.7.2.1'); | |||||||||||||||||||||||||||
| 95 | 0 | $ua->cookie_jar( {} ); | |||||||||||||||||||||||||||
| 96 | 0 | push @{ $ua->requests_redirectable }, 'POST'; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # location for saved access token | ||||||||||||||||||||||||||||
| 99 | 0 | my $cache_path = $self->{cache_dir}; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | if (!defined $cache_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | $cache_path = (getpwuid($>))[7]."/.cache"; | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | -d $cache_path || mkdir $cache_path, 0700; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | $cache_path .= "/webservice-garminconnect"; | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | -d $cache_path || mkdir $cache_path, 0700; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | # untaint | ||||||||||||||||||||||||||||
| 107 | 0 | $self->{username} =~ m/([a-z0-9+\-_.=\?@]+)/i; | |||||||||||||||||||||||||||
| 108 | 0 | $cache_path .= "/${1}_oauth"; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # try saved access token | ||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if (open my $cache_fh, '<', $cache_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | (my $access_token = <$cache_fh>) =~ s/\s+//; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | $ua->default_header('Authorization', 'Bearer ' . $access_token); | |||||||||||||||||||||||||||
| 115 | 0 | $self->{useragent} = $ua; | |||||||||||||||||||||||||||
| 116 | 0 | $self->{is_logged_in} = 1; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # simple api call to validate | ||||||||||||||||||||||||||||
| 119 | 0 | eval { $self->profile }; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | return unless $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | my %sso_embed_params = ( | |||||||||||||||||||||||||||
| 124 | id => 'gauth-widget', | ||||||||||||||||||||||||||||
| 125 | embedWidget => 'true', | ||||||||||||||||||||||||||||
| 126 | gauthHost => 'https://sso.garmin.com/sso', | ||||||||||||||||||||||||||||
| 127 | ); | ||||||||||||||||||||||||||||
| 128 | 0 | my $uri = URI->new('https://sso.garmin.com/sso/embed'); | |||||||||||||||||||||||||||
| 129 | 0 | $uri->query_form(%sso_embed_params); | |||||||||||||||||||||||||||
| 130 | 0 | my $response = $ua->get($uri); | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | croak "Can't retrieve /sso/embed: " . $response->status_line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | unless $response->is_success; | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | my %signin_params = ( | |||||||||||||||||||||||||||
| 135 | id => 'gauth-widget', | ||||||||||||||||||||||||||||
| 136 | embedWidget => 'true', | ||||||||||||||||||||||||||||
| 137 | gauthHost => 'https://sso.garmin.com/sso/embed', | ||||||||||||||||||||||||||||
| 138 | service => 'https://sso.garmin.com/sso/embed', | ||||||||||||||||||||||||||||
| 139 | source => 'https://sso.garmin.com/sso/embed', | ||||||||||||||||||||||||||||
| 140 | redirectAfterAccountLoginUrl => 'https://sso.garmin.com/sso/embed', | ||||||||||||||||||||||||||||
| 141 | redirectAfterAccountCreationUrl => 'https://sso.garmin.com/sso/embed', | ||||||||||||||||||||||||||||
| 142 | ); | ||||||||||||||||||||||||||||
| 143 | 0 | $uri = URI->new('https://sso.garmin.com/sso/signin'); | |||||||||||||||||||||||||||
| 144 | 0 | $uri->query_form(%signin_params); | |||||||||||||||||||||||||||
| 145 | 0 | $response = $ua->get($uri); | |||||||||||||||||||||||||||
| 146 | 0 | ▶ | croak "Can't retrieve /sso/signin: " . $response->status_line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | unless $response->is_success; | ||||||||||||||||||||||||||||
| 148 | # get the CSRF token from the response, it's a hidden form field | ||||||||||||||||||||||||||||
| 149 | 0 | my $csrf_token; | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | if ($response->decoded_content =~ /name="_csrf"\s+value="(.+?)"/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | $csrf_token = $1; | |||||||||||||||||||||||||||
| 152 | } else { | ||||||||||||||||||||||||||||
| 153 | 0 | croak "couldn't find CSRF token"; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # submit login form with email and password | ||||||||||||||||||||||||||||
| 157 | $response = $ua->post($uri, Referer => "$uri", Content => { | ||||||||||||||||||||||||||||
| 158 | username => $self->{username}, | ||||||||||||||||||||||||||||
| 159 | password => $self->{password}, | ||||||||||||||||||||||||||||
| 160 | 0 | embed => 'true', | |||||||||||||||||||||||||||
| 161 | _csrf => $csrf_token, | ||||||||||||||||||||||||||||
| 162 | }); | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | croak "Can't submit login page: " . $response->status_line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | unless $response->is_success; | ||||||||||||||||||||||||||||
| 165 | 0 | my $title; | |||||||||||||||||||||||||||
| 166 | 0 | ▶ | if ($response->decoded_content =~ m:<title>(.+)</title>:) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | $title = $1; | |||||||||||||||||||||||||||
| 168 | } else { | ||||||||||||||||||||||||||||
| 169 | 0 | croak "couldn't find <title> in login response"; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | if ($title ne 'Success') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | croak "expected post-login <title> of \"Success\", not \"$title\""; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | 0 | my $ticket; | |||||||||||||||||||||||||||
| 175 | 0 | ▶ | if ($response->decoded_content =~ /embed\?ticket=([^"]+)"/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | $ticket = $1; | |||||||||||||||||||||||||||
| 177 | } else { | ||||||||||||||||||||||||||||
| 178 | 0 | croak "couldn't find ticket in login response"; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # get oauth1 token, these came from https://thegarth.s3.amazonaws.com/oauth_consumer.json | ||||||||||||||||||||||||||||
| 182 | # and are what the Garmin Connect mobile app uses. Perhaps we should | ||||||||||||||||||||||||||||
| 183 | # try to fetch these from there at runtime in case they ever change? | ||||||||||||||||||||||||||||
| 184 | 0 | my $oauth = WWW::OAuth->new( | |||||||||||||||||||||||||||
| 185 | client_id => "fc3e99d2-118c-44b8-8ae3-03370dde24c0", | ||||||||||||||||||||||||||||
| 186 | client_secret => "E08WAR897WEy2knn7aFBrvegVAf0AFdWBBF", | ||||||||||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | $uri = 'https://connectapi.garmin.com/oauth-service/oauth/' . | |||||||||||||||||||||||||||
| 191 | "preauthorized?ticket=$ticket&login-url=" . | ||||||||||||||||||||||||||||
| 192 | 'https://sso.garmin.com/sso/embed&accepts-mfa-tokens=true'; | ||||||||||||||||||||||||||||
| 193 | 0 | ▶ | $ua->add_handler(request_prepare => sub { $oauth->authenticate($_[0]) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 194 | 0 | $response = $ua->get($uri); | |||||||||||||||||||||||||||
| 195 | 0 | ▶ | croak "Can't retrieve oauth1 page: " . $response->status_line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | unless $response->is_success; | ||||||||||||||||||||||||||||
| 197 | 0 | my %response_data = @{form_urldecode($response->content)}; | |||||||||||||||||||||||||||
| 198 | 0 | foreach my $key ( qw( oauth_token oauth_token_secret ) ) { | |||||||||||||||||||||||||||
| 199 | 0 | ▶ | if (!defined $response_data{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 0 | croak "oauth response didn't include \"$key\""; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 0 | $oauth->token($response_data{oauth_token}); | |||||||||||||||||||||||||||
| 204 | 0 | $oauth->token_secret($response_data{oauth_token_secret}); | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 0 | $uri = 'https://connectapi.garmin.com/oauth-service/oauth/exchange/user/2.0'; | |||||||||||||||||||||||||||
| 207 | 0 | $response = $ua->post($uri); | |||||||||||||||||||||||||||
| 208 | 0 | ▶ | croak "Can't retrieve oauth1 page: " . $response->status_line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | unless $response->is_success; | ||||||||||||||||||||||||||||
| 210 | 0 | my $response_data = decode_json($response->content); | |||||||||||||||||||||||||||
| 211 | 0 | ▶ | if (!defined $response_data->{access_token}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 0 | croak "couldn't find access token in response"; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | # make subsequent calls use the access token in the Authorization header | ||||||||||||||||||||||||||||
| 216 | 0 | $ua->remove_handler('request_prepare'); | |||||||||||||||||||||||||||
| 217 | 0 | my $access_token = $response_data->{access_token}; | |||||||||||||||||||||||||||
| 218 | 0 | $ua->default_header('Authorization', 'Bearer ' . $access_token); | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | #$uri = 'https://connectapi.garmin.com/activitylist-service/activities/search/activities?limit=20&start=0'; | ||||||||||||||||||||||||||||
| 221 | #$response = $ua->get($uri); | ||||||||||||||||||||||||||||
| 222 | #croak "Can't retrieve activity search page: " . $response->status_line | ||||||||||||||||||||||||||||
| 223 | # unless $response->is_success; | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | # Record our logged-in status so future calls will skip login. | ||||||||||||||||||||||||||||
| 226 | 0 | $self->{useragent} = $ua; | |||||||||||||||||||||||||||
| 227 | 0 | $self->{is_logged_in} = 1; | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # save access token | ||||||||||||||||||||||||||||
| 230 | 0 | ▶ | if (open my $cache_fh, '>', $cache_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 0 | chmod 0600, $cache_fh; | |||||||||||||||||||||||||||
| 232 | 0 | print $cache_fh $access_token, "\n"; | |||||||||||||||||||||||||||
| 233 | 0 | close $cache_fh; | |||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 3 | ▶ | sub _api_raw { | ||||||||||||||||||||||||||
|
Subroutine
_api_raw: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 238 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 239 | 3 | my ($api, %opts) = @_; | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | # Ensure we are logged in | ||||||||||||||||||||||||||||
| 242 | 3 | $self->_login(); | |||||||||||||||||||||||||||
| 243 | 3 | my $ua = $self->{useragent}; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 3 | my $url = URI->new($self->{searchurl}); | |||||||||||||||||||||||||||
| 246 | 3 | $url->path($api); | |||||||||||||||||||||||||||
| 247 | 3 | $url->query_form(%opts); | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 3 | my $headers = [ | |||||||||||||||||||||||||||
| 250 | 'NK' => 'NT', | ||||||||||||||||||||||||||||
| 251 | 'X-app-ver' => '4.71.1.4', | ||||||||||||||||||||||||||||
| 252 | 'X-lang' => 'en-US', | ||||||||||||||||||||||||||||
| 253 | 'X-Requested-With' => 'XMLHttpRequest', | ||||||||||||||||||||||||||||
| 254 | ]; | ||||||||||||||||||||||||||||
| 255 | 3 | my $request = HTTP::Request->new('GET', $url, $headers); | |||||||||||||||||||||||||||
| 256 | 3 | my $response = $ua->request($request); | |||||||||||||||||||||||||||
| 257 | 3 | ▶ | croak "Can't make $api request: " . $response->status_line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | unless $response->is_success; | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 3 | return $response; | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 1 | ▶ | sub _api { | ||||||||||||||||||||||||||
|
Subroutine
_api: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 264 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 265 | 1 | my ($api, %opts) = @_; | |||||||||||||||||||||||||||
| 266 | 1 | my $json = JSON->new(); | |||||||||||||||||||||||||||
| 267 | 1 | return $json->decode($self->_api_raw($api, %opts)->content()); | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | =head2 profile | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | Returns the user's Garmin Connect profile | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =cut | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 0 | ▶ | sub profile { | ||||||||||||||||||||||||||
|
Subroutine
profile: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 277 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 278 | 0 | return $self->_api("/userprofile-service/socialProfile"); | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =head2 activities( %search_criteria ) | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | Returns a list of activities matching the requested criteria. If no criteria | ||||||||||||||||||||||||||||
| 284 | are specified, returns all the user's activities. Possible criteria: | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | =over | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =item limit | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | (Optional) The maximum number of activities to return. If not specified, | ||||||||||||||||||||||||||||
| 291 | all the user's activities will be returned. | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =item pagesize | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | (Optional) The number of activities to return in each call to Garmin | ||||||||||||||||||||||||||||
| 296 | Connect. (One call to this subroutine may call Garmin Connect several | ||||||||||||||||||||||||||||
| 297 | times to retrieve all the requested activities.) Defaults to 50. | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | =back | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | =cut | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 0 | ▶ | sub activities { | ||||||||||||||||||||||||||
|
Subroutine
activities: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 304 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 305 | 0 | my %opts = @_; | |||||||||||||||||||||||||||
| 306 | 0 | my $json = JSON->new(); | |||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | # Ensure we are logged in | ||||||||||||||||||||||||||||
| 309 | 0 | $self->_login(); | |||||||||||||||||||||||||||
| 310 | 0 | my $ua = $self->{useragent}; | |||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | # We can only fetch a fixed number of activities at a time. | ||||||||||||||||||||||||||||
| 313 | 0 | my @activities; | |||||||||||||||||||||||||||
| 314 | 0 | my $start = 0; | |||||||||||||||||||||||||||
| 315 | 0 | my $pagesize = 50; | |||||||||||||||||||||||||||
| 316 | 0 | ▶ | if( defined $opts{pagesize} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 0 | ▶ | if( $opts{pagesize} > 0 && $opts{pagesize} < 50 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opts{"pagesize"} > 0 and $opts{"pagesize"} < 50
MC/DC0%0/2
$opts{"pagesize"} > 0 and $opts{"pagesize"} < 50
$opts{"pagesize"} > 0
$opts{"pagesize"} < 50
Truth table0%0/3
A and B
A= $opts{"pagesize"} > 0B= $opts{"pagesize"} < 50
| |||||||||||||||||||||||||||||
| 318 | 0 | $pagesize = $opts{pagesize}; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | # Special case when the limit is smaller than one page. | ||||||||||||||||||||||||||||
| 323 | 0 | ▶ | if( defined $opts{limit} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 0 | ▶ | if( $opts{limit} < $pagesize ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 0 | $pagesize = $opts{limit}; | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 0 | my $data = []; | |||||||||||||||||||||||||||
| 330 | do { | ||||||||||||||||||||||||||||
| 331 | # Make a search request | ||||||||||||||||||||||||||||
| 332 | 0 | $data = $self->_api("/activitylist-service/activities/search/activities", start => $start, limit => $pagesize); | |||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | # Add this set of activities to the list. | ||||||||||||||||||||||||||||
| 335 | 0 | foreach my $activity ( @{$data} ) { | |||||||||||||||||||||||||||
| 336 | 0 | ▶ | if( defined $opts{limit} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | # add this activity only if we're under the limit | ||||||||||||||||||||||||||||
| 338 | 0 | ▶ | if( @activities < $opts{limit} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 339 | 0 | push @activities, { activity => $activity }; | |||||||||||||||||||||||||||
| 340 | } else { | ||||||||||||||||||||||||||||
| 341 | 0 | $data = []; # stop retrieving more activities | |||||||||||||||||||||||||||
| 342 | 0 | last; | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | } else { | ||||||||||||||||||||||||||||
| 345 | 0 | push @activities, { activity => $activity }; | |||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | # Increment the start offset for the next request. | ||||||||||||||||||||||||||||
| 350 | 0 | $start += $pagesize; | |||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 0 | ▶ | } while( @{$data} > 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 0 | return @activities; | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | Joel Loudermilk, C<< <joel at loudermilk.org> >> | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | =head1 BUGS | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | Please report any bugs or feature requests to L<https://github.com/jlouder/garmin-connect-perl/issues>. | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | perldoc WebService::GarminConnect | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | You can also look for information at: | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | =over 4 | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | L<http://annocpan.org/dist/WebService-GarminConnect> | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | L<http://cpanratings.perl.org/d/WebService-GarminConnect> | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | L<http://search.cpan.org/dist/WebService-GarminConnect> | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | =item * GitHub Repository | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | L<https://github.com/jlouder/garmin-connect-perl> | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | =back | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | Copyright 2023 Joel Loudermilk. | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | This program is free software: you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 402 | it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||
| 403 | the Free Software Foundation, either version 3 of the License, or | ||||||||||||||||||||||||||||
| 404 | (at your option) any later version. | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 407 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 408 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||
| 409 | GNU General Public License for more details. | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||
| 412 | along with this program. If not, see L<http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | =cut | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 7 | 1; # End of WebService::GarminConnect | |||||||||||||||||||||||||||