Add csv download error handling. fix bugs

master
Lio Novelli 2022-05-15 12:37:21 +02:00
parent f498315b53
commit 25d8092836
3 changed files with 30 additions and 15 deletions

View File

@ -146,7 +146,7 @@ class InvoiceCommand extends Command
$list = $this->youtrack->listReports(); $list = $this->youtrack->listReports();
$output->writeln(var_export($list, TRUE)); $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')) { if ($report = $input->getOption('report')) {
$this->youtrack->setReportId($report); $this->youtrack->setReportId($report);
} }
@ -161,12 +161,15 @@ class InvoiceCommand extends Command
// Asume people are literate. // Asume people are literate.
$this->youtrack->setReportId($report); $this->youtrack->setReportId($report);
} }
if ($output->isVerbose()) {
$output->writeln("Setting report: <info>{$report}</info>.");
}
} }
if ($youtrack = $input->getOption('youtrack')) { if ($youtrack = $input->getOption('youtrack')) {
$report_id = $this->youtrack->getReportId(); $report_id = $this->youtrack->getReportId();
$cache_clear_status = $this->youtrack->clearReportCache($report_id); $cache_clear_status = $this->youtrack->clearReportCache($report_id);
if ($output->isVerbose()) { if ($output->isVerbose()) {
$output->writeln("Report cache cleared, status: {$cache_clear_status}"); $output->writeln("Report <info>{$report_id}</info> cache cleared, status: {$cache_clear_status}");
} }
$file = $this->youtrack->downloadReport($report_id); $file = $this->youtrack->downloadReport($report_id);
} }

View File

@ -84,7 +84,7 @@ class ReportCommand extends Command {
$report_id = $this->trackingService->getReportId(); $report_id = $this->trackingService->getReportId();
$report_name = $reports[$report_id]; $report_name = $reports[$report_id];
// Code duplication. // Code duplication.
$cache_clear_status = $this->timeTrackingService->clearReportCache($report_id); $cache_clear_status = $this->trackingService->clearReportCache($report_id);
if ($output->isVerbose()) { if ($output->isVerbose()) {
$output->writeln("Report cache cleared, status: {$cache_clear_status}"); $output->writeln("Report cache cleared, status: {$cache_clear_status}");
} }

View File

@ -7,6 +7,7 @@ declare(strict_types=1);
namespace RprtCli\Utils\TimeTrackingServices; namespace RprtCli\Utils\TimeTrackingServices;
use GuzzleHttp\ClientInterface; use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use RprtCli\Utils\Configuration\ConfigurationInterface; use RprtCli\Utils\Configuration\ConfigurationInterface;
class YoutrackService implements YoutrackInterface class YoutrackService implements YoutrackInterface
@ -93,21 +94,32 @@ class YoutrackService implements YoutrackInterface
$query = ['$top' => -1]; $query = ['$top' => -1];
$yt_token = $this->getYtToken(); $yt_token = $this->getYtToken();
$headers = [ $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', 'Accept-Language' => 'en-US,en;q=0.5',
"Authorization" => "Bearer $yt_token", "Authorization" => "Bearer $yt_token",
]; ];
try {
$csv_response = $this->httpClient->request('GET', $this->getYtUrl($path), [ $csv_response = $this->httpClient->request('GET', $this->getYtUrl($path), [
'headers' => $headers, 'headers' => $headers,
'query' => $query, 'query' => $query,
]); ]);
// Write csv response test into temporary file. // Write csv response test into temporary file.
$csv_file = tempnam(sys_get_temp_dir(), "rprt-cli/{$report_id}_"); $csv_file = tempnam(sys_get_temp_dir(), "rprt-csv-{$report_id}");
file_put_contents($csv_file, $csv_response->getBody()); file_put_contents($csv_file, $csv_response->getBody());
return $csv_file; 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 { protected function getYtToken(): string {
if (isset($this->ytToken)) { if (isset($this->ytToken)) {
@ -162,7 +174,7 @@ class YoutrackService implements YoutrackInterface
return $reports; return $reports;
} }
public function clearReportCache(string $report_id) :void { public function clearReportCache(string $report_id) :int {
$path = "/youtrack/api/reports/${report_id}/status"; $path = "/youtrack/api/reports/${report_id}/status";
$query = [ $query = [
'$top' => -1, '$top' => -1,