Skip to content

Commit

Permalink
more debug
Browse files Browse the repository at this point in the history
  • Loading branch information
zajca committed Feb 4, 2025
1 parent cc7272a commit ab68e9c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/Keboola/StorageApi/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class Client
public $token;

// current run id sent with all request

/** @var array<mixed>|null */
private array|null $debug;

private $runId = null;

// configuration will be send with all requests
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -2988,6 +2997,7 @@ protected function request($method, $url, $options = [], $responseFileName = nul
}

try {
$this->log('[' . $method . '] ' . $url, $requestOptions);
/**
* @var ResponseInterface $response
*/
Expand Down Expand Up @@ -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'];
}
Expand Down Expand Up @@ -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);
}

Expand Down
36 changes: 29 additions & 7 deletions tests/ClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class ClientTestCase extends TestCase
{
use \PHPUnitRetry\RetryTrait;
// use \PHPUnitRetry\RetryTrait;

public function getLogger(): ConsoleLogger
{
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 <projectId>-<tokenId>-<hash>
$debug['project'] = $tokenParts[0];
$debug['token'] = $tokenParts[1];
}
$debug['url'] = $url;
$debug['test'] = $this->getTestName();
return $debug;
}

/**
* @param Client $client
* @return string
Expand Down

0 comments on commit ab68e9c

Please sign in to comment.