Skip to content

Commit

Permalink
Renamed CACHE_FILE to AUTH_CACHE_FILE; added _authentication_cache_fi…
Browse files Browse the repository at this point in the history
…le()
  • Loading branch information
stevieb9 committed Apr 14, 2022
1 parent 4ac7fc8 commit 3b41b17
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Revision history for Tesla-API
- Added _api_attempts(); returns the number of attempts we've made for each
individual call to Tesla's API
- Renamed _cache() to _api_cache()
- Renamed CACHE_FILE to AUTH_CACHE_FILE
- Added _authentication_cache_file() which allows changing the default
AUTH_CACHE_FILE path to something custom (primarily for testing)
-

0.10 2022-04-11
- Remove erroneous call example from Tesla::Vehicle from the SYNOPSIS
Expand Down
34 changes: 22 additions & 12 deletions lib/Tesla/API.pm
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,14 @@ use constant {
API_CACHE_PERSIST => 0,
API_CACHE_TIMEOUT_SECONDS => 2,
API_TIMEOUT_RETRIES => 3,
CACHE_FILE => "$home_dir/tesla_api_cache.json",
AUTH_CACHE_FILE => "$home_dir/tesla_auth_cache.json",
ENDPOINTS_FILE => dist_file('Tesla-API', 'endpoints.json'),
OPTION_CODES_FILE => dist_file('Tesla-API', 'option_codes.json'),
TOKEN_EXPIRY_WINDOW => 5,
URL_API => 'https://owner-api.teslamotors.com/',
URL_ENDPOINTS => 'https://raw.githubusercontent.com/tdorssers/TeslaPy/master/teslapy/endpoints.json',
URL_OPTION_CODES => 'https://raw.githubusercontent.com/tdorssers/TeslaPy/master/teslapy/option_codes.json',
URL_AUTH => 'https://auth.tesla.com/oauth2/v3/authorize',
URL_TOKEN => 'https://auth.tesla.com/oauth2/v3/token',
USERAGENT_STRING => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0',
USERAGENT_TIMEOUT => 180,
URL_AUTH => 'https://auth.tesla.com/oauth2/v3/authorize', URL_TOKEN => 'https://auth.tesla.com/oauth2/v3/token', USERAGENT_STRING => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0', USERAGENT_TIMEOUT => 180,
};

# Public object methods
Expand Down Expand Up @@ -317,7 +314,7 @@ sub _access_token {

my ($self) = @_;

if (! -e CACHE_FILE) {
if (! -e $self->_authentication_cache_file) {
my $auth_code = $self->_authentication_code;
$self->_access_token_generate($auth_code);
}
Expand All @@ -342,7 +339,11 @@ sub _access_token_data {
return $self->{cache_data} if $self->{cache_data};

{
open my $fh, '<', CACHE_FILE or die "Can't open Tesla cache file " . CACHE_FILE . ": $!";
open my $fh, '<', $self->_authentication_cache_file or die
"Can't open Tesla cache file " .
$self->_authentication_cache_file .
": $!";

my $json = <$fh>;
$self->{cache_data} = decode_json($json);
}
Expand Down Expand Up @@ -463,7 +464,7 @@ sub _access_token_update {

$self->_access_token_data($token_data);

open my $fh, '>', CACHE_FILE or die $!;
open my $fh, '>', $self->_authentication_cache_file or die $!;

print $fh JSON->new->allow_nonref->encode($token_data);
}
Expand All @@ -478,6 +479,15 @@ sub _api_attempts {

return $self->{api_attempts} || 0;
}
sub _authentication_cache_file {
my ($self, $filename) = @_;

if (defined $filename) {
$self->{authentication_cache_file} = $filename;
}

return $self->{authentication_cache_file} || AUTH_CACHE_FILE;
}
sub _authentication_code {
# If an access token is unavailable, prompt the user with a URL to
# authenticate to Tesla, and have them paste in the resulting URL
Expand Down Expand Up @@ -662,8 +672,8 @@ be redirected again to a "Page Not Found" page, in which you must copy the URL
from the address bar and paste it back into the console.
We then internally generate an access token for you, store it in a
C<tesla_api_cache.json> file in your home directory, and use it on all subsequent
accesses.
C<tesla_auth_cache.json> file in your home directory, and use it on all
subsequent accesses.
B<NOTE>: If you do not have a Tesla account, you can still instantiate a
L<Tesla::API> object by supplying the C<< unauthenticated => 1 >> parameter
Expand Down Expand Up @@ -1085,14 +1095,14 @@ I<Default>: C<3>
I<Override>: None
=head2 CACHE_FILE
=head2 AUTH_CACHE_FILE
The path and filename of the file we'll store the Tesla API access token
information.
I<Default>: C<$home_dir/tesla_api_cache.json>
I<Override>: None
I<Override>: C<_authentication_cache_file()>, used primarily for unit testing.
=head2 ENDPOINTS_FILE
Expand Down

0 comments on commit 3b41b17

Please sign in to comment.