tesla 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.yardopts +5 -0
- data/CHANGELOG.md +31 -0
- data/CONTRIBUTING.md +45 -0
- data/{LICENSE.txt → LICENSE.md} +1 -1
- data/README.md +366 -0
- data/SECURITY.md +38 -0
- data/lib/tesla/api/alert_endpoints.rb +35 -0
- data/lib/tesla/api/charging_endpoints.rb +115 -0
- data/lib/tesla/api/climate_endpoints.rb +180 -0
- data/lib/tesla/api/closure_endpoints.rb +142 -0
- data/lib/tesla/api/command_endpoints.rb +63 -0
- data/lib/tesla/api/media_endpoints.rb +114 -0
- data/lib/tesla/api/navigation_endpoints.rb +51 -0
- data/lib/tesla/api/oauth_endpoints.rb +118 -0
- data/lib/tesla/api/partner_endpoints.rb +62 -0
- data/lib/tesla/api/security_endpoints.rb +123 -0
- data/lib/tesla/api/software_endpoints.rb +38 -0
- data/lib/tesla/api/user_endpoints.rb +40 -0
- data/lib/tesla/api/vehicle_endpoints.rb +174 -0
- data/lib/tesla/api.rb +39 -0
- data/lib/tesla/authenticator.rb +33 -0
- data/lib/tesla/bearer_token_authenticator.rb +38 -0
- data/lib/tesla/certificate_options.rb +51 -0
- data/lib/tesla/charge_state.rb +111 -0
- data/lib/tesla/charging_site.rb +67 -0
- data/lib/tesla/client.rb +336 -0
- data/lib/tesla/client_credentials.rb +230 -0
- data/lib/tesla/climate_state.rb +116 -0
- data/lib/tesla/configuration.rb +483 -0
- data/lib/tesla/connection.rb +361 -0
- data/lib/tesla/connection_pool.rb +186 -0
- data/lib/tesla/deep_copy.rb +41 -0
- data/lib/tesla/drive_state.rb +80 -0
- data/lib/tesla/errors/bad_gateway.rb +9 -0
- data/lib/tesla/errors/bad_request.rb +9 -0
- data/lib/tesla/errors/client_error.rb +9 -0
- data/lib/tesla/errors/command_failed.rb +42 -0
- data/lib/tesla/errors/device_unexpected_response.rb +12 -0
- data/lib/tesla/errors/error.rb +7 -0
- data/lib/tesla/errors/forbidden.rb +14 -0
- data/lib/tesla/errors/gateway_timeout.rb +9 -0
- data/lib/tesla/errors/http_error.rb +89 -0
- data/lib/tesla/errors/internal_server_error.rb +9 -0
- data/lib/tesla/errors/invalid_response.rb +33 -0
- data/lib/tesla/errors/locked.rb +12 -0
- data/lib/tesla/errors/method_not_allowed.rb +9 -0
- data/lib/tesla/errors/misdirected_request.rb +13 -0
- data/lib/tesla/errors/network_error.rb +30 -0
- data/lib/tesla/errors/not_acceptable.rb +9 -0
- data/lib/tesla/errors/not_found.rb +9 -0
- data/lib/tesla/errors/oauth_error.rb +53 -0
- data/lib/tesla/errors/payment_required.rb +12 -0
- data/lib/tesla/errors/precondition_failed.rb +13 -0
- data/lib/tesla/errors/request_timeout.rb +13 -0
- data/lib/tesla/errors/server_error.rb +9 -0
- data/lib/tesla/errors/service_unavailable.rb +9 -0
- data/lib/tesla/errors/too_many_redirects.rb +9 -0
- data/lib/tesla/errors/too_many_requests.rb +9 -0
- data/lib/tesla/errors/unauthorized.rb +9 -0
- data/lib/tesla/errors/unavailable_for_legal_reasons.rb +12 -0
- data/lib/tesla/errors/unprocessable_content.rb +12 -0
- data/lib/tesla/errors/vehicle_asleep.rb +32 -0
- data/lib/tesla/gui_settings.rb +60 -0
- data/lib/tesla/idempotence.rb +30 -0
- data/lib/tesla/identifiers.rb +27 -0
- data/lib/tesla/json_parsing.rb +38 -0
- data/lib/tesla/nearby_charging_sites.rb +35 -0
- data/lib/tesla/path_escaping.rb +30 -0
- data/lib/tesla/redacted_output.rb +161 -0
- data/lib/tesla/redirect_handler.rb +196 -0
- data/lib/tesla/region.rb +27 -0
- data/lib/tesla/request_builder.rb +138 -0
- data/lib/tesla/resource.rb +403 -0
- data/lib/tesla/response_parser.rb +92 -0
- data/lib/tesla/retry_after.rb +34 -0
- data/lib/tesla/retry_handler.rb +252 -0
- data/lib/tesla/settings.rb +115 -0
- data/lib/tesla/url_validation.rb +131 -0
- data/lib/tesla/user.rb +36 -0
- data/lib/tesla/vehicle.rb +90 -0
- data/lib/tesla/vehicle_config.rb +95 -0
- data/lib/tesla/vehicle_data.rb +67 -0
- data/lib/tesla/vehicle_state.rb +125 -0
- data/lib/tesla/version.rb +6 -0
- data/lib/tesla.rb +139 -0
- data/sig/manifest.yaml +10 -0
- data/sig/tesla.rbs +989 -0
- metadata +134 -118
- data/.document +0 -5
- data/.rvmrc +0 -1
- data/Gemfile +0 -13
- data/Gemfile.lock +0 -20
- data/README.rdoc +0 -19
- data/Rakefile +0 -47
- data/test/helper.rb +0 -19
- data/test/test_tesla.rb +0 -7
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "certificate_options"
|
|
4
|
+
require_relative "connection"
|
|
5
|
+
require_relative "redirect_handler"
|
|
6
|
+
require_relative "request_builder"
|
|
7
|
+
require_relative "retry_handler"
|
|
8
|
+
require_relative "settings"
|
|
9
|
+
require_relative "url_validation"
|
|
10
|
+
|
|
11
|
+
module Tesla
|
|
12
|
+
# Global configuration for {Tesla::Client} instances
|
|
13
|
+
# @api public
|
|
14
|
+
module Configuration
|
|
15
|
+
include CertificateOptions
|
|
16
|
+
include Settings
|
|
17
|
+
include URLValidation
|
|
18
|
+
|
|
19
|
+
# The host of the Fleet API for North America and Asia-Pacific, other than China
|
|
20
|
+
NORTH_AMERICA_HOST = "https://fleet-api.prd.na.vn.cloud.tesla.com"
|
|
21
|
+
|
|
22
|
+
# The host of the Fleet API for Europe, the Middle East, and Africa
|
|
23
|
+
EUROPE_HOST = "https://fleet-api.prd.eu.vn.cloud.tesla.com"
|
|
24
|
+
|
|
25
|
+
# The host of the Fleet API for China
|
|
26
|
+
CHINA_HOST = "https://fleet-api.prd.cn.vn.cloud.tesla.cn"
|
|
27
|
+
|
|
28
|
+
# The API endpoint used when the TESLA_HOST environment variable is not set
|
|
29
|
+
DEFAULT_HOST = NORTH_AMERICA_HOST
|
|
30
|
+
|
|
31
|
+
# The URL a user is sent to in order to authorize the application
|
|
32
|
+
DEFAULT_AUTHORIZATION_ENDPOINT = "https://auth.tesla.com/oauth2/v3/authorize"
|
|
33
|
+
|
|
34
|
+
# The URL tokens are issued and refreshed at
|
|
35
|
+
DEFAULT_TOKEN_ENDPOINT = "https://fleet-auth.prd.vn.cloud.tesla.com/oauth2/v3/token"
|
|
36
|
+
|
|
37
|
+
# The default 'User-Agent' HTTP header
|
|
38
|
+
DEFAULT_USER_AGENT = RequestBuilder::DEFAULT_USER_AGENT
|
|
39
|
+
|
|
40
|
+
# The credentials that fall back to an environment variable until they are assigned
|
|
41
|
+
ENVIRONMENT = {
|
|
42
|
+
access_token: "TESLA_ACCESS_TOKEN",
|
|
43
|
+
refresh_token: "TESLA_REFRESH_TOKEN",
|
|
44
|
+
client_id: "TESLA_CLIENT_ID",
|
|
45
|
+
client_secret: "TESLA_CLIENT_SECRET",
|
|
46
|
+
redirect_uri: "TESLA_REDIRECT_URI"
|
|
47
|
+
}.freeze
|
|
48
|
+
private_constant :ENVIRONMENT
|
|
49
|
+
|
|
50
|
+
# The host used for API requests
|
|
51
|
+
#
|
|
52
|
+
# This is the host of the Fleet API for the region of the account (see {API::UserEndpoints#region}), or the
|
|
53
|
+
# host the vehicle command proxy listens at, which signs the commands of the vehicles that take only signed
|
|
54
|
+
# ones and hands every other request on to the Fleet API.
|
|
55
|
+
#
|
|
56
|
+
# @api public
|
|
57
|
+
# @return [String] the host, including scheme
|
|
58
|
+
# @example Get the host
|
|
59
|
+
# Tesla.host
|
|
60
|
+
attr_reader :host
|
|
61
|
+
|
|
62
|
+
# Set the host used for API requests
|
|
63
|
+
#
|
|
64
|
+
# The host is checked here rather than when a request is made with it, as {Client#host=} checks the host of a
|
|
65
|
+
# client, so that the error names the assignment that was wrong.
|
|
66
|
+
#
|
|
67
|
+
# @api public
|
|
68
|
+
# @param host [String] the host, including scheme
|
|
69
|
+
# @return [void]
|
|
70
|
+
# @raise [ArgumentError] if the host is not an HTTP or HTTPS URL, in which case the host is left as it was
|
|
71
|
+
# @example Send requests to the Fleet API for Europe
|
|
72
|
+
# Tesla.host = Tesla::Configuration::EUROPE_HOST
|
|
73
|
+
# @example Send requests through the vehicle command proxy
|
|
74
|
+
# Tesla.host = "https://localhost:4443"
|
|
75
|
+
def host=(host)
|
|
76
|
+
@host = validate_host(host)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# The audience tokens are issued for
|
|
80
|
+
#
|
|
81
|
+
# The token endpoint issues a token for the host of the Fleet API it is used with, which is the host of the
|
|
82
|
+
# client unless that is the vehicle command proxy, in which case the host of the Fleet API for the region of the
|
|
83
|
+
# account is assigned here.
|
|
84
|
+
#
|
|
85
|
+
# @api public
|
|
86
|
+
# @return [String, nil] the host of the Fleet API the tokens are used with, or nil for the host of the client
|
|
87
|
+
# @example Issue tokens for the Fleet API for Europe while requests are sent through a proxy
|
|
88
|
+
# Tesla.audience = Tesla::Configuration::EUROPE_HOST
|
|
89
|
+
attr_accessor :audience
|
|
90
|
+
|
|
91
|
+
# The URL a user is sent to in order to authorize the application
|
|
92
|
+
# @api public
|
|
93
|
+
# @return [String] the authorization endpoint
|
|
94
|
+
# @example Get or set the authorization endpoint
|
|
95
|
+
# Tesla.authorization_endpoint = "https://auth.tesla.cn/oauth2/v3/authorize"
|
|
96
|
+
attr_accessor :authorization_endpoint
|
|
97
|
+
|
|
98
|
+
# The URL tokens are issued and refreshed at
|
|
99
|
+
# @api public
|
|
100
|
+
# @return [String] the token endpoint
|
|
101
|
+
# @example Get or set the token endpoint
|
|
102
|
+
# Tesla.token_endpoint = "https://auth.tesla.cn/oauth2/v3/token"
|
|
103
|
+
attr_accessor :token_endpoint
|
|
104
|
+
|
|
105
|
+
# @!method access_token
|
|
106
|
+
# The access token requests are authorized with
|
|
107
|
+
#
|
|
108
|
+
# Falls back to the TESLA_ACCESS_TOKEN environment variable until one is assigned.
|
|
109
|
+
#
|
|
110
|
+
# @api public
|
|
111
|
+
# @return [String, nil] the access token
|
|
112
|
+
# @example Get the access token
|
|
113
|
+
# Tesla.access_token
|
|
114
|
+
# @!method access_token=(access_token)
|
|
115
|
+
# Set the access token requests are authorized with
|
|
116
|
+
# @api public
|
|
117
|
+
# @param access_token [String, nil] the access token
|
|
118
|
+
# @return [void]
|
|
119
|
+
# @example Set the access token
|
|
120
|
+
# Tesla.access_token = "eyJhbGciOiJSUzI1NiIs"
|
|
121
|
+
environment_setting :access_token, ENVIRONMENT.fetch(:access_token)
|
|
122
|
+
|
|
123
|
+
# @!method refresh_token
|
|
124
|
+
# The refresh token a new access token is asked for with
|
|
125
|
+
#
|
|
126
|
+
# Falls back to the TESLA_REFRESH_TOKEN environment variable until one is assigned.
|
|
127
|
+
#
|
|
128
|
+
# @api public
|
|
129
|
+
# @return [String, nil] the refresh token
|
|
130
|
+
# @example Get the refresh token
|
|
131
|
+
# Tesla.refresh_token
|
|
132
|
+
# @!method refresh_token=(refresh_token)
|
|
133
|
+
# Set the refresh token a new access token is asked for with
|
|
134
|
+
# @api public
|
|
135
|
+
# @param refresh_token [String, nil] the refresh token
|
|
136
|
+
# @return [void]
|
|
137
|
+
# @example Set the refresh token
|
|
138
|
+
# Tesla.refresh_token = "NA_9d0b4bfd0c4c3a6e"
|
|
139
|
+
environment_setting :refresh_token, ENVIRONMENT.fetch(:refresh_token)
|
|
140
|
+
|
|
141
|
+
# @!method client_id
|
|
142
|
+
# The client ID of the application, as developer.tesla.com issued it
|
|
143
|
+
#
|
|
144
|
+
# Falls back to the TESLA_CLIENT_ID environment variable until one is assigned.
|
|
145
|
+
#
|
|
146
|
+
# @api public
|
|
147
|
+
# @return [String, nil] the client ID
|
|
148
|
+
# @example Get the client ID
|
|
149
|
+
# Tesla.client_id
|
|
150
|
+
# @!method client_id=(client_id)
|
|
151
|
+
# Set the client ID of the application
|
|
152
|
+
# @api public
|
|
153
|
+
# @param client_id [String, nil] the client ID
|
|
154
|
+
# @return [void]
|
|
155
|
+
# @example Set the client ID
|
|
156
|
+
# Tesla.client_id = "81527cff06843c8634fdc09e8ac0abef"
|
|
157
|
+
environment_setting :client_id, ENVIRONMENT.fetch(:client_id)
|
|
158
|
+
|
|
159
|
+
# @!method client_secret
|
|
160
|
+
# The client secret of the application, as developer.tesla.com issued it
|
|
161
|
+
#
|
|
162
|
+
# Falls back to the TESLA_CLIENT_SECRET environment variable until one is assigned.
|
|
163
|
+
#
|
|
164
|
+
# @api public
|
|
165
|
+
# @return [String, nil] the client secret
|
|
166
|
+
# @example Get the client secret
|
|
167
|
+
# Tesla.client_secret
|
|
168
|
+
# @!method client_secret=(client_secret)
|
|
169
|
+
# Set the client secret of the application
|
|
170
|
+
# @api public
|
|
171
|
+
# @param client_secret [String, nil] the client secret
|
|
172
|
+
# @return [void]
|
|
173
|
+
# @example Set the client secret
|
|
174
|
+
# Tesla.client_secret = "ta-secret.7vx6VkVvk2gBgSrN"
|
|
175
|
+
environment_setting :client_secret, ENVIRONMENT.fetch(:client_secret)
|
|
176
|
+
|
|
177
|
+
# @!method redirect_uri
|
|
178
|
+
# The URL the authorization server sends a user back to
|
|
179
|
+
#
|
|
180
|
+
# It is one of the redirect URIs registered for the application at developer.tesla.com.
|
|
181
|
+
#
|
|
182
|
+
# Falls back to the TESLA_REDIRECT_URI environment variable until one is assigned.
|
|
183
|
+
#
|
|
184
|
+
# @api public
|
|
185
|
+
# @return [String, nil] the redirect URI
|
|
186
|
+
# @example Get the redirect URI
|
|
187
|
+
# Tesla.redirect_uri
|
|
188
|
+
# @!method redirect_uri=(redirect_uri)
|
|
189
|
+
# Set the URL the authorization server sends a user back to
|
|
190
|
+
# @api public
|
|
191
|
+
# @param redirect_uri [String, nil] the redirect URI
|
|
192
|
+
# @return [void]
|
|
193
|
+
# @example Set the redirect URI
|
|
194
|
+
# Tesla.redirect_uri = "https://example.com/auth/callback"
|
|
195
|
+
environment_setting :redirect_uri, ENVIRONMENT.fetch(:redirect_uri)
|
|
196
|
+
|
|
197
|
+
# What is called with the token each time the access token is refreshed
|
|
198
|
+
#
|
|
199
|
+
# A refresh token of the Fleet API is used once: refreshing the access token answers with another refresh
|
|
200
|
+
# token, which is the one the next refresh needs. Whatever stores the tokens is told of the new ones here.
|
|
201
|
+
#
|
|
202
|
+
# @api public
|
|
203
|
+
# @return [#call, nil] what is called with the `SimpleOAuth::OAuth2::Token`, or nil to call nothing
|
|
204
|
+
# @example Store the tokens each time they are refreshed
|
|
205
|
+
# Tesla.on_token_refresh = ->(token) { File.write("refresh_token", token.refresh_token) }
|
|
206
|
+
attr_accessor :on_token_refresh
|
|
207
|
+
|
|
208
|
+
# The 'User-Agent' HTTP header sent with requests
|
|
209
|
+
# @api public
|
|
210
|
+
# @return [String] the user agent
|
|
211
|
+
# @example Get or set the user agent
|
|
212
|
+
# Tesla.user_agent = "Custom User Agent"
|
|
213
|
+
attr_accessor :user_agent
|
|
214
|
+
|
|
215
|
+
# @!method open_timeout
|
|
216
|
+
# The timeout for opening connections in seconds
|
|
217
|
+
# @api public
|
|
218
|
+
# @return [Numeric] the timeout for opening connections in seconds
|
|
219
|
+
# @example Get the open timeout
|
|
220
|
+
# Tesla.open_timeout
|
|
221
|
+
# @!method open_timeout=(open_timeout)
|
|
222
|
+
# Set the timeout for opening connections in seconds
|
|
223
|
+
# @api public
|
|
224
|
+
# @param open_timeout [Numeric] the timeout for opening connections in seconds
|
|
225
|
+
# @return [void]
|
|
226
|
+
# @raise [ArgumentError] if it is not a number of seconds, in which case the timeout is left as it was
|
|
227
|
+
# @example Set the open timeout
|
|
228
|
+
# Tesla.open_timeout = 30
|
|
229
|
+
seconds_setting :open_timeout
|
|
230
|
+
|
|
231
|
+
# @!method read_timeout
|
|
232
|
+
# The timeout for reading responses in seconds
|
|
233
|
+
# @api public
|
|
234
|
+
# @return [Numeric] the timeout for reading responses in seconds
|
|
235
|
+
# @example Get the read timeout
|
|
236
|
+
# Tesla.read_timeout
|
|
237
|
+
# @!method read_timeout=(read_timeout)
|
|
238
|
+
# Set the timeout for reading responses in seconds
|
|
239
|
+
# @api public
|
|
240
|
+
# @param read_timeout [Numeric] the timeout for reading responses in seconds
|
|
241
|
+
# @return [void]
|
|
242
|
+
# @raise [ArgumentError] if it is not a number of seconds, in which case the timeout is left as it was
|
|
243
|
+
# @example Set the read timeout
|
|
244
|
+
# Tesla.read_timeout = 30
|
|
245
|
+
seconds_setting :read_timeout
|
|
246
|
+
|
|
247
|
+
# @!method write_timeout
|
|
248
|
+
# The timeout for writing requests in seconds
|
|
249
|
+
# @api public
|
|
250
|
+
# @return [Numeric] the timeout for writing requests in seconds
|
|
251
|
+
# @example Get the write timeout
|
|
252
|
+
# Tesla.write_timeout
|
|
253
|
+
# @!method write_timeout=(write_timeout)
|
|
254
|
+
# Set the timeout for writing requests in seconds
|
|
255
|
+
# @api public
|
|
256
|
+
# @param write_timeout [Numeric] the timeout for writing requests in seconds
|
|
257
|
+
# @return [void]
|
|
258
|
+
# @raise [ArgumentError] if it is not a number of seconds, in which case the timeout is left as it was
|
|
259
|
+
# @example Set the write timeout
|
|
260
|
+
# Tesla.write_timeout = 30
|
|
261
|
+
seconds_setting :write_timeout
|
|
262
|
+
|
|
263
|
+
# @!method keep_alive_timeout
|
|
264
|
+
# The seconds an idle connection is kept open for another request
|
|
265
|
+
#
|
|
266
|
+
# Zero closes every connection when its request is done.
|
|
267
|
+
#
|
|
268
|
+
# @api public
|
|
269
|
+
# @return [Numeric] the seconds an idle connection is kept open
|
|
270
|
+
# @example Get the keep-alive timeout
|
|
271
|
+
# Tesla.keep_alive_timeout
|
|
272
|
+
# @!method keep_alive_timeout=(keep_alive_timeout)
|
|
273
|
+
# Set the seconds an idle connection is kept open for another request
|
|
274
|
+
# @api public
|
|
275
|
+
# @param keep_alive_timeout [Numeric] the seconds an idle connection is kept open
|
|
276
|
+
# @return [void]
|
|
277
|
+
# @raise [ArgumentError] if it is not a number of seconds, in which case the timeout is left as it was
|
|
278
|
+
# @example Set the keep-alive timeout
|
|
279
|
+
# Tesla.keep_alive_timeout = 0
|
|
280
|
+
seconds_setting :keep_alive_timeout
|
|
281
|
+
|
|
282
|
+
# The IO object for debug output
|
|
283
|
+
#
|
|
284
|
+
# The access token, the tokens and the client secret of a request to the token endpoint, and the PIN of a
|
|
285
|
+
# command are redacted from what is written to it.
|
|
286
|
+
#
|
|
287
|
+
# @api public
|
|
288
|
+
# @return [IO, nil] the IO object for debug output
|
|
289
|
+
# @example Get or set the debug output
|
|
290
|
+
# Tesla.debug_output = $stderr
|
|
291
|
+
attr_accessor :debug_output
|
|
292
|
+
|
|
293
|
+
# The proxy URL for requests
|
|
294
|
+
#
|
|
295
|
+
# When nil, proxies are read from the http_proxy, https_proxy, and no_proxy environment variables.
|
|
296
|
+
#
|
|
297
|
+
# @api public
|
|
298
|
+
# @return [String, nil] the proxy URL for requests
|
|
299
|
+
# @example Get the proxy URL
|
|
300
|
+
# Tesla.proxy_url
|
|
301
|
+
attr_reader :proxy_url
|
|
302
|
+
|
|
303
|
+
# Set the proxy URL for requests
|
|
304
|
+
#
|
|
305
|
+
# The proxy URL is checked here rather than when a request is made with it, as {Connection#proxy_url=} checks
|
|
306
|
+
# the proxy URL of a connection, so that the error names the assignment that was wrong.
|
|
307
|
+
#
|
|
308
|
+
# @api public
|
|
309
|
+
# @param proxy_url [String, nil] the proxy URL, or nil to read proxies from the environment
|
|
310
|
+
# @return [void]
|
|
311
|
+
# @raise [ArgumentError] if the proxy URL is invalid, in which case the proxy is left as it was; the message
|
|
312
|
+
# leaves out its user and password
|
|
313
|
+
# @example Set the proxy URL
|
|
314
|
+
# Tesla.proxy_url = "http://proxy.example.com:8080"
|
|
315
|
+
def proxy_url=(proxy_url)
|
|
316
|
+
parse_proxy_uri(proxy_url) unless proxy_url.nil?
|
|
317
|
+
@proxy_url = proxy_url
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
# @!method max_redirects
|
|
321
|
+
# The maximum number of redirects to follow
|
|
322
|
+
#
|
|
323
|
+
# A redirect to another host is followed without the access token, and one that would send the body of a
|
|
324
|
+
# request to another host is not followed at all.
|
|
325
|
+
#
|
|
326
|
+
# @api public
|
|
327
|
+
# @return [Integer] the maximum number of redirects to follow
|
|
328
|
+
# @example Get the maximum redirects
|
|
329
|
+
# Tesla.max_redirects
|
|
330
|
+
# @!method max_redirects=(max_redirects)
|
|
331
|
+
# Set the maximum number of redirects to follow
|
|
332
|
+
# @api public
|
|
333
|
+
# @param max_redirects [Integer] the maximum number of redirects to follow
|
|
334
|
+
# @return [void]
|
|
335
|
+
# @raise [ArgumentError] if it is not a whole number of times, in which case the maximum is left as it was
|
|
336
|
+
# @example Set the maximum redirects
|
|
337
|
+
# Tesla.max_redirects = 5
|
|
338
|
+
count_setting :max_redirects
|
|
339
|
+
|
|
340
|
+
# @!method max_retries
|
|
341
|
+
# The number of times a request that was turned away is sent again
|
|
342
|
+
#
|
|
343
|
+
# A request answered with a 429 Too Many Requests is sent again, and one that only asks for something is sent
|
|
344
|
+
# again for a 502, 503, or 504 and when the network loses it. A command is not, since the vehicle may have
|
|
345
|
+
# carried it out. Zero turns retrying off.
|
|
346
|
+
#
|
|
347
|
+
# @api public
|
|
348
|
+
# @return [Integer] the number of times a request is sent again
|
|
349
|
+
# @example Get the maximum retries
|
|
350
|
+
# Tesla.max_retries
|
|
351
|
+
# @!method max_retries=(max_retries)
|
|
352
|
+
# Set the number of times a request that was turned away is sent again
|
|
353
|
+
# @api public
|
|
354
|
+
# @param max_retries [Integer] the number of times a request is sent again
|
|
355
|
+
# @return [void]
|
|
356
|
+
# @raise [ArgumentError] if it is not a whole number of times, in which case the maximum is left as it was
|
|
357
|
+
# @example Turn retrying off
|
|
358
|
+
# Tesla.max_retries = 0
|
|
359
|
+
count_setting :max_retries
|
|
360
|
+
|
|
361
|
+
# @!method max_retry_delay
|
|
362
|
+
# The longest a request waits before it is sent again, in seconds
|
|
363
|
+
#
|
|
364
|
+
# A response that asks to wait longer raises rather than pausing the caller's thread for that long.
|
|
365
|
+
#
|
|
366
|
+
# @api public
|
|
367
|
+
# @return [Numeric] the longest a request waits before it is sent again, in seconds
|
|
368
|
+
# @example Get the maximum retry delay
|
|
369
|
+
# Tesla.max_retry_delay
|
|
370
|
+
# @!method max_retry_delay=(max_retry_delay)
|
|
371
|
+
# Set the longest a request waits before it is sent again, in seconds
|
|
372
|
+
# @api public
|
|
373
|
+
# @param max_retry_delay [Numeric] the longest a request waits before it is sent again, in seconds
|
|
374
|
+
# @return [void]
|
|
375
|
+
# @raise [ArgumentError] if it is not a number of seconds, in which case the maximum is left as it was
|
|
376
|
+
# @example Set the maximum retry delay
|
|
377
|
+
# Tesla.max_retry_delay = 30
|
|
378
|
+
seconds_setting :max_retry_delay
|
|
379
|
+
|
|
380
|
+
# @!method ca_file
|
|
381
|
+
# The path of a file of certificates TLS is verified with
|
|
382
|
+
#
|
|
383
|
+
# The certificates in the file are trusted alongside the ones OpenSSL already trusts, which is how the
|
|
384
|
+
# certificate the vehicle command proxy was started with is trusted.
|
|
385
|
+
#
|
|
386
|
+
# @api public
|
|
387
|
+
# @return [String, nil] the path of the file, or nil to verify with the certificates OpenSSL trusts
|
|
388
|
+
# @example Get the CA file
|
|
389
|
+
# Tesla.ca_file
|
|
390
|
+
# @!method ca_file=(ca_file)
|
|
391
|
+
# Set the path of a file of certificates TLS is verified with
|
|
392
|
+
# @api public
|
|
393
|
+
# @param ca_file [String, nil] the path of the file, or nil to verify with the certificates OpenSSL trusts
|
|
394
|
+
# @return [void]
|
|
395
|
+
# @raise [ArgumentError] if the path does not name a file, in which case the path is left as it was
|
|
396
|
+
# @example Trust the certificate of the vehicle command proxy
|
|
397
|
+
# Tesla.ca_file = "config/tls-cert.pem"
|
|
398
|
+
|
|
399
|
+
# Set the default configuration when the module is extended
|
|
400
|
+
#
|
|
401
|
+
# @api private
|
|
402
|
+
# @param base [Module] the module extending Configuration
|
|
403
|
+
# @return [void]
|
|
404
|
+
def self.extended(base)
|
|
405
|
+
base.reset
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
# The host used when none has been assigned
|
|
409
|
+
#
|
|
410
|
+
# @api public
|
|
411
|
+
# @return [String] the TESLA_HOST environment variable, or the host of the Fleet API for North America
|
|
412
|
+
# @example Get the default host
|
|
413
|
+
# Tesla.default_host
|
|
414
|
+
def default_host
|
|
415
|
+
ENV.fetch("TESLA_HOST", DEFAULT_HOST)
|
|
416
|
+
end
|
|
417
|
+
|
|
418
|
+
# Convenience method to allow configuration options to be set in a block
|
|
419
|
+
#
|
|
420
|
+
# @api public
|
|
421
|
+
# @yield [self] the configuration
|
|
422
|
+
# @return [self]
|
|
423
|
+
# @example Configure the credentials of the application
|
|
424
|
+
# Tesla.configure do |config|
|
|
425
|
+
# config.client_id = "81527cff06843c8634fdc09e8ac0abef"
|
|
426
|
+
# config.client_secret = "ta-secret.7vx6VkVvk2gBgSrN"
|
|
427
|
+
# end
|
|
428
|
+
def configure
|
|
429
|
+
yield self
|
|
430
|
+
self
|
|
431
|
+
end
|
|
432
|
+
|
|
433
|
+
# Reset all configuration options to defaults
|
|
434
|
+
#
|
|
435
|
+
# The default host is taken as it is rather than checked the way a host assigned to {#host=} is, so that a
|
|
436
|
+
# `TESLA_HOST` that is not a URL is reported when a client is built with it rather than when the library is
|
|
437
|
+
# required.
|
|
438
|
+
#
|
|
439
|
+
# @api public
|
|
440
|
+
# @return [self]
|
|
441
|
+
# @example Reset the configuration
|
|
442
|
+
# Tesla.reset
|
|
443
|
+
def reset
|
|
444
|
+
@host = default_host
|
|
445
|
+
self.user_agent = DEFAULT_USER_AGENT
|
|
446
|
+
reset_credentials
|
|
447
|
+
reset_connection
|
|
448
|
+
self
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
private
|
|
452
|
+
|
|
453
|
+
# Clear all credentials, so that each falls back to the environment again
|
|
454
|
+
# @api private
|
|
455
|
+
# @return [void]
|
|
456
|
+
def reset_credentials
|
|
457
|
+
ENVIRONMENT.each_key do |setting|
|
|
458
|
+
variable = :"@#{setting}"
|
|
459
|
+
remove_instance_variable(variable) if instance_variable_defined?(variable)
|
|
460
|
+
end
|
|
461
|
+
self.audience = nil
|
|
462
|
+
self.authorization_endpoint = DEFAULT_AUTHORIZATION_ENDPOINT
|
|
463
|
+
self.token_endpoint = DEFAULT_TOKEN_ENDPOINT
|
|
464
|
+
self.on_token_refresh = nil
|
|
465
|
+
end
|
|
466
|
+
|
|
467
|
+
# Reset the connection, redirect, and retry options to their defaults
|
|
468
|
+
# @api private
|
|
469
|
+
# @return [void]
|
|
470
|
+
def reset_connection
|
|
471
|
+
self.open_timeout = Connection::DEFAULT_OPEN_TIMEOUT
|
|
472
|
+
self.read_timeout = Connection::DEFAULT_READ_TIMEOUT
|
|
473
|
+
self.write_timeout = Connection::DEFAULT_WRITE_TIMEOUT
|
|
474
|
+
self.keep_alive_timeout = Connection::DEFAULT_KEEP_ALIVE_TIMEOUT
|
|
475
|
+
self.debug_output = nil
|
|
476
|
+
self.proxy_url = nil
|
|
477
|
+
self.ca_file = nil
|
|
478
|
+
self.max_redirects = RedirectHandler::DEFAULT_MAX_REDIRECTS
|
|
479
|
+
self.max_retries = RetryHandler::DEFAULT_MAX_RETRIES
|
|
480
|
+
self.max_retry_delay = RetryHandler::DEFAULT_MAX_RETRY_DELAY
|
|
481
|
+
end
|
|
482
|
+
end
|
|
483
|
+
end
|