From ab68e9c64fd161766d5f8149ba7113f34f51a315 Mon Sep 17 00:00:00 2001 From: zajca Date: Tue, 4 Feb 2025 21:21:50 +0100 Subject: [PATCH] more debug --- src/Keboola/StorageApi/Client.php | 15 +++++++++++++ tests/ClientTestCase.php | 36 +++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/Keboola/StorageApi/Client.php b/src/Keboola/StorageApi/Client.php index 0102d6714..3d2ad7f15 100644 --- a/src/Keboola/StorageApi/Client.php +++ b/src/Keboola/StorageApi/Client.php @@ -72,6 +72,10 @@ class Client public $token; // current run id sent with all request + + /** @var array|null */ + private array|null $debug; + private $runId = null; // configuration will be send with all requests @@ -157,6 +161,11 @@ public function __construct(array $config = []) $this->userAgent .= ' ' . $config['userAgent']; } + $this->debug = null; + if (isset($config['debug'])) { + $this->debug = $config['debug']; + } + if (!isset($config['token'])) { throw new \InvalidArgumentException('token must be set'); } @@ -2988,6 +2997,7 @@ protected function request($method, $url, $options = [], $responseFileName = nul } try { + $this->log('[' . $method . '] ' . $url, $requestOptions); /** * @var ResponseInterface $response */ @@ -3043,10 +3053,12 @@ private function handleAsyncTask(Response $jobCreatedResponse) { /** @var array{id: int, results: mixed} $job */ $job = json_decode((string) $jobCreatedResponse->getBody(), true); + $this->log('Job ' . $job['id'], $job); $job = $this->waitForJob($job['id']); if ($job === null) { throw new ClientException('StorageJob expected'); } + $this->log('Job ' . $job['id'] . 'result', $job); $this->handleJobError($job); return $job['results']; } @@ -3121,6 +3133,9 @@ public function handleAsyncTasks($jobIds) */ private function log($message, $context = []) { + if ($this->debug !== null) { + $context['debug'] = $this->debug; + } $this->logger->debug($message, $context); } diff --git a/tests/ClientTestCase.php b/tests/ClientTestCase.php index 807da76bf..a2cc03adc 100644 --- a/tests/ClientTestCase.php +++ b/tests/ClientTestCase.php @@ -12,7 +12,7 @@ class ClientTestCase extends TestCase { - use \PHPUnitRetry\RetryTrait; +// use \PHPUnitRetry\RetryTrait; public function getLogger(): ConsoleLogger { @@ -31,6 +31,7 @@ public function getClient(array $options) if (!array_key_exists('logger', $options)) { $options['logger'] = $this->getLogger(); } + $options['debug'] = $this->buildDebugOption($options['token'], $options['url']); return new Client($options); } @@ -43,6 +44,7 @@ public function getBranchAwareClient($branchId, array $options) $options['token'], $options['url'], ); + $options['debug'] = $this->buildDebugOption($options['token'], $options['url']); return new BranchAwareClient($branchId, $options); } @@ -139,12 +141,7 @@ public function getClientForToken(string $token): Client return $this->getClient($this->getClientOptionsForToken($token)); } - /** - * @param string $token - * @param string $url - * @return string - */ - protected function buildUserAgentString($token, $url) + protected function buildUserAgentString(string $token, string $url): string { $testSuiteName = ''; if (SUITE_NAME) { @@ -176,6 +173,31 @@ protected function buildUserAgentString($token, $url) ); } + /** + * @return array{suite?: string, buildId?: string, project?: string, token?: string, url: string, test: string} + */ + protected function buildDebugOption(string $token, string $url): array + { + $debug = []; + if (SUITE_NAME) { + $debug['suite'] = SUITE_NAME; + } + + if (TRAVIS_BUILD_ID) { + $debug['buildId'] = TRAVIS_BUILD_ID; + } + + $tokenParts = explode('-', $token); + if (count($tokenParts) === 3) { + // token comes in from of -- + $debug['project'] = $tokenParts[0]; + $debug['token'] = $tokenParts[1]; + } + $debug['url'] = $url; + $debug['test'] = $this->getTestName(); + return $debug; + } + /** * @param Client $client * @return string