1
0
mirror of https://github.com/beestat/app.git synced 2025-06-23 07:20:42 -04:00

Fixed ecobee tokens not properly persisting across multiple requests in the same API call

This commit is contained in:
Jon Ziebell 2023-08-25 08:06:17 -04:00
parent ca8ff3d4b4
commit 9b6d8e91b0

View File

@ -21,6 +21,17 @@ class ecobee extends external_api {
protected static $cache = false; protected static $cache = false;
protected static $cache_for = null; protected static $cache_for = null;
/**
* If the original API call fails, the ecobee token is updated outside of
* the current transaction to ensure it doesn't get rolled back due to an
* exception.
*
* The problem with that is that all subsequent API calls need this
* value...so it's stored here statically on the class and used instead of
* the old database value.
*/
protected static $ecobee_token = null;
/** /**
* Redirect to ecobee to do the oAuth. * Redirect to ecobee to do the oAuth.
*/ */
@ -120,11 +131,10 @@ class ecobee extends external_api {
* @param array $arguments POST or GET parameters * @param array $arguments POST or GET parameters
* @param boolean $auto_refresh_token Whether or not to automatically get a * @param boolean $auto_refresh_token Whether or not to automatically get a
* new token if the old one is expired. * new token if the old one is expired.
* @param string $ecobee_token Force-use a specific token.
* *
* @return array The response of this API call. * @return array The response of this API call.
*/ */
public function ecobee_api($method, $endpoint, $arguments, $auto_refresh_token = true, $ecobee_token = null) { public function ecobee_api($method, $endpoint, $arguments, $auto_refresh_token = true) {
$curl = [ $curl = [
'method' => $method 'method' => $method
]; ];
@ -141,7 +151,7 @@ class ecobee extends external_api {
// For non-authorization endpoints, add the access_token header. Will use // For non-authorization endpoints, add the access_token header. Will use
// provided token if set, otherwise will get the one for the logged in // provided token if set, otherwise will get the one for the logged in
// user. // user.
if($ecobee_token === null) { if(self::$ecobee_token === null) {
$ecobee_tokens = $this->api( $ecobee_tokens = $this->api(
'ecobee_token', 'ecobee_token',
'read', 'read',
@ -152,6 +162,8 @@ class ecobee extends external_api {
throw new cora\exception('No ecobee access for this user.', 10501, false); throw new cora\exception('No ecobee access for this user.', 10501, false);
} }
$ecobee_token = $ecobee_tokens[0]; $ecobee_token = $ecobee_tokens[0];
} else {
$ecobee_token = self::$ecobee_token;
} }
$curl['header'] = [ $curl['header'] = [
@ -200,8 +212,8 @@ class ecobee extends external_api {
if (isset($response['status']) === true && $response['status']['code'] === 14) { if (isset($response['status']) === true && $response['status']['code'] === 14) {
// Authentication token has expired. Refresh your tokens. // Authentication token has expired. Refresh your tokens.
if ($auto_refresh_token === true) { if ($auto_refresh_token === true) {
$ecobee_token = $this->api('ecobee_token', 'refresh'); self::$ecobee_token = $this->api('ecobee_token', 'refresh');
return $this->ecobee_api($method, $endpoint, $arguments, false, $ecobee_token); return $this->ecobee_api($method, $endpoint, $arguments, false);
} }
else { else {
if($this::$log_mysql !== 'all') { if($this::$log_mysql !== 'all') {