jlouder / garmin-connect-perl

Perl module for accessing Garmin Connect data
2 stars 3 forks source link

More general _api method (low-priority feature suggestion) #9

Open kmgwalker opened 8 months ago

kmgwalker commented 8 months ago

Thanks for the great module! It's an essential part of my bike/Garmin workflow.

Feature suggestion: Something like the _api method, but which returns raw content rather than trying to parse JSON.

Why it would be useful: My workflow is to scan all recent activities, then download exported *.tcx versions of any activities which I have not already downloaded (GET ".../download-service/export/tcx/activity/$aid"). I can't use _api directly, since it insists on treating the downloaded data as JSON to be decoded.

Why it's low priority: It's easy enough to copy and edit the source of the _api method to do what I want.

sample code:

(adapted from $gc->_api method; $self replaced by $gc instantiated above) sub kw_gcget { my ($path, %arg) = @;

$gc->_login();  # (in case we are not already logged in)

my $url = URI->new($gc->{searchurl});
$url->path($path);

my $ua = $gc->{useragent};
my $headers = [
    'NK' => 'NT',
    'X-app-ver' => '4.71.1.4',
    'X-lang' => 'en-US',
    'X-Requested-With' => 'XMLHttpRequest',
];
my $request = HTTP::Request->new('GET', $url, $headers);
my $response = $ua->request($request);
die "!! kw_gc_get GET $url failed: " . $response->status_line unless ($response->is_success);

if ($arg{content_file}) {
    $response->content > io($arg{content_file});
}
else {
    return $response->content;
}

}

jlouder commented 8 months ago

Thank you for this suggestion, and by my count you are now the third known user of this module! :)

I didn't realize there were API calls that didn't return JSON. I'll definitely do something to make this work for you without having to copy and modify the code. But it might be whenever I next have to make other changes. (This module tends to be something I only think about when it quits working.)

Something I'd like to do is be able to download all of my activities (including GPS points) as a sort of data backup. There are so many Garmin APIs that I wouldn't want to try to expose them all as first-class methods, so I was thinking as well that the _api method should be generic enough that it can call anything.

kmgwalker commented 8 months ago

Sounds good -- thanks.

I'm not sure the export functionality is, strictly speaking, an api call. But downloading "download-service/export/tcx/activity/$aid" and "download-service/export/gpx/activity/$aid" (where $aid is the activity ID) gets the job done.