Skip to content

Commit

Permalink
[tuya-connector][shelly-connector] Fixing api calls (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed May 10, 2023
1 parent bb5434a commit 0a7411e
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/API/OpenApi.php
Expand Up @@ -63,7 +63,7 @@ final class OpenApi

use Nette\SmartObject;

private const CONNECTION_TIMEOUT = 1;
private const CONNECTION_TIMEOUT = 30;

private const VERSION = '0.1.0';

Expand Down Expand Up @@ -1599,11 +1599,43 @@ private function refreshAccessToken(string $path): void
self::REFRESH_TOKEN_API_ENDPOINT,
$this->tokenInfo->getRefreshToken(),
),
$this->buildRequestHeaders('get', self::REFRESH_TOKEN_API_ENDPOINT),
$this->buildRequestHeaders(
'get',
sprintf(
self::REFRESH_TOKEN_API_ENDPOINT,
$this->tokenInfo->getRefreshToken(),
),
),
);

assert($response instanceof Message\ResponseInterface);

$body = $response->getBody()->getContents();

try {
$decodedResponse = Utils\Json::decode($body, Utils\Json::FORCE_ARRAY);

} catch (Utils\JsonException) {
throw new Exceptions\OpenApiCall('Received response body is not valid JSON');
}

if (!is_array($decodedResponse)) {
throw new Exceptions\OpenApiCall('Received response body is not valid JSON');
}

$data = Utils\ArrayHash::from($decodedResponse);

if (
$data->offsetExists('success')
&& boolval($data->offsetGet('success')) !== true
) {
if ($data->offsetExists('msg')) {
throw new Exceptions\OpenApiCall(strval($data->offsetGet('msg')));
}

throw new Exceptions\OpenApiCall('Received response is not success');
}

try {
$parsedMessage = $this->schemaValidator->validate(
$response->getBody()->getContents(),
Expand Down

0 comments on commit 0a7411e

Please sign in to comment.