| 1 | package Tesla::API; | ||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||
| 3 | 28 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | 28 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||
| 6 | 28 | ▶ | use Carp qw(croak confess); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | 28 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 28 | ▶ | use Digest::SHA qw(sha256_hex); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | 28 | ▶ | use FindBin qw($RealBin); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 10 | 28 | ▶ | use File::Copy; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | 28 | ▶ | use File::HomeDir; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | 28 | ▶ | use File::Share qw(:all); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 13 | 28 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 14 | 28 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 15 | 28 | ▶ | use MIME::Base64 qw(encode_base64url); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 16 | 28 | ▶ | use WWW::Mechanize; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 17 | 28 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||
| 19 | 28 | our $VERSION = '1.01'; | |||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||
| 21 | 28 | $| = 1; | |||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||
| 23 | 28 | my $home_dir; | |||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | # The %api_cache hash is a cache for Tesla API call data across all objects. | ||||||||||||||||||||||||||||||||||||||
| 26 | # It is managed by the _api_cache() private method. Each cache slot contains the | ||||||||||||||||||||||||||||||||||||||
| 27 | # time() that it was stored, and will time out after | ||||||||||||||||||||||||||||||||||||||
| 28 | # API_CACHE_TIMEOUT_SECONDS/api_cache_time() | ||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||
| 30 | my %api_cache; | ||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||
| 32 | BEGIN { | ||||||||||||||||||||||||||||||||||||||
| 33 | 28 | ▶ | $home_dir = File::HomeDir->my_home; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||
| 36 | use constant { | ||||||||||||||||||||||||||||||||||||||
| 37 | DEBUG_CACHE => $ENV{DEBUG_TESLA_API_CACHE}, | ||||||||||||||||||||||||||||||||||||||
| 38 | API_CACHE_PERSIST => 0, | ||||||||||||||||||||||||||||||||||||||
| 39 | API_CACHE_TIMEOUT_SECONDS => 2, | ||||||||||||||||||||||||||||||||||||||
| 40 | API_TIMEOUT_RETRIES => 3, | ||||||||||||||||||||||||||||||||||||||
| 41 | AUTH_CACHE_FILE => "$home_dir/tesla_auth_cache.json", | ||||||||||||||||||||||||||||||||||||||
| 42 | ENDPOINTS_FILE => $ENV{TESLA_API_ENDPOINTS_FILE} // dist_file('Tesla-API', 'endpoints.json'), | ||||||||||||||||||||||||||||||||||||||
| 43 | 28 | ▶ | OPTION_CODES_FILE => $ENV{TESLA_API_OPTIONCODES_FILE} // dist_file('Tesla-API', 'option_codes.json'), | ||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
$ENV{"TESLA_API_ENDPOINTS_FILE"} // dist_file("Tesla-API", "endpoints.json")
$ENV{"TESLA_API_OPTIONCODES_FILE"} // dist_file("Tesla-API", "option_codes.json")
MC/DC0%0/2
$ENV{"TESLA_API_ENDPOINTS_FILE"} // dist_file("Tesla-API", "endpoints.json")
$ENV{"TESLA_API_ENDPOINTS_FILE"}
dist_file("Tesla-API", "endpoints.json")
MC/DC0%0/2
$ENV{"TESLA_API_OPTIONCODES_FILE"} // dist_file("Tesla-API", "option_codes.json")
$ENV{"TESLA_API_OPTIONCODES_FILE"}
dist_file("Tesla-API", "option_codes.json")
Truth table66%2/3
A // B
A= $ENV{"TESLA_API_ENDPOINTS_FILE"}B= dist_file("Tesla-API", "endpoints.json")
Truth table66%2/3
A // B
A= $ENV{"TESLA_API_OPTIONCODES_FILE"}B= dist_file("Tesla-API", "option_codes.json")
| |||||||||||||||||||||||||||||||||||||||
| 44 | TOKEN_EXPIRY_WINDOW => 5, | ||||||||||||||||||||||||||||||||||||||
| 45 | URL_API => 'https://owner-api.teslamotors.com/', | ||||||||||||||||||||||||||||||||||||||
| 46 | URL_ENDPOINTS => 'https://raw.githubusercontent.com/tdorssers/TeslaPy/master/teslapy/endpoints.json', | ||||||||||||||||||||||||||||||||||||||
| 47 | URL_OPTION_CODES => 'https://raw.githubusercontent.com/tdorssers/TeslaPy/master/teslapy/option_codes.json', | ||||||||||||||||||||||||||||||||||||||
| 48 | URL_AUTH => 'https://auth.tesla.com/oauth2/v3/authorize', | ||||||||||||||||||||||||||||||||||||||
| 49 | URL_TOKEN => 'https://auth.tesla.com/oauth2/v3/token', | ||||||||||||||||||||||||||||||||||||||
| 50 | USERAGENT_STRING => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0', | ||||||||||||||||||||||||||||||||||||||
| 51 | USERAGENT_TIMEOUT => 180, | ||||||||||||||||||||||||||||||||||||||
| 52 | 28 | ▶ | }; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||
| 54 | # Public object methods | ||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||
| 56 | sub new { | ||||||||||||||||||||||||||||||||||||||
| 57 | 28 | ▶ | my ($class, %params) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 58 | 28 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||
| 60 | 28 | $self->endpoints; | |||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||
| 62 | # Return early if the user specifies that they are | ||||||||||||||||||||||||||||||||||||||
| 63 | # not authenticated. Use this param for unit tests | ||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||
| 65 | 28 | ▶ | if ($params{unauthenticated}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 66 | 24 | return $self; | |||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||
| 69 | 4 | $self->api_cache_persist($params{api_cache_persist}); | |||||||||||||||||||||||||||||||||||||
| 70 | 4 | $self->api_cache_time($params{api_cache_time}); | |||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||
| 72 | 4 | $self->mech; | |||||||||||||||||||||||||||||||||||||
| 73 | 4 | $self->_access_token; | |||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||
| 75 | 4 | return $self; | |||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||
| 77 | sub api { | ||||||||||||||||||||||||||||||||||||||
| 78 | 7 | ▶ | my ($self, %params) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
api: called CC=13 SCAR=26.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||
| 80 | 7 | my $endpoint_name = $params{endpoint}; | |||||||||||||||||||||||||||||||||||||
| 81 | 7 | my $id = $params{id}; | |||||||||||||||||||||||||||||||||||||
| 82 | 7 | my $api_params = $params{api_params}; | |||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||
| 84 | 7 | ▶ | if (! defined $endpoint_name) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 85 | 1 | croak "Tesla::API::api() requires an endpoint name sent in"; | |||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||
| 88 | 6 | my $endpoint = $self->endpoints($endpoint_name); | |||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||
| 90 | 6 | my $type = $endpoint->{TYPE}; | |||||||||||||||||||||||||||||||||||||
| 91 | 6 | my $auth = $endpoint->{AUTH}; | |||||||||||||||||||||||||||||||||||||
| 92 | 6 | my $uri = $endpoint->{URI}; | |||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||
| 94 | 6 | ▶ | if ($uri =~ /\{/) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | if (! defined $id || $id !~ /^\d+$/) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $id and $id =~ /^\d+$/
MC/DC0%0/2
defined $id and $id =~ /^\d+$/
defined $id
$id =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $idB= $id =~ /^\d+$/
| |||||||||||||||||||||||||||||||||||||||
| 96 | 1 | croak "Endpoint $endpoint_name requires an \$id as an integer"; | |||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $uri =~ s/\{.*?\}/$id/; | |||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||
| 101 | 5 | ▶ | $id //= 0; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$id //= 0
MC/DC0%0/1
$id //= 0
$id
Truth table50%1/2
A
A= $id
| |||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||
| 103 | # Return early if all cache mechanisms check out | ||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||
| 105 | 5 | ▶ | if ($self->api_cache_persist || $self->api_cache_time) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->api_cache_persist or $self->api_cache_time
MC/DC0%0/2
$self->api_cache_persist or $self->api_cache_time
$self->api_cache_persist
$self->api_cache_time
Truth table66%2/3
A or B
A= $self->api_cache_persistB= $self->api_cache_time
| |||||||||||||||||||||||||||||||||||||||
| 106 | 5 | my $cache = $self->_api_cache(endpoint => $endpoint_name, id => $id); | |||||||||||||||||||||||||||||||||||||
| 107 | 5 | ▶ | if ($cache) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 108 | 2 | ▶ | if ($self->api_cache_persist || time - $cache->{time} <= $self->api_cache_time) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->api_cache_persist or time - $cache->{"time"} <= $self->api_cache_time
MC/DC0%0/2
$self->api_cache_persist or time - $cache->{"time"} <= $self->api_cache_time
$self->api_cache_persist
time - $cache->{"time"} <= $self->api_cache_time
Truth table66%2/3
A or B
A= $self->api_cache_persistB= time - $cache->{"time"} <= $self->api_cache_time
| |||||||||||||||||||||||||||||||||||||||
| 109 | 2 | warn "Returning cache for $endpoint_name/$id pair...\n" if DEBUG_CACHE; | |||||||||||||||||||||||||||||||||||||
| 110 | 2 | return $cache->{data}; | |||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||
| 115 | 3 | my $url = URI->new(URL_API . $uri); | |||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||
| 117 | 3 | my $header = ['Content-Type' => 'application/json; charset=UTF-8']; | |||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||
| 119 | 3 | ▶ | if ($auth) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 120 | 3 | my $token_string = "Bearer " . $self->_access_token; | |||||||||||||||||||||||||||||||||||||
| 121 | 3 | push @$header, 'Authorization' => $token_string; | |||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||
| 124 | 3 | my $request = HTTP::Request->new( | |||||||||||||||||||||||||||||||||||||
| 125 | $type, | ||||||||||||||||||||||||||||||||||||||
| 126 | $url, | ||||||||||||||||||||||||||||||||||||||
| 127 | $header, | ||||||||||||||||||||||||||||||||||||||
| 128 | JSON->new->allow_nonref->encode($api_params) | ||||||||||||||||||||||||||||||||||||||
| 129 | ); | ||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||
| 131 | 3 | my ($success, $code, $response_data) = $self->_tesla_api_call($request); | |||||||||||||||||||||||||||||||||||||
| 132 | 3 | my $data = $self->_decode($response_data); | |||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||
| 134 | 3 | ▶ | if ($data->{error}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 135 | 0 | return $data; | |||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | $self->_api_cache( | ||||||||||||||||||||||||||||||||||||||
| 139 | endpoint => $endpoint_name, | ||||||||||||||||||||||||||||||||||||||
| 140 | id => $id, | ||||||||||||||||||||||||||||||||||||||
| 141 | data => $data->{response} | ||||||||||||||||||||||||||||||||||||||
| 142 | 3 | ); | |||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||
| 144 | 3 | return $data->{response}; | |||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||
| 146 | sub api_cache_clear { | ||||||||||||||||||||||||||||||||||||||
| 147 | 7 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
api_cache_clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 148 | 7 | %api_cache = (); | |||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||
| 150 | sub api_cache_persist { | ||||||||||||||||||||||||||||||||||||||
| 151 | 17 | ▶ | my ($self, $persist) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
api_cache_persist: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 152 | 17 | ▶ | if (defined $persist) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 153 | 3 | $self->{api_cache_persist} = $persist; | |||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||
| 155 | 17 | ▶ | return $self->{api_cache_persist} // API_CACHE_PERSIST; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"api_cache_persist"} // 0
MC/DC100%1/1
$self->{"api_cache_persist"} // 0
$self->{"api_cache_persist"}
Truth table100%2/2
A
A= $self->{"api_cache_persist"}
| |||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||
| 157 | sub api_cache_time { | ||||||||||||||||||||||||||||||||||||||
| 158 | 20 | ▶ | my ($self, $cache_seconds) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
api_cache_time: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 159 | 20 | ▶ | if (defined $cache_seconds) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 160 | 10 | ▶ | if ($cache_seconds !~ /^\d+$/) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 161 | 5 | croak "api_cache_time() requires an int as \$cache_seconds param"; | |||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||
| 163 | 5 | $self->{api_cache_time} = $cache_seconds; | |||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||
| 165 | 15 | ▶ | return $self->{api_cache_time} // API_CACHE_TIMEOUT_SECONDS; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"api_cache_time"} // 2
MC/DC100%1/1
$self->{"api_cache_time"} // 2
$self->{"api_cache_time"}
Truth table100%2/2
A
A= $self->{"api_cache_time"}
| |||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||
| 167 | sub endpoints { | ||||||||||||||||||||||||||||||||||||||
| 168 | 45 | ▶ | my ($self, $endpoint) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
endpoints: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||
| 170 | 45 | ▶ | if (! $self->{endpoints} || $self->{reset_endpoints_data}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $self->{"endpoints"} or $self->{"reset_endpoints_data"}
MC/DC100%2/2
not $self->{"endpoints"} or $self->{"reset_endpoints_data"}
not $self->{"endpoints"}
$self->{"reset_endpoints_data"}
Truth table100%3/3
not(A) or B
A= not $self->{"endpoints"}B= $self->{"reset_endpoints_data"}
| |||||||||||||||||||||||||||||||||||||||
| 171 | 30 | $self->{reset_endpoints_data} = 0; | |||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||
| 173 | 30 | my $json_endpoints; | |||||||||||||||||||||||||||||||||||||
| 174 | { | ||||||||||||||||||||||||||||||||||||||
| 175 | 30 | local $/; | |||||||||||||||||||||||||||||||||||||
| 176 | 30 | ▶ | open my $fh, '<', ENDPOINTS_FILE | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 177 | 0 | or die "Can't open ${\ENDPOINTS_FILE}: $!"; | |||||||||||||||||||||||||||||||||||||
| 178 | 30 | $json_endpoints = <$fh>; | |||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||
| 181 | 30 | my $perl_endpoints = decode_json($json_endpoints); | |||||||||||||||||||||||||||||||||||||
| 182 | 30 | $self->{endpoints} = $perl_endpoints; | |||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||
| 185 | 45 | ▶ | if ($endpoint) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 186 | 6 | ▶ | if (! exists $self->{endpoints}{$endpoint}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 187 | 0 | croak "Tesla API endpoint $endpoint does not exist"; | |||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||
| 189 | 6 | return $self->{endpoints}{$endpoint}; | |||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||
| 192 | 39 | return $self->{endpoints}; | |||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||
| 194 | sub mech { | ||||||||||||||||||||||||||||||||||||||
| 195 | 10 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
mech: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||
| 197 | 10 | ▶ | if ($self->{mech} && ! $self->{mech_reset}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"mech"} and not $self->{"mech_reset"}
MC/DC50%1/2
$self->{"mech"} and not $self->{"mech_reset"}
$self->{"mech"}
not $self->{"mech_reset"}
Truth table66%2/3
A and not(B)
A= $self->{"mech"}B= not $self->{"mech_reset"}
| |||||||||||||||||||||||||||||||||||||||
| 198 | 4 | return $self->{mech}; | |||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||
| 201 | 6 | my $www_mech = WWW::Mechanize->new( | |||||||||||||||||||||||||||||||||||||
| 202 | agent => $self->useragent_string, | ||||||||||||||||||||||||||||||||||||||
| 203 | autocheck => 0, | ||||||||||||||||||||||||||||||||||||||
| 204 | cookie_jar => {}, | ||||||||||||||||||||||||||||||||||||||
| 205 | timeout => $self->useragent_timeout | ||||||||||||||||||||||||||||||||||||||
| 206 | ); | ||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||
| 208 | 6 | $self->{mech} = $www_mech; | |||||||||||||||||||||||||||||||||||||
| 209 | 6 | $self->{mech_reset} = 0; | |||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||
| 211 | 6 | return $self->{mech}; | |||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||
| 213 | sub option_codes { | ||||||||||||||||||||||||||||||||||||||
| 214 | 1165 | ▶ | my ($self, $code) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
option_codes: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||
| 216 | 1165 | ▶ | if (! $self->{option_codes} || $self->{reset_option_codes_data}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $self->{"option_codes"} or $self->{"reset_option_codes_data"}
MC/DC100%2/2
not $self->{"option_codes"} or $self->{"reset_option_codes_data"}
not $self->{"option_codes"}
$self->{"reset_option_codes_data"}
Truth table100%3/3
not(A) or B
A= not $self->{"option_codes"}B= $self->{"reset_option_codes_data"}
| |||||||||||||||||||||||||||||||||||||||
| 217 | 3 | print "RESET DATA\n"; | |||||||||||||||||||||||||||||||||||||
| 218 | 3 | $self->{reset_option_codes_data} = 0; | |||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||
| 220 | 3 | my $json_option_codes; | |||||||||||||||||||||||||||||||||||||
| 221 | { | ||||||||||||||||||||||||||||||||||||||
| 222 | 3 | local $/; | |||||||||||||||||||||||||||||||||||||
| 223 | 3 | ▶ | open my $fh, '<', OPTION_CODES_FILE | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 224 | 0 | or die "Can't open ${\OPTION_CODES_FILE}: $!"; | |||||||||||||||||||||||||||||||||||||
| 225 | 3 | $json_option_codes = <$fh>; | |||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||
| 228 | 3 | my $perl_option_codes = decode_json($json_option_codes); | |||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||
| 230 | 3 | $self->{option_codes} = $perl_option_codes; | |||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||
| 233 | 1165 | ▶ | if ($code) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 234 | 1157 | ▶ | if (! exists $self->{option_codes}{$code}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 235 | 1 | croak "Tesla API option code $code does not exist"; | |||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||
| 237 | 1156 | return $self->{option_codes}{$code}; | |||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||
| 240 | 8 | return $self->{option_codes}; | |||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||
| 242 | sub update_data_files { | ||||||||||||||||||||||||||||||||||||||
| 243 | 5 | ▶ | my ($self, $url_type) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
update_data_files: called CC=16 SCAR=28.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||
| 245 | 5 | my @urls_to_process; | |||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||
| 247 | 5 | ▶ | if (defined $url_type) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 248 | 5 | ▶ | push @urls_to_process, URL_ENDPOINTS if $url_type eq 'endpoints'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 249 | 5 | ▶ | push @urls_to_process, URL_OPTION_CODES if $url_type eq 'option_codes'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||
| 251 | else { | ||||||||||||||||||||||||||||||||||||||
| 252 | 0 | @urls_to_process = (ENDPOINTS_FILE, OPTION_CODES_FILE); | |||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||
| 255 | 5 | for my $data_url (@urls_to_process) { | |||||||||||||||||||||||||||||||||||||
| 256 | 5 | my $filename; | |||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||
| 258 | 5 | ▶ | if ($data_url =~ /.*\/(\w+\.json)$/) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 259 | 5 | $filename = $1; | |||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||
| 262 | 5 | ▶ | if (! defined $filename) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 263 | 0 | croak "Couldn't extract the filename from '$data_url'"; | |||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||
| 266 | 5 | (my $data_method = $filename) =~ s/\.json//; | |||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||
| 268 | 5 | my $url = URI->new($data_url); | |||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||
| 270 | 5 | my $request = HTTP::Request->new('GET', $url,); | |||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||
| 272 | 5 | my ($success, $code, $response_data) = $self->_tesla_api_call($request); | |||||||||||||||||||||||||||||||||||||
| 273 | 5 | my $new_data = decode_json($response_data); | |||||||||||||||||||||||||||||||||||||
| 274 | 5 | my $existing_data = $self->$data_method; | |||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||
| 276 | 5 | my $data_differs; | |||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||
| 278 | 5 | ▶ | if (scalar keys %$new_data != scalar keys %$existing_data) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 279 | 2 | $data_differs = 1; | |||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||
| 282 | 5 | ▶ | if (! $data_differs) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 283 | 3 | for my $key (keys %$new_data) { | |||||||||||||||||||||||||||||||||||||
| 284 | 1071 | ▶ | if (! exists $existing_data->{$key}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 285 | 1 | $data_differs = 1; | |||||||||||||||||||||||||||||||||||||
| 286 | 1 | last; | |||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||
| 291 | 5 | ▶ | if ($data_differs) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||
| 293 | 3 | ▶ | my $file = $filename =~ /endpoints/ | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 294 | ? ENDPOINTS_FILE | ||||||||||||||||||||||||||||||||||||||
| 295 | : OPTION_CODES_FILE ; | ||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||
| 297 | 3 | ▶ | if ($filename =~ /endpoints/) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 298 | 2 | $self->{reset_endpoints_data} = 1; | |||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||
| 300 | else { | ||||||||||||||||||||||||||||||||||||||
| 301 | 1 | $self->{reset_option_codes_data} = 1; | |||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||
| 304 | # Make a backup copy | ||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||
| 306 | 3 | my $backup = "$file." . time; | |||||||||||||||||||||||||||||||||||||
| 307 | 3 | ▶ | copy($file, $backup) or die "Can't create $file backup file!: $!"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||
| 309 | 3 | chmod 0644, $file; | |||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||
| 311 | 3 | ▶ | open my $fh, '>', "$file" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 312 | or die "Can't open '$file' for writing: $!"; | ||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||
| 314 | 3 | print $fh JSON->new->pretty->encode($new_data); | |||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||
| 318 | sub useragent_string { | ||||||||||||||||||||||||||||||||||||||
| 319 | 8 | ▶ | my ($self, $ua_string) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
useragent_string: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||
| 321 | 8 | ▶ | if (defined $ua_string) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 322 | 1 | $self->{useragent_string} = $ua_string; | |||||||||||||||||||||||||||||||||||||
| 323 | 1 | $self->{mech_reset} = 1; | |||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||
| 326 | 8 | ▶ | return $self->{useragent_string} // USERAGENT_STRING; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"useragent_string"} // "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0"
MC/DC100%1/1
$self->{"useragent_string"} // "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0"
$self->{"useragent_string"}
Truth table100%2/2
A
A= $self->{"useragent_string"}
| |||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||
| 328 | sub useragent_timeout { | ||||||||||||||||||||||||||||||||||||||
| 329 | 21 | ▶ | my ($self, $timeout) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
useragent_timeout: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||
| 331 | 21 | ▶ | if (defined $timeout) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 332 | 14 | ▶ | if ($timeout !~ /^\d+?(?:\.\d+)?$/) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 333 | 9 | croak "useragent_timeout() requires an integer or float"; | |||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||
| 335 | 5 | $self->{useragent_timeout} = $timeout; | |||||||||||||||||||||||||||||||||||||
| 336 | 5 | $self->{mech_reset} = 1; | |||||||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||
| 339 | 12 | ▶ | return $self->{useragent_timeout} // USERAGENT_TIMEOUT; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"useragent_timeout"} // 180
MC/DC100%1/1
$self->{"useragent_timeout"} // 180
$self->{"useragent_timeout"}
Truth table100%2/2
A
A= $self->{"useragent_timeout"}
| |||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||
| 342 | # Private methods | ||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||
| 344 | sub _access_token { | ||||||||||||||||||||||||||||||||||||||
| 345 | # Returns the access token from the cache file or generates | ||||||||||||||||||||||||||||||||||||||
| 346 | # that cache file (with token) if it isn't available | ||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||
| 348 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_access_token: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||
| 350 | 0 | ▶ | if (! -e $self->_authentication_cache_file) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 351 | 0 | $self->_access_token_generate; | |||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||
| 354 | 0 | ▶ | if (! $self->_access_token_valid) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 355 | 0 | $self->_access_token_refresh; | |||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||
| 358 | 0 | $self->{access_token} = $self->_access_token_data->{access_token}; | |||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||
| 360 | 0 | return $self->{access_token}; | |||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||
| 362 | sub _access_token_data { | ||||||||||||||||||||||||||||||||||||||
| 363 | # Fetches and stores the access token data to the AUTH_CACHE_FILE | ||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||
| 365 | 9 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_access_token_data: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||
| 367 | 9 | ▶ | $self->{cache_data} = $data if defined $data; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||
| 369 | 9 | ▶ | return $self->{cache_data} if $self->{cache_data}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||
| 371 | { | ||||||||||||||||||||||||||||||||||||||
| 372 | 2 | local $/; | |||||||||||||||||||||||||||||||||||||
| 373 | 2 | ▶ | open my $fh, '<', $self->_authentication_cache_file or die | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 374 | "Can't open Tesla cache file " . | ||||||||||||||||||||||||||||||||||||||
| 375 | $self->_authentication_cache_file . | ||||||||||||||||||||||||||||||||||||||
| 376 | ": $!"; | ||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||
| 378 | 1 | my $json = <$fh>; | |||||||||||||||||||||||||||||||||||||
| 379 | 1 | $self->{cache_data} = decode_json($json); | |||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||
| 382 | 1 | return $self->{cache_data}; | |||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||
| 384 | sub _access_token_generate { | ||||||||||||||||||||||||||||||||||||||
| 385 | # Generates an access token and stores it in the cache file | ||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||
| 387 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_access_token_generate: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||
| 389 | 1 | my $auth_code = $self->_authentication_code; | |||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||
| 391 | 1 | my $url = URI->new(URL_TOKEN); | |||||||||||||||||||||||||||||||||||||
| 392 | 1 | my $header = ['Content-Type' => 'application/json; charset=UTF-8']; | |||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||
| 394 | 1 | my $request_data = { | |||||||||||||||||||||||||||||||||||||
| 395 | grant_type => "authorization_code", | ||||||||||||||||||||||||||||||||||||||
| 396 | client_id => "ownerapi", | ||||||||||||||||||||||||||||||||||||||
| 397 | code => $auth_code, | ||||||||||||||||||||||||||||||||||||||
| 398 | code_verifier => $self->_authentication_code_verifier, | ||||||||||||||||||||||||||||||||||||||
| 399 | redirect_uri => "https://auth.tesla.com/void/callback", | ||||||||||||||||||||||||||||||||||||||
| 400 | }; | ||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||
| 402 | 1 | my $request = HTTP::Request->new( | |||||||||||||||||||||||||||||||||||||
| 403 | 'POST', | ||||||||||||||||||||||||||||||||||||||
| 404 | $url, | ||||||||||||||||||||||||||||||||||||||
| 405 | $header, | ||||||||||||||||||||||||||||||||||||||
| 406 | JSON->new->allow_nonref->encode($request_data) | ||||||||||||||||||||||||||||||||||||||
| 407 | ); | ||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||
| 409 | 1 | my ($success, $code, $response_data)= $self->_tesla_api_call($request); | |||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||
| 411 | 1 | my $token_data = decode_json($response_data); | |||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||
| 413 | 1 | $token_data = $self->_access_token_set_expiry($token_data); | |||||||||||||||||||||||||||||||||||||
| 414 | 1 | $self->_access_token_update($token_data); | |||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||
| 416 | 1 | return $token_data; | |||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||
| 418 | sub _access_token_valid { | ||||||||||||||||||||||||||||||||||||||
| 419 | # Checks the validity of an existing token | ||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||
| 421 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_access_token_valid: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||
| 423 | 2 | my $token_expires_at = $self->_access_token_data->{expires_at}; | |||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||
| 425 | 2 | my $valid = 0; | |||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||
| 427 | 2 | ▶ | if (time + TOKEN_EXPIRY_WINDOW < $token_expires_at) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 428 | 1 | $valid = 1; | |||||||||||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||
| 431 | 2 | return $valid; | |||||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||||
| 433 | sub _access_token_set_expiry { | ||||||||||||||||||||||||||||||||||||||
| 434 | # Sets the access token expiry date/time after generation and | ||||||||||||||||||||||||||||||||||||||
| 435 | # renewal | ||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||
| 437 | 5 | ▶ | my ($self, $token_data) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_access_token_set_expiry: called CC=3 SCAR=12.3
| |||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||
| 439 | 5 | ▶ | if (! defined $token_data || ref($token_data) ne 'HASH') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $token_data or ref $token_data ne "HASH"
MC/DC0%0/2
not defined $token_data or ref $token_data ne "HASH"
not defined $token_data
ref $token_data ne "HASH"
Truth table33%1/3
not(A) or B
A= not defined $token_dataB= ref $token_data ne "HASH"
| |||||||||||||||||||||||||||||||||||||||
| 440 | 0 | croak "_access_token_set_expiry() needs a hash reference of token data"; | |||||||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||
| 443 | 5 | my $expiry = time + $token_data->{expires_in}; | |||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||
| 445 | 5 | $token_data->{expires_at} = $expiry; | |||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||
| 447 | 5 | return $token_data; | |||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||
| 449 | sub _access_token_refresh { | ||||||||||||||||||||||||||||||||||||||
| 450 | # Renews an expired/invalid access token | ||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||
| 452 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_access_token_refresh: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||
| 454 | 0 | my $url = URI->new(URL_TOKEN); | |||||||||||||||||||||||||||||||||||||
| 455 | 0 | my $header = ['Content-Type' => 'application/json; charset=UTF-8']; | |||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||
| 457 | 0 | my $refresh_token = $self->_access_token_data->{refresh_token}; | |||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||
| 459 | 0 | my $request_data = { | |||||||||||||||||||||||||||||||||||||
| 460 | grant_type => 'refresh_token', | ||||||||||||||||||||||||||||||||||||||
| 461 | refresh_token => $refresh_token, | ||||||||||||||||||||||||||||||||||||||
| 462 | client_id => 'ownerapi', | ||||||||||||||||||||||||||||||||||||||
| 463 | }; | ||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||
| 465 | 0 | my $request = HTTP::Request->new( | |||||||||||||||||||||||||||||||||||||
| 466 | 'POST', | ||||||||||||||||||||||||||||||||||||||
| 467 | $url, | ||||||||||||||||||||||||||||||||||||||
| 468 | $header, | ||||||||||||||||||||||||||||||||||||||
| 469 | JSON->new->allow_nonref->encode($request_data) | ||||||||||||||||||||||||||||||||||||||
| 470 | ); | ||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||
| 472 | 0 | my ($success, $code, $response_data) = $self->_tesla_api_call($request); | |||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||
| 474 | # Extract the token data | ||||||||||||||||||||||||||||||||||||||
| 475 | 0 | my $token_data = decode_json($response_data); | |||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||
| 477 | # Re-add the existing refresh token; its still valid | ||||||||||||||||||||||||||||||||||||||
| 478 | 0 | $token_data->{refresh_token} = $refresh_token; | |||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||
| 480 | # Set the expiry time | ||||||||||||||||||||||||||||||||||||||
| 481 | 0 | $token_data = $self->_access_token_set_expiry($token_data); | |||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||
| 483 | # Update the cached token | ||||||||||||||||||||||||||||||||||||||
| 484 | 0 | $self->_access_token_update($token_data); | |||||||||||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||||||||||||
| 486 | sub _access_token_update { | ||||||||||||||||||||||||||||||||||||||
| 487 | # Writes the new or updated token to the cache file | ||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||
| 489 | 2 | ▶ | my ($self, $token_data) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_access_token_update: called CC=4 SCAR=15.9
| |||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||
| 491 | 2 | ▶ | if (! defined $token_data || ref($token_data) ne 'HASH') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $token_data or ref $token_data ne "HASH"
MC/DC0%0/2
not defined $token_data or ref $token_data ne "HASH"
not defined $token_data
ref $token_data ne "HASH"
Truth table33%1/3
not(A) or B
A= not defined $token_dataB= ref $token_data ne "HASH"
| |||||||||||||||||||||||||||||||||||||||
| 492 | 0 | croak "_access_token_update() needs a hash reference of token data"; | |||||||||||||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||
| 495 | 2 | $self->_access_token_data($token_data); | |||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||
| 497 | 2 | ▶ | open my $fh, '>', $self->_authentication_cache_file or die $!; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||
| 499 | 2 | print $fh JSON->new->allow_nonref->encode($token_data); | |||||||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||||||||
| 501 | sub _api_attempts { | ||||||||||||||||||||||||||||||||||||||
| 502 | # Stores and returns the number of attempts of each API call to Tesla | ||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||
| 504 | 16 | ▶ | my ($self, $add) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_api_attempts: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||
| 506 | 16 | ▶ | if (defined $add) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 507 | 12 | ▶ | if ($add) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 508 | 9 | $self->{api_attempts}++; | |||||||||||||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||||||||||||
| 510 | else { | ||||||||||||||||||||||||||||||||||||||
| 511 | 3 | $self->{api_attempts} = 0; | |||||||||||||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||
| 515 | 16 | ▶ | return $self->{api_attempts} || 0; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'api_attempts'} || 0
MC/DC100%1/1
$self->{'api_attempts'} || 0
$self->{'api_attempts'}
Truth table100%2/2
A
A= $self->{'api_attempts'}
| |||||||||||||||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||||||||||||
| 517 | sub _api_cache { | ||||||||||||||||||||||||||||||||||||||
| 518 | # Stores the Tesla API fetched data | ||||||||||||||||||||||||||||||||||||||
| 519 | 14 | ▶ | my ($self, %params) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_api_cache: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||
| 521 | 14 | my $endpoint = $params{endpoint}; | |||||||||||||||||||||||||||||||||||||
| 522 | 14 | ▶ | my $id = $params{id} // 0; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$params{"id"} // 0
MC/DC100%1/1
$params{"id"} // 0
$params{"id"}
Truth table100%2/2
A
A= $params{"id"}
| |||||||||||||||||||||||||||||||||||||||
| 523 | 14 | my $data = $params{data}; | |||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||
| 525 | 14 | ▶ | if (! $endpoint) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 526 | 1 | croak "_api_cache() requires an endpoint name sent in"; | |||||||||||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||
| 529 | 13 | ▶ | if ($data) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 530 | 6 | $api_cache{$endpoint}{$id}{data} = $data; | |||||||||||||||||||||||||||||||||||||
| 531 | 6 | $api_cache{$endpoint}{$id}{time} = time; | |||||||||||||||||||||||||||||||||||||
| 532 | } | ||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||
| 534 | 13 | return $api_cache{$endpoint}{$id}; | |||||||||||||||||||||||||||||||||||||
| 535 | } | ||||||||||||||||||||||||||||||||||||||
| 536 | sub _api_cache_data { | ||||||||||||||||||||||||||||||||||||||
| 537 | # Returns the entire API cache (for testing) | ||||||||||||||||||||||||||||||||||||||
| 538 | 6 | ▶ | return %api_cache; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_api_cache_data: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 539 | } | ||||||||||||||||||||||||||||||||||||||
| 540 | sub _authentication_cache_file { | ||||||||||||||||||||||||||||||||||||||
| 541 | 11 | ▶ | my ($self, $filename) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_authentication_cache_file: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||
| 543 | 11 | ▶ | if (defined $filename) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 544 | 4 | $self->{authentication_cache_file} = $filename; | |||||||||||||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||
| 547 | 11 | ▶ | return $self->{authentication_cache_file} || AUTH_CACHE_FILE; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'authentication_cache_file'} || '/home/cpancover/tesla_auth_cache.json'
MC/DC100%1/1
$self->{'authentication_cache_file'} || '/home/cpancover/tesla_auth_cache.json'
$self->{'authentication_cache_file'}
Truth table100%2/2
A
A= $self->{'authentication_cache_file'}
| |||||||||||||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||||||||||||
| 549 | sub _authentication_code { | ||||||||||||||||||||||||||||||||||||||
| 550 | # If an access token is unavailable, prompt the user with a URL to | ||||||||||||||||||||||||||||||||||||||
| 551 | # authenticate to Tesla, and have them paste in the resulting URL | ||||||||||||||||||||||||||||||||||||||
| 552 | # We then extract and return the access code to generate the access | ||||||||||||||||||||||||||||||||||||||
| 553 | # token | ||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||
| 555 | 3 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_authentication_code: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||
| 557 | 3 | ▶ | return $self->{authentication_code} if $self->{authentication_code}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||
| 559 | 1 | my $auth_url = $self->_authentication_code_url; | |||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||
| 561 | # If we're in testing mode, we don't want to be waiting for | ||||||||||||||||||||||||||||||||||||||
| 562 | # a read from STDIN | ||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||
| 564 | 1 | my $code_url; | |||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||
| 566 | 1 | ▶ | if ($ENV{TESLA_API_TESTING}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 567 | 1 | $code_url = $ENV{TESLA_API_TESTING_CODE_URL}; | |||||||||||||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||||||||||||
| 569 | else { | ||||||||||||||||||||||||||||||||||||||
| 570 | 0 | print "\n$auth_url\n"; | |||||||||||||||||||||||||||||||||||||
| 571 | 0 | print "\nPaste URL here: "; | |||||||||||||||||||||||||||||||||||||
| 572 | 0 | $code_url = <STDIN>; | |||||||||||||||||||||||||||||||||||||
| 573 | } | ||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||
| 575 | 1 | chomp $code_url; | |||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||
| 577 | 1 | my $code = $self->_authentication_code_extract($code_url); | |||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||
| 579 | 1 | $self->{authentication_code} = $code; | |||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||
| 581 | 1 | return $code; | |||||||||||||||||||||||||||||||||||||
| 582 | } | ||||||||||||||||||||||||||||||||||||||
| 583 | sub _authentication_code_extract { | ||||||||||||||||||||||||||||||||||||||
| 584 | # Pull in the pasted URL with the code, extract the code, | ||||||||||||||||||||||||||||||||||||||
| 585 | # and return it | ||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||
| 587 | 3 | ▶ | my ($self, $code_url) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_authentication_code_extract: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||
| 589 | 3 | my $code; | |||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||
| 591 | 3 | ▶ | if ($code_url =~ /code=(.*?)\&/) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 592 | 2 | $code = $1; | |||||||||||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||||||||||||
| 594 | else { | ||||||||||||||||||||||||||||||||||||||
| 595 | 1 | croak "Could not extract the authorization code from the URL"; | |||||||||||||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||
| 598 | 2 | return $code; | |||||||||||||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||||||||||||
| 600 | sub _authentication_code_url { | ||||||||||||||||||||||||||||||||||||||
| 601 | # Generate Tesla's authentication URL | ||||||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||||||
| 603 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_authentication_code_url: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 604 | 2 | my $auth_url = URI->new(URL_AUTH); | |||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||
| 606 | my %params = ( | ||||||||||||||||||||||||||||||||||||||
| 607 | client_id => 'ownerapi', | ||||||||||||||||||||||||||||||||||||||
| 608 | code_challenge => $self->_authentication_code_verifier, | ||||||||||||||||||||||||||||||||||||||
| 609 | code_challenge_method => 'S256', | ||||||||||||||||||||||||||||||||||||||
| 610 | redirect_uri => 'https://auth.tesla.com/void/callback', | ||||||||||||||||||||||||||||||||||||||
| 611 | response_type => 'code', | ||||||||||||||||||||||||||||||||||||||
| 612 | scope => 'openid email offline_access', | ||||||||||||||||||||||||||||||||||||||
| 613 | state => '123', | ||||||||||||||||||||||||||||||||||||||
| 614 | login_hint => $ENV{TESLA_EMAIL}, | ||||||||||||||||||||||||||||||||||||||
| 615 | 2 | ); | |||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||
| 617 | 2 | $auth_url->query_form(%params); | |||||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||||||
| 619 | 2 | ||||||||||||||||||||||||||||||||||||||
| 620 | "\nPlease follow the URL displayed below in your browser and log into Tesla, " . | ||||||||||||||||||||||||||||||||||||||
| 621 | "then paste the URL from the resulting 'Page Not Found' page's address bar, " . | ||||||||||||||||||||||||||||||||||||||
| 622 | "then hit ENTER:\n"; | ||||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||||
| 624 | 2 | return $auth_url; | |||||||||||||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||||||||||||
| 626 | sub _authentication_code_verifier { | ||||||||||||||||||||||||||||||||||||||
| 627 | # When generating an access token, generate and store a code | ||||||||||||||||||||||||||||||||||||||
| 628 | # validation key | ||||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||||
| 630 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_authentication_code_verifier: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||||
| 632 | 6 | ▶ | if (defined $self->{authentication_code_verifier}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 633 | return $self->{authentication_code_verifier} | ||||||||||||||||||||||||||||||||||||||
| 634 | 2 | } | |||||||||||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||||||||||||
| 636 | 4 | my $code_verifier = _random_string(); | |||||||||||||||||||||||||||||||||||||
| 637 | 4 | $code_verifier = sha256_hex($code_verifier); | |||||||||||||||||||||||||||||||||||||
| 638 | 4 | $code_verifier = encode_base64url($code_verifier); | |||||||||||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||||||||||||
| 640 | 4 | return $self->{authentication_code_verifier} = $code_verifier; | |||||||||||||||||||||||||||||||||||||
| 641 | } | ||||||||||||||||||||||||||||||||||||||
| 642 | sub _decode { | ||||||||||||||||||||||||||||||||||||||
| 643 | # Decode JSON to Perl | ||||||||||||||||||||||||||||||||||||||
| 644 | 4 | ▶ | my ($self, $json) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 645 | 4 | my $perl = decode_json($json); | |||||||||||||||||||||||||||||||||||||
| 646 | 4 | return $perl; | |||||||||||||||||||||||||||||||||||||
| 647 | } | ||||||||||||||||||||||||||||||||||||||
| 648 | sub _random_string { | ||||||||||||||||||||||||||||||||||||||
| 649 | # Returns a proper length alpha-num string for Tesla API token code | ||||||||||||||||||||||||||||||||||||||
| 650 | # verification key | ||||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||
| 652 | 4 | ▶ | my @chars = ('A' .. 'Z', 'a' .. 'z', 0 .. 9); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_random_string: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 653 | 4 | my $rand_string; | |||||||||||||||||||||||||||||||||||||
| 654 | 4 | $rand_string .= $chars[rand @chars] for 1 .. 85; | |||||||||||||||||||||||||||||||||||||
| 655 | 4 | return $rand_string; | |||||||||||||||||||||||||||||||||||||
| 656 | } | ||||||||||||||||||||||||||||||||||||||
| 657 | sub _tesla_api_call { | ||||||||||||||||||||||||||||||||||||||
| 658 | # Responsible for all calls to the Tesla API | ||||||||||||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||||||||||||
| 660 | 2 | ▶ | my ($self, $request) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_tesla_api_call: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||||||
| 662 | 2 | $self->_api_attempts(0); | |||||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||||||
| 664 | 2 | my $response; | |||||||||||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||||||||||||
| 666 | 2 | for (1 .. API_TIMEOUT_RETRIES) { | |||||||||||||||||||||||||||||||||||||
| 667 | 4 | $response = $self->mech->request($request); | |||||||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||||||||
| 669 | 4 | $self->_api_attempts(1); | |||||||||||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||||||||||||
| 671 | 4 | ▶ | if ($response->is_success) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 672 | 1 | last; | |||||||||||||||||||||||||||||||||||||
| 673 | } | ||||||||||||||||||||||||||||||||||||||
| 674 | } | ||||||||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||||||||
| 676 | 2 | my $success = $response->is_success; | |||||||||||||||||||||||||||||||||||||
| 677 | 2 | my $code = $response->code; | |||||||||||||||||||||||||||||||||||||
| 678 | 2 | my $decoded_content = $response->decoded_content; | |||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||
| 680 | 2 | ▶ | if (! $response->is_success) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 681 | 1 | my $error_msg = $response->status_line; | |||||||||||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||||||||||||
| 683 | 1 | my $error_string = "Error - Tesla API said: '$error_msg'"; | |||||||||||||||||||||||||||||||||||||
| 684 | 1 | warn $error_string; | |||||||||||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||||||||||||
| 686 | 1 | $decoded_content = "{\"error\" : \"$error_msg\"}"; | |||||||||||||||||||||||||||||||||||||
| 687 | } | ||||||||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||||||||
| 689 | 2 | return ($success, $code, $decoded_content); | |||||||||||||||||||||||||||||||||||||
| 690 | } | ||||||||||||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||||||||||||
| 692 | 28 | 1; | |||||||||||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||||||||||||
| 694 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||
| 696 | Tesla::API - Interface to Tesla's API | ||||||||||||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||||||||||||
| 698 | =for html | ||||||||||||||||||||||||||||||||||||||
| 699 | <a href="https://github.com/stevieb9/tesla-api/actions"><img src="https://github.com/stevieb9/tesla-api/workflows/CI/badge.svg"/></a> | ||||||||||||||||||||||||||||||||||||||
| 700 | <a href='https://coveralls.io/github/stevieb9/tesla-api?branch=master'><img src='https://coveralls.io/repos/stevieb9/tesla-api/badge.svg?branch=master&service=github' alt='Coverage Status' /></a> | ||||||||||||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||||||||||||
| 702 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||||||||||||
| 704 | use Tesla::API; | ||||||||||||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||||||||||||
| 706 | my $tesla = Tesla::API->new; | ||||||||||||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||||||||||||
| 708 | my @endpoint_names = keys %{ $tesla->endpoints }; | ||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||
| 710 | # See Tesla::Vehicle for direct access to vehicle-related methods | ||||||||||||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||||||||||||
| 712 | my $endpoint_name = 'VEHICLE_DATA'; | ||||||||||||||||||||||||||||||||||||||
| 713 | my $vehicle_id = 3234234242124; | ||||||||||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||||||||
| 715 | # Get the entire list of car data | ||||||||||||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||||||||||||
| 717 | my $car_data = $tesla->api( | ||||||||||||||||||||||||||||||||||||||
| 718 | endpoint => $endpoint_name, | ||||||||||||||||||||||||||||||||||||||
| 719 | id => $vehicle_id | ||||||||||||||||||||||||||||||||||||||
| 720 | ); | ||||||||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||||||||
| 722 | # Send the open trunk command | ||||||||||||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||||||||||||
| 724 | $tesla->api( | ||||||||||||||||||||||||||||||||||||||
| 725 | endpoint => 'ACTUATE_TRUNK', | ||||||||||||||||||||||||||||||||||||||
| 726 | id => $vehicle_id, | ||||||||||||||||||||||||||||||||||||||
| 727 | api_params => {which_trunk => 'rear'} | ||||||||||||||||||||||||||||||||||||||
| 728 | ); | ||||||||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||||||||
| 730 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||||||||
| 732 | This distribution provides access to the Tesla API. | ||||||||||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||||||||||
| 734 | This class is designed to be subclassed. For example, I have already begun a | ||||||||||||||||||||||||||||||||||||||
| 735 | new L<Tesla::Vehicle> distribution which will have access and update methods | ||||||||||||||||||||||||||||||||||||||
| 736 | that deal specifically with Tesla autos, then a C<Tesla::Powerwall> | ||||||||||||||||||||||||||||||||||||||
| 737 | distribution for their battery storage etc. | ||||||||||||||||||||||||||||||||||||||
| 738 | |||||||||||||||||||||||||||||||||||||||
| 739 | =head1 METHODS - CORE | ||||||||||||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||||||||||||
| 741 | =head2 new(%params) | ||||||||||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||||||||
| 743 | Instantiates and returns a new L<Tesla::API> object. | ||||||||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||||||||
| 745 | B<NOTE>: When instantiating an object and you haven't previously authenticated, | ||||||||||||||||||||||||||||||||||||||
| 746 | a URL will be displayed on the console for you to navigate to. You will then | ||||||||||||||||||||||||||||||||||||||
| 747 | be redirected to Tesla's login page where you will authenticate. You will | ||||||||||||||||||||||||||||||||||||||
| 748 | be redirected again to a "Page Not Found" page, in which you must copy the URL | ||||||||||||||||||||||||||||||||||||||
| 749 | from the address bar and paste it back into the console. | ||||||||||||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||||||||||||
| 751 | We then internally generate an access token for you, store it in a | ||||||||||||||||||||||||||||||||||||||
| 752 | C<tesla_auth_cache.json> file in your home directory, and use it on all | ||||||||||||||||||||||||||||||||||||||
| 753 | subsequent accesses. | ||||||||||||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||||||||||||
| 755 | B<NOTE>: If you do not have a Tesla account, you can still instantiate a | ||||||||||||||||||||||||||||||||||||||
| 756 | L<Tesla::API> object by supplying the C<< unauthenticated => 1 >> parameter | ||||||||||||||||||||||||||||||||||||||
| 757 | to C<new()>. | ||||||||||||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||||||||||||
| 759 | B<Parameters>: | ||||||||||||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||||||||||||
| 761 | All parameters are to be sent in the form of a hash. | ||||||||||||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||||||||||||
| 763 | unauthenticated | ||||||||||||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||||||||||||
| 765 | I<Optional, Bool>: Set to true to bypass the access token generation. | ||||||||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||||||||
| 767 | I<Default>: C<undef> | ||||||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||||||
| 769 | api_cache_persist | ||||||||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||||||||
| 771 | I<Optional, Bool>: Set this to true if you want to make multiple calls against | ||||||||||||||||||||||||||||||||||||||
| 772 | the same data set, where having the cache time out and re-populated between | ||||||||||||||||||||||||||||||||||||||
| 773 | these calls would be non-beneficial. | ||||||||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||||||||
| 775 | I<Default>: False | ||||||||||||||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||||||||||||
| 777 | api_cache_time | ||||||||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||||||||
| 779 | I<Optional, Integer>: By default, we cache the fetched data from the Tesla API | ||||||||||||||||||||||||||||||||||||||
| 780 | for two seconds. If you make calls that have already been called within that | ||||||||||||||||||||||||||||||||||||||
| 781 | time, we will return the cached data. | ||||||||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||||||||
| 783 | Send in the number of seconds you'd like to cache the data for. A value of zero | ||||||||||||||||||||||||||||||||||||||
| 784 | (C<0>) will disable caching and all calls through this library will go directly | ||||||||||||||||||||||||||||||||||||||
| 785 | to Tesla every time. | ||||||||||||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||||||||||||
| 787 | I<Return>: Integer, the number of seconds we're caching Tesla API data for. | ||||||||||||||||||||||||||||||||||||||
| 788 | |||||||||||||||||||||||||||||||||||||||
| 789 | =head2 api(%params) | ||||||||||||||||||||||||||||||||||||||
| 790 | |||||||||||||||||||||||||||||||||||||||
| 791 | Responsible for disseminating the endpoints and retrieving data through the | ||||||||||||||||||||||||||||||||||||||
| 792 | Tesla API. | ||||||||||||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||||||||||||
| 794 | All parameters are to be sent in as a hash. | ||||||||||||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||||||||||||
| 796 | B<Parameters>: | ||||||||||||||||||||||||||||||||||||||
| 797 | |||||||||||||||||||||||||||||||||||||||
| 798 | endpoint | ||||||||||||||||||||||||||||||||||||||
| 799 | |||||||||||||||||||||||||||||||||||||||
| 800 | I<Mandatory, String>: A valid Tesla API endpoint name. The entire list can be | ||||||||||||||||||||||||||||||||||||||
| 801 | found in the C<share/endpoints.json> file for the time being. | ||||||||||||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||||||||||||
| 803 | id | ||||||||||||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||||||||||||
| 805 | I<Optional, Integer>: Some endpoints require an ID sent in (eg. vehicle ID, | ||||||||||||||||||||||||||||||||||||||
| 806 | Powerwall ID etc). | ||||||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||||||
| 808 | api_params | ||||||||||||||||||||||||||||||||||||||
| 809 | |||||||||||||||||||||||||||||||||||||||
| 810 | I<Optional, Hash Reference>: Some API calls require additional parameters. Send | ||||||||||||||||||||||||||||||||||||||
| 811 | in a hash reference where the keys are the API parameter name, and the value is, | ||||||||||||||||||||||||||||||||||||||
| 812 | well, the value. | ||||||||||||||||||||||||||||||||||||||
| 813 | |||||||||||||||||||||||||||||||||||||||
| 814 | I<Return>: Hash or array reference, depending on the endpoint. If an error | ||||||||||||||||||||||||||||||||||||||
| 815 | occurs while communicating with the Tesla API, we'll send back a hash reference | ||||||||||||||||||||||||||||||||||||||
| 816 | containing C<< error => 'Tesla error message' >>. | ||||||||||||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||||||||||||
| 818 | =head2 endpoints | ||||||||||||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||||||||||||
| 820 | Returns a hash reference of hash references. Each key is the name of the | ||||||||||||||||||||||||||||||||||||||
| 821 | endpoint, and its value contains data on how we process the call to Tesla. | ||||||||||||||||||||||||||||||||||||||
| 822 | |||||||||||||||||||||||||||||||||||||||
| 823 | Example (snipped for brevity): | ||||||||||||||||||||||||||||||||||||||
| 824 | |||||||||||||||||||||||||||||||||||||||
| 825 | { | ||||||||||||||||||||||||||||||||||||||
| 826 | MEDIA_VOLUME_DOWN => { | ||||||||||||||||||||||||||||||||||||||
| 827 | TYPE => 'POST', | ||||||||||||||||||||||||||||||||||||||
| 828 | URI => 'api/1/vehicles/{vehicle_id}/command/media_volume_down' | ||||||||||||||||||||||||||||||||||||||
| 829 | AUTH => $VAR1->{'UPGRADES_CREATE_OFFLINE_ORDER'}{'AUTH'}, | ||||||||||||||||||||||||||||||||||||||
| 830 | }, | ||||||||||||||||||||||||||||||||||||||
| 831 | VEHICLE_DATA => { | ||||||||||||||||||||||||||||||||||||||
| 832 | TYPE => 'GET', | ||||||||||||||||||||||||||||||||||||||
| 833 | URI => 'api/1/vehicles/{vehicle_id}/vehicle_data', | ||||||||||||||||||||||||||||||||||||||
| 834 | AUTH => $VAR1->{'UPGRADES_CREATE_OFFLINE_ORDER'}{'AUTH'} | ||||||||||||||||||||||||||||||||||||||
| 835 | }, | ||||||||||||||||||||||||||||||||||||||
| 836 | } | ||||||||||||||||||||||||||||||||||||||
| 837 | |||||||||||||||||||||||||||||||||||||||
| 838 | Bracketed names in the URI (eg: C<{vehicle_id}>) are variable placeholders. | ||||||||||||||||||||||||||||||||||||||
| 839 | It will be replaced with the ID sent in to the various method or C<api()> | ||||||||||||||||||||||||||||||||||||||
| 840 | call. | ||||||||||||||||||||||||||||||||||||||
| 841 | |||||||||||||||||||||||||||||||||||||||
| 842 | To get a list of endpoint names: | ||||||||||||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||||||||||||
| 844 | my @endpoint_names = keys %{ $tesla->endpoints }; | ||||||||||||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||||||||||||
| 846 | =head2 mech | ||||||||||||||||||||||||||||||||||||||
| 847 | |||||||||||||||||||||||||||||||||||||||
| 848 | Returns the L<WWW::Mechanize> object we've instantiated internally. | ||||||||||||||||||||||||||||||||||||||
| 849 | |||||||||||||||||||||||||||||||||||||||
| 850 | =head2 option_codes | ||||||||||||||||||||||||||||||||||||||
| 851 | |||||||||||||||||||||||||||||||||||||||
| 852 | B<NOTE>: I'm unsure if the option codes are vehicle specific, or general for | ||||||||||||||||||||||||||||||||||||||
| 853 | all Tesla products, so I'm leaving this method here for now. | ||||||||||||||||||||||||||||||||||||||
| 854 | |||||||||||||||||||||||||||||||||||||||
| 855 | Returns a hash reference of 'option code' => 'description' pairs. | ||||||||||||||||||||||||||||||||||||||
| 856 | |||||||||||||||||||||||||||||||||||||||
| 857 | =head2 update_data_files($type) | ||||||||||||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||||||||||||
| 859 | Checks to see if there are any updates to the C<endpoints.json> or | ||||||||||||||||||||||||||||||||||||||
| 860 | C<option_codes.json> files online, and updates them locally. | ||||||||||||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||||||||||||
| 862 | Parameters: | ||||||||||||||||||||||||||||||||||||||
| 863 | |||||||||||||||||||||||||||||||||||||||
| 864 | $type | ||||||||||||||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||||||||||||
| 866 | I<Optional, String>: One of B<endpoints> or B<option_codes>. If set, we'll | ||||||||||||||||||||||||||||||||||||||
| 867 | operate on only that file. | ||||||||||||||||||||||||||||||||||||||
| 868 | |||||||||||||||||||||||||||||||||||||||
| 869 | I<Return>: None. C<croak()>s on faiure. | ||||||||||||||||||||||||||||||||||||||
| 870 | |||||||||||||||||||||||||||||||||||||||
| 871 | =head2 useragent_string($ua_string) | ||||||||||||||||||||||||||||||||||||||
| 872 | |||||||||||||||||||||||||||||||||||||||
| 873 | Sets/gets the useragent string we send to the Tesla API. | ||||||||||||||||||||||||||||||||||||||
| 874 | |||||||||||||||||||||||||||||||||||||||
| 875 | I<Optional, String>: The user agent browser string to send to the Tesla API. | ||||||||||||||||||||||||||||||||||||||
| 876 | |||||||||||||||||||||||||||||||||||||||
| 877 | I<Return>: String, the currently set value. | ||||||||||||||||||||||||||||||||||||||
| 878 | |||||||||||||||||||||||||||||||||||||||
| 879 | =head2 useragent_timeout($timeout) | ||||||||||||||||||||||||||||||||||||||
| 880 | |||||||||||||||||||||||||||||||||||||||
| 881 | Sets/gets the timeout we use in the L<WWW::Mechanize|/mech> object that we | ||||||||||||||||||||||||||||||||||||||
| 882 | communicate to the Tesla API with. | ||||||||||||||||||||||||||||||||||||||
| 883 | |||||||||||||||||||||||||||||||||||||||
| 884 | Parameters: | ||||||||||||||||||||||||||||||||||||||
| 885 | |||||||||||||||||||||||||||||||||||||||
| 886 | $timeout | ||||||||||||||||||||||||||||||||||||||
| 887 | |||||||||||||||||||||||||||||||||||||||
| 888 | I<Optional, Integer/Float>: The timeout in seconds or fractions of a second. | ||||||||||||||||||||||||||||||||||||||
| 889 | |||||||||||||||||||||||||||||||||||||||
| 890 | I<Return>: Integer/Float, the currently set value. | ||||||||||||||||||||||||||||||||||||||
| 891 | |||||||||||||||||||||||||||||||||||||||
| 892 | =head1 METHODS - API CACHE | ||||||||||||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||||||||||||
| 894 | =head2 api_cache_clear | ||||||||||||||||||||||||||||||||||||||
| 895 | |||||||||||||||||||||||||||||||||||||||
| 896 | Some methods chain method calls. For example, calling | ||||||||||||||||||||||||||||||||||||||
| 897 | C<< $vehicle->doors_lock >> will poll the API, then cache the state data. | ||||||||||||||||||||||||||||||||||||||
| 898 | |||||||||||||||||||||||||||||||||||||||
| 899 | if another call is made to C<< $vehicle->locked >> immediately thereafter to | ||||||||||||||||||||||||||||||||||||||
| 900 | check whether the door is actually closed or not, the old cached data would | ||||||||||||||||||||||||||||||||||||||
| 901 | normally be returned. | ||||||||||||||||||||||||||||||||||||||
| 902 | |||||||||||||||||||||||||||||||||||||||
| 903 | If we don't clear the cache out between these two calls, we will be returned | ||||||||||||||||||||||||||||||||||||||
| 904 | stale data. | ||||||||||||||||||||||||||||||||||||||
| 905 | |||||||||||||||||||||||||||||||||||||||
| 906 | Takes no parameters, has no return. Only use this call in API calls that | ||||||||||||||||||||||||||||||||||||||
| 907 | somehow manipulate the state of the object you're working with. | ||||||||||||||||||||||||||||||||||||||
| 908 | |||||||||||||||||||||||||||||||||||||||
| 909 | =head2 api_cache_persist($bool) | ||||||||||||||||||||||||||||||||||||||
| 910 | |||||||||||||||||||||||||||||||||||||||
| 911 | $bool | ||||||||||||||||||||||||||||||||||||||
| 912 | |||||||||||||||||||||||||||||||||||||||
| 913 | I<Optional, Bool>: Set this to true if you want to make multiple calls against | ||||||||||||||||||||||||||||||||||||||
| 914 | the same data set, where having the cache time out and re-populated between | ||||||||||||||||||||||||||||||||||||||
| 915 | these calls would be non-beneficial. | ||||||||||||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||||||||||||
| 917 | You can ensure fresh data for the set by making a call to C<api_cache_clear()> | ||||||||||||||||||||||||||||||||||||||
| 918 | before the first call that fetches data. | ||||||||||||||||||||||||||||||||||||||
| 919 | |||||||||||||||||||||||||||||||||||||||
| 920 | I<Default>: False | ||||||||||||||||||||||||||||||||||||||
| 921 | |||||||||||||||||||||||||||||||||||||||
| 922 | =head2 api_cache_time($cache_seconds) | ||||||||||||||||||||||||||||||||||||||
| 923 | |||||||||||||||||||||||||||||||||||||||
| 924 | The number of seconds we will cache retrieved endpoint data from the Tesla API | ||||||||||||||||||||||||||||||||||||||
| 925 | for, to reduce the number of successive calls to retrieve the same data. | ||||||||||||||||||||||||||||||||||||||
| 926 | |||||||||||||||||||||||||||||||||||||||
| 927 | B<Parameters>: | ||||||||||||||||||||||||||||||||||||||
| 928 | |||||||||||||||||||||||||||||||||||||||
| 929 | $cache_seconds | ||||||||||||||||||||||||||||||||||||||
| 930 | |||||||||||||||||||||||||||||||||||||||
| 931 | I<Optional, Integer>: By default, we cache the fetched data from the Tesla API | ||||||||||||||||||||||||||||||||||||||
| 932 | for two seconds. If you make calls that have already been called within that | ||||||||||||||||||||||||||||||||||||||
| 933 | time, we will return the cached data. | ||||||||||||||||||||||||||||||||||||||
| 934 | |||||||||||||||||||||||||||||||||||||||
| 935 | Send in the number of seconds you'd like to cache the data for. A value of zero | ||||||||||||||||||||||||||||||||||||||
| 936 | (C<0>) will disable caching and all calls through this library will go directly | ||||||||||||||||||||||||||||||||||||||
| 937 | to Tesla every time. | ||||||||||||||||||||||||||||||||||||||
| 938 | |||||||||||||||||||||||||||||||||||||||
| 939 | I<Return>: Integer, the number of seconds we're caching Tesla API data for. | ||||||||||||||||||||||||||||||||||||||
| 940 | |||||||||||||||||||||||||||||||||||||||
| 941 | =head1 API CACHING | ||||||||||||||||||||||||||||||||||||||
| 942 | |||||||||||||||||||||||||||||||||||||||
| 943 | We've employed a complex caching mechanism for data received from Tesla's API. | ||||||||||||||||||||||||||||||||||||||
| 944 | |||||||||||||||||||||||||||||||||||||||
| 945 | By default, we cache retrieved data for every endpoint/ID pair in the cache for | ||||||||||||||||||||||||||||||||||||||
| 946 | two seconds (modifiable by C<api_cache_timeout()>, or C<api_cache_timeout> in | ||||||||||||||||||||||||||||||||||||||
| 947 | C<new()>). | ||||||||||||||||||||||||||||||||||||||
| 948 | |||||||||||||||||||||||||||||||||||||||
| 949 | This means that if you call three methods in a row that all extract information | ||||||||||||||||||||||||||||||||||||||
| 950 | from the data returned via a single endpoint/ID pair, you may get back the | ||||||||||||||||||||||||||||||||||||||
| 951 | cached result, or if the cache has timed out, you'll get data from another call | ||||||||||||||||||||||||||||||||||||||
| 952 | to the Tesla API. In some cases, having the data updated may be desirable, | ||||||||||||||||||||||||||||||||||||||
| 953 | sometimes you want data from the same set. | ||||||||||||||||||||||||||||||||||||||
| 954 | |||||||||||||||||||||||||||||||||||||||
| 955 | Here are some examples on how to deal with the caching mechanism. We will use | ||||||||||||||||||||||||||||||||||||||
| 956 | a L<Tesla::Vehicle> object for this example: | ||||||||||||||||||||||||||||||||||||||
| 957 | |||||||||||||||||||||||||||||||||||||||
| 958 | =head2 Store API cache for 10 seconds | ||||||||||||||||||||||||||||||||||||||
| 959 | |||||||||||||||||||||||||||||||||||||||
| 960 | Again, by default, we cache and return data from the Tesla API for two seconds. | ||||||||||||||||||||||||||||||||||||||
| 961 | Change it to 10: | ||||||||||||||||||||||||||||||||||||||
| 962 | |||||||||||||||||||||||||||||||||||||||
| 963 | my $api = Tesla::API->new(api_cache_timeout => 10); | ||||||||||||||||||||||||||||||||||||||
| 964 | |||||||||||||||||||||||||||||||||||||||
| 965 | ...or: | ||||||||||||||||||||||||||||||||||||||
| 966 | |||||||||||||||||||||||||||||||||||||||
| 967 | my $car = Tesla::Vehicle->new(api_cache_timeout => 10); | ||||||||||||||||||||||||||||||||||||||
| 968 | |||||||||||||||||||||||||||||||||||||||
| 969 | ...or: | ||||||||||||||||||||||||||||||||||||||
| 970 | |||||||||||||||||||||||||||||||||||||||
| 971 | $car->api_cache_timeout(10); | ||||||||||||||||||||||||||||||||||||||
| 972 | |||||||||||||||||||||||||||||||||||||||
| 973 | =head2 Disable API caching | ||||||||||||||||||||||||||||||||||||||
| 974 | |||||||||||||||||||||||||||||||||||||||
| 975 | my $api = Tesla::API->new(api_cache_timeout => 0); | ||||||||||||||||||||||||||||||||||||||
| 976 | |||||||||||||||||||||||||||||||||||||||
| 977 | ...or: | ||||||||||||||||||||||||||||||||||||||
| 978 | |||||||||||||||||||||||||||||||||||||||
| 979 | my $car = Tesla::Vehicle->new(api_cache_timeout => 0); | ||||||||||||||||||||||||||||||||||||||
| 980 | |||||||||||||||||||||||||||||||||||||||
| 981 | ...or: | ||||||||||||||||||||||||||||||||||||||
| 982 | |||||||||||||||||||||||||||||||||||||||
| 983 | $car->api_cache_timeout(0); | ||||||||||||||||||||||||||||||||||||||
| 984 | |||||||||||||||||||||||||||||||||||||||
| 985 | =head2 Flush the API cache | ||||||||||||||||||||||||||||||||||||||
| 986 | |||||||||||||||||||||||||||||||||||||||
| 987 | $api->api_cache_clear; | ||||||||||||||||||||||||||||||||||||||
| 988 | |||||||||||||||||||||||||||||||||||||||
| 989 | ...or: | ||||||||||||||||||||||||||||||||||||||
| 990 | |||||||||||||||||||||||||||||||||||||||
| 991 | $car->api_cache_clear; | ||||||||||||||||||||||||||||||||||||||
| 992 | |||||||||||||||||||||||||||||||||||||||
| 993 | =head2 Permanently use the cached data until manually flushed | ||||||||||||||||||||||||||||||||||||||
| 994 | |||||||||||||||||||||||||||||||||||||||
| 995 | my $api = Tesla::API->new(api_cache_persist => 1); | ||||||||||||||||||||||||||||||||||||||
| 996 | |||||||||||||||||||||||||||||||||||||||
| 997 | ...or: | ||||||||||||||||||||||||||||||||||||||
| 998 | |||||||||||||||||||||||||||||||||||||||
| 999 | my $car = Tesla::Vehicle->new(api_cache_persist => 1); | ||||||||||||||||||||||||||||||||||||||
| 1000 | |||||||||||||||||||||||||||||||||||||||
| 1001 | ...or: | ||||||||||||||||||||||||||||||||||||||
| 1002 | |||||||||||||||||||||||||||||||||||||||
| 1003 | $car->api_cache_persist(1); | ||||||||||||||||||||||||||||||||||||||
| 1004 | |||||||||||||||||||||||||||||||||||||||
| 1005 | =head2 Use the cache for a period of time | ||||||||||||||||||||||||||||||||||||||
| 1006 | |||||||||||||||||||||||||||||||||||||||
| 1007 | If making multiple calls to methods that use the same data set and want to be | ||||||||||||||||||||||||||||||||||||||
| 1008 | sure the data doesn't change until you're done, do this: | ||||||||||||||||||||||||||||||||||||||
| 1009 | |||||||||||||||||||||||||||||||||||||||
| 1010 | my $car = Tesla::Vehicle->new; # Default caching of 2 seconds | ||||||||||||||||||||||||||||||||||||||
| 1011 | |||||||||||||||||||||||||||||||||||||||
| 1012 | sub work { | ||||||||||||||||||||||||||||||||||||||
| 1013 | |||||||||||||||||||||||||||||||||||||||
| 1014 | # Clear the cache so it gets updated, but set it to persistent so once | ||||||||||||||||||||||||||||||||||||||
| 1015 | # the cache data is updated, it remains | ||||||||||||||||||||||||||||||||||||||
| 1016 | |||||||||||||||||||||||||||||||||||||||
| 1017 | $car->api_cache_clear; | ||||||||||||||||||||||||||||||||||||||
| 1018 | $car->api_cache_persist(1); | ||||||||||||||||||||||||||||||||||||||
| 1019 | |||||||||||||||||||||||||||||||||||||||
| 1020 | say $car->online; | ||||||||||||||||||||||||||||||||||||||
| 1021 | say $car->lat; | ||||||||||||||||||||||||||||||||||||||
| 1022 | say $car->lon; | ||||||||||||||||||||||||||||||||||||||
| 1023 | say $car->battery_level; | ||||||||||||||||||||||||||||||||||||||
| 1024 | |||||||||||||||||||||||||||||||||||||||
| 1025 | # Now unset the persist flag so other parts of your program won't be | ||||||||||||||||||||||||||||||||||||||
| 1026 | # affected by it | ||||||||||||||||||||||||||||||||||||||
| 1027 | |||||||||||||||||||||||||||||||||||||||
| 1028 | $car->api_cache_persist(0); | ||||||||||||||||||||||||||||||||||||||
| 1029 | } | ||||||||||||||||||||||||||||||||||||||
| 1030 | |||||||||||||||||||||||||||||||||||||||
| 1031 | If you are sure no other parts of your program will be affected by having a | ||||||||||||||||||||||||||||||||||||||
| 1032 | persistent cache, you can set it globally: | ||||||||||||||||||||||||||||||||||||||
| 1033 | |||||||||||||||||||||||||||||||||||||||
| 1034 | my $car = Tesla::Vehicle->new(api_cache_persist => 1); | ||||||||||||||||||||||||||||||||||||||
| 1035 | |||||||||||||||||||||||||||||||||||||||
| 1036 | while (1) { | ||||||||||||||||||||||||||||||||||||||
| 1037 | |||||||||||||||||||||||||||||||||||||||
| 1038 | # Clear the cache at the beginning of the loop so it gets updated, | ||||||||||||||||||||||||||||||||||||||
| 1039 | # unless you never want new data after the first saving of data | ||||||||||||||||||||||||||||||||||||||
| 1040 | |||||||||||||||||||||||||||||||||||||||
| 1041 | $car->api_cache_clear; | ||||||||||||||||||||||||||||||||||||||
| 1042 | |||||||||||||||||||||||||||||||||||||||
| 1043 | say $car->online; | ||||||||||||||||||||||||||||||||||||||
| 1044 | say $car->lat; | ||||||||||||||||||||||||||||||||||||||
| 1045 | say $car->lon; | ||||||||||||||||||||||||||||||||||||||
| 1046 | say $car->battery_level; | ||||||||||||||||||||||||||||||||||||||
| 1047 | } | ||||||||||||||||||||||||||||||||||||||
| 1048 | |||||||||||||||||||||||||||||||||||||||
| 1049 | =head1 EXAMPLE USAGE | ||||||||||||||||||||||||||||||||||||||
| 1050 | |||||||||||||||||||||||||||||||||||||||
| 1051 | See L<Tesla::Vehicle> for vehicle specific methods. | ||||||||||||||||||||||||||||||||||||||
| 1052 | |||||||||||||||||||||||||||||||||||||||
| 1053 | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||
| 1054 | use Tesla::API; | ||||||||||||||||||||||||||||||||||||||
| 1055 | use feature 'say'; | ||||||||||||||||||||||||||||||||||||||
| 1056 | |||||||||||||||||||||||||||||||||||||||
| 1057 | my $tesla = Tesla::API->new; | ||||||||||||||||||||||||||||||||||||||
| 1058 | my $vehicle_id = 1234238782349137; | ||||||||||||||||||||||||||||||||||||||
| 1059 | |||||||||||||||||||||||||||||||||||||||
| 1060 | print Dumper $tesla->api(endpoint => 'VEHICLE_DATA', id => $vehicle_id); | ||||||||||||||||||||||||||||||||||||||
| 1061 | |||||||||||||||||||||||||||||||||||||||
| 1062 | Output (massively and significantly snipped for brevity): | ||||||||||||||||||||||||||||||||||||||
| 1063 | |||||||||||||||||||||||||||||||||||||||
| 1064 | $VAR1 = { | ||||||||||||||||||||||||||||||||||||||
| 1065 | 'vehicle_config' => { | ||||||||||||||||||||||||||||||||||||||
| 1066 | 'car_type' => 'modelx', | ||||||||||||||||||||||||||||||||||||||
| 1067 | 'rear_seat_type' => 7, | ||||||||||||||||||||||||||||||||||||||
| 1068 | 'rear_drive_unit' => 'Small', | ||||||||||||||||||||||||||||||||||||||
| 1069 | 'wheel_type' => 'Turbine22Dark', | ||||||||||||||||||||||||||||||||||||||
| 1070 | 'timestamp' => '1647461524710', | ||||||||||||||||||||||||||||||||||||||
| 1071 | 'rear_seat_heaters' => 3, | ||||||||||||||||||||||||||||||||||||||
| 1072 | 'trim_badging' => '100d', | ||||||||||||||||||||||||||||||||||||||
| 1073 | 'headlamp_type' => 'Led', | ||||||||||||||||||||||||||||||||||||||
| 1074 | 'driver_assist' => 'TeslaAP3', | ||||||||||||||||||||||||||||||||||||||
| 1075 | }, | ||||||||||||||||||||||||||||||||||||||
| 1076 | 'id_s' => 'XXXXXXXXXXXXXXXXX', | ||||||||||||||||||||||||||||||||||||||
| 1077 | 'vehicle_id' => 'XXXXXXXXXX', | ||||||||||||||||||||||||||||||||||||||
| 1078 | 'charge_state' => { | ||||||||||||||||||||||||||||||||||||||
| 1079 | 'usable_battery_level' => 69, | ||||||||||||||||||||||||||||||||||||||
| 1080 | 'battery_range' => '189.58', | ||||||||||||||||||||||||||||||||||||||
| 1081 | 'charge_limit_soc_std' => 90, | ||||||||||||||||||||||||||||||||||||||
| 1082 | 'charge_amps' => 48, | ||||||||||||||||||||||||||||||||||||||
| 1083 | 'charge_limit_soc' => 90, | ||||||||||||||||||||||||||||||||||||||
| 1084 | 'battery_level' => 69, | ||||||||||||||||||||||||||||||||||||||
| 1085 | }, | ||||||||||||||||||||||||||||||||||||||
| 1086 | 'vin' => 'XXXXXXXX', | ||||||||||||||||||||||||||||||||||||||
| 1087 | 'in_service' => $VAR1->{'vehicle_config'}{'use_range_badging'}, | ||||||||||||||||||||||||||||||||||||||
| 1088 | 'user_id' => 'XXXXXX', | ||||||||||||||||||||||||||||||||||||||
| 1089 | 'id' => 'XXXXXXXXXXXXX', | ||||||||||||||||||||||||||||||||||||||
| 1090 | 'drive_state' => { | ||||||||||||||||||||||||||||||||||||||
| 1091 | 'shift_state' => 'P', | ||||||||||||||||||||||||||||||||||||||
| 1092 | 'heading' => 92, | ||||||||||||||||||||||||||||||||||||||
| 1093 | 'longitude' => '-XXX.XXXXXX', | ||||||||||||||||||||||||||||||||||||||
| 1094 | 'latitude' => 'XX.XXXXXX', | ||||||||||||||||||||||||||||||||||||||
| 1095 | 'power' => 0, | ||||||||||||||||||||||||||||||||||||||
| 1096 | 'speed' => undef, | ||||||||||||||||||||||||||||||||||||||
| 1097 | }, | ||||||||||||||||||||||||||||||||||||||
| 1098 | 'api_version' => 34, | ||||||||||||||||||||||||||||||||||||||
| 1099 | 'display_name' => 'Dream machine', | ||||||||||||||||||||||||||||||||||||||
| 1100 | 'state' => 'online', | ||||||||||||||||||||||||||||||||||||||
| 1101 | 'access_type' => 'OWNER', | ||||||||||||||||||||||||||||||||||||||
| 1102 | 'option_codes' => 'AD15,MDL3,PBSB,RENA,BT37,ID3W,RF3G,S3PB,DRLH,DV2W,W39B,APF0,COUS,BC3B,CH07,PC30,FC3P,FG31,GLFR,HL31,HM31,IL31,LTPB,MR31,FM3B,RS3H,SA3P,STCP,SC04,SU3C,T3CA,TW00,TM00,UT3P,WR00,AU3P,APH3,AF00,ZCST,MI00,CDM0', | ||||||||||||||||||||||||||||||||||||||
| 1103 | 'vehicle_state' => { | ||||||||||||||||||||||||||||||||||||||
| 1104 | 'valet_mode' => $VAR1->{'vehicle_config'}{'use_range_badging'}, | ||||||||||||||||||||||||||||||||||||||
| 1105 | 'vehicle_name' => 'Dream machine', | ||||||||||||||||||||||||||||||||||||||
| 1106 | 'sentry_mode_available' => $VAR1->{'vehicle_config'}{'plg'}, | ||||||||||||||||||||||||||||||||||||||
| 1107 | 'sentry_mode' => $VAR1->{'vehicle_config'}{'use_range_badging'}, | ||||||||||||||||||||||||||||||||||||||
| 1108 | 'car_version' => '2022.4.5.4 abcfac6bfcdc', | ||||||||||||||||||||||||||||||||||||||
| 1109 | 'homelink_device_count' => 3, | ||||||||||||||||||||||||||||||||||||||
| 1110 | 'is_user_present' => $VAR1->{'vehicle_config'}{'use_range_badging'}, | ||||||||||||||||||||||||||||||||||||||
| 1111 | 'odometer' => 'XXXXXXX.233656', | ||||||||||||||||||||||||||||||||||||||
| 1112 | 'media_state' => { | ||||||||||||||||||||||||||||||||||||||
| 1113 | 'remote_control_enabled' => $VAR1->{'vehicle_config'}{'plg'} | ||||||||||||||||||||||||||||||||||||||
| 1114 | }, | ||||||||||||||||||||||||||||||||||||||
| 1115 | }, | ||||||||||||||||||||||||||||||||||||||
| 1116 | 'autopark_style' => 'dead_man', | ||||||||||||||||||||||||||||||||||||||
| 1117 | 'software_update' => { | ||||||||||||||||||||||||||||||||||||||
| 1118 | 'expected_duration_sec' => 2700, | ||||||||||||||||||||||||||||||||||||||
| 1119 | 'version' => ' ', | ||||||||||||||||||||||||||||||||||||||
| 1120 | 'status' => '', | ||||||||||||||||||||||||||||||||||||||
| 1121 | 'download_perc' => 0, | ||||||||||||||||||||||||||||||||||||||
| 1122 | 'install_perc' => 1 | ||||||||||||||||||||||||||||||||||||||
| 1123 | }, | ||||||||||||||||||||||||||||||||||||||
| 1124 | 'speed_limit_mode' => { | ||||||||||||||||||||||||||||||||||||||
| 1125 | 'max_limit_mph' => 90, | ||||||||||||||||||||||||||||||||||||||
| 1126 | 'min_limit_mph' => '50', | ||||||||||||||||||||||||||||||||||||||
| 1127 | 'active' => $VAR1->{'vehicle_config'}{'use_range_badging'}, | ||||||||||||||||||||||||||||||||||||||
| 1128 | 'current_limit_mph' => '80.029031', | ||||||||||||||||||||||||||||||||||||||
| 1129 | 'pin_code_set' => $VAR1->{'vehicle_config'}{'plg'} | ||||||||||||||||||||||||||||||||||||||
| 1130 | }, | ||||||||||||||||||||||||||||||||||||||
| 1131 | 'climate_state' => { | ||||||||||||||||||||||||||||||||||||||
| 1132 | 'passenger_temp_setting' => '20.5', | ||||||||||||||||||||||||||||||||||||||
| 1133 | 'driver_temp_setting' => '20.5', | ||||||||||||||||||||||||||||||||||||||
| 1134 | 'side_mirror_heaters' => $VAR1->{'vehicle_config'}{'use_range_badging'}, | ||||||||||||||||||||||||||||||||||||||
| 1135 | 'is_climate_on' => $VAR1->{'vehicle_config'}{'use_range_badging'}, | ||||||||||||||||||||||||||||||||||||||
| 1136 | 'fan_status' => 0, | ||||||||||||||||||||||||||||||||||||||
| 1137 | 'seat_heater_third_row_right' => 0, | ||||||||||||||||||||||||||||||||||||||
| 1138 | 'seat_heater_right' => 0, | ||||||||||||||||||||||||||||||||||||||
| 1139 | 'is_front_defroster_on' => $VAR1->{'vehicle_config'}{'use_range_badging'}, | ||||||||||||||||||||||||||||||||||||||
| 1140 | 'battery_heater' => $VAR1->{'vehicle_config'}{'use_range_badging'}, | ||||||||||||||||||||||||||||||||||||||
| 1141 | 'is_rear_defroster_on' => $VAR1->{'vehicle_config'}{'use_range_badging'}, | ||||||||||||||||||||||||||||||||||||||
| 1142 | }, | ||||||||||||||||||||||||||||||||||||||
| 1143 | 'gui_settings' => { | ||||||||||||||||||||||||||||||||||||||
| 1144 | 'gui_temperature_units' => 'C', | ||||||||||||||||||||||||||||||||||||||
| 1145 | 'gui_charge_rate_units' => 'km/hr', | ||||||||||||||||||||||||||||||||||||||
| 1146 | 'gui_24_hour_time' => $VAR1->{'vehicle_config'}{'use_range_badging'}, | ||||||||||||||||||||||||||||||||||||||
| 1147 | 'gui_range_display' => 'Ideal', | ||||||||||||||||||||||||||||||||||||||
| 1148 | 'show_range_units' => $VAR1->{'vehicle_config'}{'plg'}, | ||||||||||||||||||||||||||||||||||||||
| 1149 | 'gui_distance_units' => 'km/hr', | ||||||||||||||||||||||||||||||||||||||
| 1150 | 'timestamp' => '1647461524710' | ||||||||||||||||||||||||||||||||||||||
| 1151 | } | ||||||||||||||||||||||||||||||||||||||
| 1152 | }; | ||||||||||||||||||||||||||||||||||||||
| 1153 | |||||||||||||||||||||||||||||||||||||||
| 1154 | =head1 CONFIGURATION VARIABLES | ||||||||||||||||||||||||||||||||||||||
| 1155 | |||||||||||||||||||||||||||||||||||||||
| 1156 | Most configuration options used in this software are defined as constants in the | ||||||||||||||||||||||||||||||||||||||
| 1157 | library file. Some are defaults that can be overridden via method calls, others | ||||||||||||||||||||||||||||||||||||||
| 1158 | are only modifiable by updating the actual value in the file. | ||||||||||||||||||||||||||||||||||||||
| 1159 | |||||||||||||||||||||||||||||||||||||||
| 1160 | =head2 DEBUG_CACHE | ||||||||||||||||||||||||||||||||||||||
| 1161 | |||||||||||||||||||||||||||||||||||||||
| 1162 | Prints to C<STDOUT> debugging output from the caching mechanism. | ||||||||||||||||||||||||||||||||||||||
| 1163 | |||||||||||||||||||||||||||||||||||||||
| 1164 | I<Override>: C<$ENV{DEBUG_TESLA_API_CACHE}>. Note that this must be configured | ||||||||||||||||||||||||||||||||||||||
| 1165 | within a C<BEGIN> block, prior to the C<use Tesla::API> line for it to have | ||||||||||||||||||||||||||||||||||||||
| 1166 | effect. | ||||||||||||||||||||||||||||||||||||||
| 1167 | |||||||||||||||||||||||||||||||||||||||
| 1168 | =head2 API_CACHE_PERSIST | ||||||||||||||||||||||||||||||||||||||
| 1169 | |||||||||||||||||||||||||||||||||||||||
| 1170 | Always/never use the cache once data has been retrieved through the Tesla API. | ||||||||||||||||||||||||||||||||||||||
| 1171 | |||||||||||||||||||||||||||||||||||||||
| 1172 | I<Default>: False (C<0>). | ||||||||||||||||||||||||||||||||||||||
| 1173 | |||||||||||||||||||||||||||||||||||||||
| 1174 | I<Override>: None | ||||||||||||||||||||||||||||||||||||||
| 1175 | |||||||||||||||||||||||||||||||||||||||
| 1176 | =head2 API_CACHE_TIMEOUT_SECONDS | ||||||||||||||||||||||||||||||||||||||
| 1177 | |||||||||||||||||||||||||||||||||||||||
| 1178 | How many seconds to reuse the cached data retrieved from the Tesla API. | ||||||||||||||||||||||||||||||||||||||
| 1179 | |||||||||||||||||||||||||||||||||||||||
| 1180 | I<Default>: C<2> | ||||||||||||||||||||||||||||||||||||||
| 1181 | |||||||||||||||||||||||||||||||||||||||
| 1182 | I<Override>: L</api_cache_time($cache_seconds)> | ||||||||||||||||||||||||||||||||||||||
| 1183 | |||||||||||||||||||||||||||||||||||||||
| 1184 | =head2 API_CACHE_RETRIES | ||||||||||||||||||||||||||||||||||||||
| 1185 | |||||||||||||||||||||||||||||||||||||||
| 1186 | How many times we'll try a Tesla API call in the event of a failure. | ||||||||||||||||||||||||||||||||||||||
| 1187 | |||||||||||||||||||||||||||||||||||||||
| 1188 | I<Default>: C<3> | ||||||||||||||||||||||||||||||||||||||
| 1189 | |||||||||||||||||||||||||||||||||||||||
| 1190 | I<Override>: None | ||||||||||||||||||||||||||||||||||||||
| 1191 | |||||||||||||||||||||||||||||||||||||||
| 1192 | =head2 AUTH_CACHE_FILE | ||||||||||||||||||||||||||||||||||||||
| 1193 | |||||||||||||||||||||||||||||||||||||||
| 1194 | The path and filename of the file we'll store the Tesla API access token | ||||||||||||||||||||||||||||||||||||||
| 1195 | information. | ||||||||||||||||||||||||||||||||||||||
| 1196 | |||||||||||||||||||||||||||||||||||||||
| 1197 | I<Default>: C<$home_dir/tesla_api_cache.json> | ||||||||||||||||||||||||||||||||||||||
| 1198 | |||||||||||||||||||||||||||||||||||||||
| 1199 | I<Override>: C<_authentication_cache_file()>, used primarily for unit testing. | ||||||||||||||||||||||||||||||||||||||
| 1200 | |||||||||||||||||||||||||||||||||||||||
| 1201 | =head2 ENDPOINTS_FILE | ||||||||||||||||||||||||||||||||||||||
| 1202 | |||||||||||||||||||||||||||||||||||||||
| 1203 | The path and filename of the file we'll store the Tesla API endpoint description | ||||||||||||||||||||||||||||||||||||||
| 1204 | file. | ||||||||||||||||||||||||||||||||||||||
| 1205 | |||||||||||||||||||||||||||||||||||||||
| 1206 | I<Default>: C<dist_file('Tesla-API', 'endpoints.json')> | ||||||||||||||||||||||||||||||||||||||
| 1207 | |||||||||||||||||||||||||||||||||||||||
| 1208 | I<Override>: C<$ENV{TESLA_API_ENDPOINTS_FILE}>. Note that this must be | ||||||||||||||||||||||||||||||||||||||
| 1209 | configured within a C<BEGIN> block, prior to the C<use Tesla::API> line for it | ||||||||||||||||||||||||||||||||||||||
| 1210 | to have effect. | ||||||||||||||||||||||||||||||||||||||
| 1211 | |||||||||||||||||||||||||||||||||||||||
| 1212 | =head2 OPTION_CODES_FILE | ||||||||||||||||||||||||||||||||||||||
| 1213 | |||||||||||||||||||||||||||||||||||||||
| 1214 | The path and filename of the file we'll store the product option code list file | ||||||||||||||||||||||||||||||||||||||
| 1215 | for Tesla products. | ||||||||||||||||||||||||||||||||||||||
| 1216 | |||||||||||||||||||||||||||||||||||||||
| 1217 | I<Default>: C<dist_file('Tesla-API', 'option_codes.json')> | ||||||||||||||||||||||||||||||||||||||
| 1218 | |||||||||||||||||||||||||||||||||||||||
| 1219 | I<Override>: C<$ENV{TESLA_API_OPTIONCODES_FILE}>. Note that this must be | ||||||||||||||||||||||||||||||||||||||
| 1220 | configured within a C<BEGIN> block, prior to the C<use Tesla::API> line for it | ||||||||||||||||||||||||||||||||||||||
| 1221 | to have effect. | ||||||||||||||||||||||||||||||||||||||
| 1222 | |||||||||||||||||||||||||||||||||||||||
| 1223 | =head2 TOKEN_EXPIRY_WINDOW | ||||||||||||||||||||||||||||||||||||||
| 1224 | |||||||||||||||||||||||||||||||||||||||
| 1225 | The number of seconds we'll add to the current time when validating the token | ||||||||||||||||||||||||||||||||||||||
| 1226 | expiry. This is effectively a cusion window so that the token has at least this | ||||||||||||||||||||||||||||||||||||||
| 1227 | many seconds before expiring to ensure the next call won't use an invalidated | ||||||||||||||||||||||||||||||||||||||
| 1228 | token | ||||||||||||||||||||||||||||||||||||||
| 1229 | |||||||||||||||||||||||||||||||||||||||
| 1230 | I<Default>: C<5> | ||||||||||||||||||||||||||||||||||||||
| 1231 | |||||||||||||||||||||||||||||||||||||||
| 1232 | I<Override>: None | ||||||||||||||||||||||||||||||||||||||
| 1233 | |||||||||||||||||||||||||||||||||||||||
| 1234 | =head2 URI_API | ||||||||||||||||||||||||||||||||||||||
| 1235 | |||||||||||||||||||||||||||||||||||||||
| 1236 | The URL we use to communicate with the Tesla API for data retrieval operations. | ||||||||||||||||||||||||||||||||||||||
| 1237 | |||||||||||||||||||||||||||||||||||||||
| 1238 | I<Default>: C<https://owner-api.teslamotors.com/> | ||||||||||||||||||||||||||||||||||||||
| 1239 | |||||||||||||||||||||||||||||||||||||||
| 1240 | I<Override>: None | ||||||||||||||||||||||||||||||||||||||
| 1241 | |||||||||||||||||||||||||||||||||||||||
| 1242 | =head2 URI_ENDPOINTS | ||||||||||||||||||||||||||||||||||||||
| 1243 | |||||||||||||||||||||||||||||||||||||||
| 1244 | The URL we use to retrieve the updated Tesla API endpoints file. | ||||||||||||||||||||||||||||||||||||||
| 1245 | |||||||||||||||||||||||||||||||||||||||
| 1246 | I<Default>: C<https://raw.githubusercontent.com/tdorssers/TeslaPy/master/teslapy/endpoints.json> | ||||||||||||||||||||||||||||||||||||||
| 1247 | |||||||||||||||||||||||||||||||||||||||
| 1248 | I<Override>: None | ||||||||||||||||||||||||||||||||||||||
| 1249 | |||||||||||||||||||||||||||||||||||||||
| 1250 | =head2 URI_OPTION_CODES | ||||||||||||||||||||||||||||||||||||||
| 1251 | |||||||||||||||||||||||||||||||||||||||
| 1252 | The URL we use to retrieve the updated Tesla API product option codes file. | ||||||||||||||||||||||||||||||||||||||
| 1253 | |||||||||||||||||||||||||||||||||||||||
| 1254 | I<Default>: C<https://raw.githubusercontent.com/tdorssers/TeslaPy/master/teslapy/option_codes.json> | ||||||||||||||||||||||||||||||||||||||
| 1255 | |||||||||||||||||||||||||||||||||||||||
| 1256 | I<Override>: None | ||||||||||||||||||||||||||||||||||||||
| 1257 | |||||||||||||||||||||||||||||||||||||||
| 1258 | =head2 URI_AUTH | ||||||||||||||||||||||||||||||||||||||
| 1259 | |||||||||||||||||||||||||||||||||||||||
| 1260 | The URL we use to perform the Tesla API authentication routines. | ||||||||||||||||||||||||||||||||||||||
| 1261 | |||||||||||||||||||||||||||||||||||||||
| 1262 | I<Default>: C<https://auth.tesla.com/oauth2/v3/authorize> | ||||||||||||||||||||||||||||||||||||||
| 1263 | |||||||||||||||||||||||||||||||||||||||
| 1264 | I<Override>: None | ||||||||||||||||||||||||||||||||||||||
| 1265 | |||||||||||||||||||||||||||||||||||||||
| 1266 | =head2 URI_TOKEN | ||||||||||||||||||||||||||||||||||||||
| 1267 | |||||||||||||||||||||||||||||||||||||||
| 1268 | The URL we use to fetch and update the Tesla API access tokens. | ||||||||||||||||||||||||||||||||||||||
| 1269 | |||||||||||||||||||||||||||||||||||||||
| 1270 | I<Default>: C<https://auth.tesla.com/oauth2/v3/token> | ||||||||||||||||||||||||||||||||||||||
| 1271 | |||||||||||||||||||||||||||||||||||||||
| 1272 | I<Override>: None | ||||||||||||||||||||||||||||||||||||||
| 1273 | |||||||||||||||||||||||||||||||||||||||
| 1274 | =head2 USERAGENT_STRING | ||||||||||||||||||||||||||||||||||||||
| 1275 | |||||||||||||||||||||||||||||||||||||||
| 1276 | String used to identify the 'browser' we're using to access the Tesla API. | ||||||||||||||||||||||||||||||||||||||
| 1277 | |||||||||||||||||||||||||||||||||||||||
| 1278 | I<Default>: C<Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0> | ||||||||||||||||||||||||||||||||||||||
| 1279 | |||||||||||||||||||||||||||||||||||||||
| 1280 | I<Override>: L</useragent_string($ua_string)> | ||||||||||||||||||||||||||||||||||||||
| 1281 | |||||||||||||||||||||||||||||||||||||||
| 1282 | =head2 USERAGENT_TIMEOUT | ||||||||||||||||||||||||||||||||||||||
| 1283 | |||||||||||||||||||||||||||||||||||||||
| 1284 | Number of seconds before we classify a call to the Tesla API as timed out. | ||||||||||||||||||||||||||||||||||||||
| 1285 | |||||||||||||||||||||||||||||||||||||||
| 1286 | I<Default>: C<180> | ||||||||||||||||||||||||||||||||||||||
| 1287 | |||||||||||||||||||||||||||||||||||||||
| 1288 | I<Override>: L</useragent_timeout($timeout)> | ||||||||||||||||||||||||||||||||||||||
| 1289 | |||||||||||||||||||||||||||||||||||||||
| 1290 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||
| 1291 | |||||||||||||||||||||||||||||||||||||||
| 1292 | Steve Bertrand, C<< <steveb at cpan.org> >> | ||||||||||||||||||||||||||||||||||||||
| 1293 | |||||||||||||||||||||||||||||||||||||||
| 1294 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||
| 1295 | |||||||||||||||||||||||||||||||||||||||
| 1296 | This distribution suite has been a long time in the works. For my other projects | ||||||||||||||||||||||||||||||||||||||
| 1297 | written in Perl previous to writing this code that required data from the Tesla | ||||||||||||||||||||||||||||||||||||||
| 1298 | API, I wrapped L<Tim Dorssers|https://github.com/tdorssers> wonderful | ||||||||||||||||||||||||||||||||||||||
| 1299 | L<TeslaPy|https://github.com/tdorssers/TeslaPy> Python project. | ||||||||||||||||||||||||||||||||||||||
| 1300 | |||||||||||||||||||||||||||||||||||||||
| 1301 | Much of the code in this distribution is heavily influenced by the code his | ||||||||||||||||||||||||||||||||||||||
| 1302 | project, and currently, we're using a direct copy of its | ||||||||||||||||||||||||||||||||||||||
| 1303 | L<Tesla API endpoint file|https://github.com/tdorssers/TeslaPy/blob/master/teslapy/endpoints.json>. | ||||||||||||||||||||||||||||||||||||||
| 1304 | |||||||||||||||||||||||||||||||||||||||
| 1305 | Thanks Tim, and great work! | ||||||||||||||||||||||||||||||||||||||
| 1306 | |||||||||||||||||||||||||||||||||||||||
| 1307 | Also thanks goes out to L<https://teslaapi.io>, as a lot of the actual request | ||||||||||||||||||||||||||||||||||||||
| 1308 | parameter information and response data layout I learned from that site while | ||||||||||||||||||||||||||||||||||||||
| 1309 | implementing the actual REST calls to the Tesla API. | ||||||||||||||||||||||||||||||||||||||
| 1310 | |||||||||||||||||||||||||||||||||||||||
| 1311 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||
| 1312 | |||||||||||||||||||||||||||||||||||||||
| 1313 | Copyright 2022 Steve Bertrand. | ||||||||||||||||||||||||||||||||||||||
| 1314 | |||||||||||||||||||||||||||||||||||||||
| 1315 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||
| 1316 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||||||||||||||||||||||||||
| 1317 | copy of the full license at: | ||||||||||||||||||||||||||||||||||||||
| 1318 | |||||||||||||||||||||||||||||||||||||||
| 1319 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||||||||||||
| 1320 | |||||||||||||||||||||||||||||||||||||||
| 1321 | The copied endpoint code data borrowed from Tim's B<TeslaPy> project has been | ||||||||||||||||||||||||||||||||||||||
| 1322 | rebranded with the Perl license here, as permitted by the MIT license TeslaPy | ||||||||||||||||||||||||||||||||||||||
| 1323 | is licensed under. | ||||||||||||||||||||||||||||||||||||||
| 1324 | |||||||||||||||||||||||||||||||||||||||
| 1325 | =cut | ||||||||||||||||||||||||||||||||||||||