diff --git a/app/src/Commands/InvoiceCommand.php b/app/src/Commands/InvoiceCommand.php index 91c7595..bf16dcf 100644 --- a/app/src/Commands/InvoiceCommand.php +++ b/app/src/Commands/InvoiceCommand.php @@ -146,7 +146,7 @@ class InvoiceCommand extends Command $list = $this->youtrack->listReports(); $output->writeln(var_export($list, TRUE)); } - if ($input->hasParameterOption('--report') || $input->hasParameterOption('-t')) { + if ($input->hasParameterOption('--report') || $input->hasParameterOption('-r')) { if ($report = $input->getOption('report')) { $this->youtrack->setReportId($report); } @@ -161,12 +161,15 @@ class InvoiceCommand extends Command // Asume people are literate. $this->youtrack->setReportId($report); } + if ($output->isVerbose()) { + $output->writeln("Setting report: {$report}."); + } } if ($youtrack = $input->getOption('youtrack')) { $report_id = $this->youtrack->getReportId(); $cache_clear_status = $this->youtrack->clearReportCache($report_id); if ($output->isVerbose()) { - $output->writeln("Report cache cleared, status: {$cache_clear_status}"); + $output->writeln("Report {$report_id} cache cleared, status: {$cache_clear_status}"); } $file = $this->youtrack->downloadReport($report_id); } diff --git a/app/src/Commands/ReportCommand.php b/app/src/Commands/ReportCommand.php index 2e8cf3d..4619b24 100644 --- a/app/src/Commands/ReportCommand.php +++ b/app/src/Commands/ReportCommand.php @@ -84,7 +84,7 @@ class ReportCommand extends Command { $report_id = $this->trackingService->getReportId(); $report_name = $reports[$report_id]; // Code duplication. - $cache_clear_status = $this->timeTrackingService->clearReportCache($report_id); + $cache_clear_status = $this->trackingService->clearReportCache($report_id); if ($output->isVerbose()) { $output->writeln("Report cache cleared, status: {$cache_clear_status}"); } diff --git a/app/src/Utils/TimeTrackingServices/YoutrackService.php b/app/src/Utils/TimeTrackingServices/YoutrackService.php index df184ac..1b22fde 100644 --- a/app/src/Utils/TimeTrackingServices/YoutrackService.php +++ b/app/src/Utils/TimeTrackingServices/YoutrackService.php @@ -7,6 +7,7 @@ declare(strict_types=1); namespace RprtCli\Utils\TimeTrackingServices; use GuzzleHttp\ClientInterface; +use GuzzleHttp\Exception\ClientException; use RprtCli\Utils\Configuration\ConfigurationInterface; class YoutrackService implements YoutrackInterface @@ -93,20 +94,31 @@ class YoutrackService implements YoutrackInterface $query = ['$top' => -1]; $yt_token = $this->getYtToken(); $headers = [ - 'Accept' => 'text/plain, */*', + 'Accept' => 'application/json, text/plain, */*', + // 'Accept-Encoding' => 'gzip, deflate, br', + // 'Connection' => 'keep-alive', 'Accept-Language' => 'en-US,en;q=0.5', "Authorization" => "Bearer $yt_token", ]; - - $csv_response = $this->httpClient->request('GET', $this->getYtUrl($path), [ - 'headers' => $headers, - 'query' => $query, - ]); - - // Write csv response test into temporary file. - $csv_file = tempnam(sys_get_temp_dir(), "rprt-cli/{$report_id}_"); - file_put_contents($csv_file, $csv_response->getBody()); - return $csv_file; + try { + $csv_response = $this->httpClient->request('GET', $this->getYtUrl($path), [ + 'headers' => $headers, + 'query' => $query, + ]); + // Write csv response test into temporary file. + $csv_file = tempnam(sys_get_temp_dir(), "rprt-csv-{$report_id}"); + file_put_contents($csv_file, $csv_response->getBody()); + return $csv_file; + } + catch (ClientException $e) { + $status = $e->getResponse()->getStatusCode(); + if ($status == 409) { + sleep(3); + // @TODO Find a way to break of of loop if necessary! + $this->downloadReport($report_id); + } + } + throw new \Exception("Unable to download report {$report_id}!"); } protected function getYtToken(): string { @@ -162,7 +174,7 @@ class YoutrackService implements YoutrackInterface return $reports; } - public function clearReportCache(string $report_id) :void { + public function clearReportCache(string $report_id) :int { $path = "/youtrack/api/reports/${report_id}/status"; $query = [ '$top' => -1,