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();
$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: <info>{$report}</info>.");
}
}
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 <info>{$report_id}</info> cache cleared, status: {$cache_clear_status}");
}
$file = $this->youtrack->downloadReport($report_id);
}

View File

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

View File

@ -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,21 +94,32 @@ 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",
];
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-cli/{$report_id}_");
$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 {
if (isset($this->ytToken)) {
@ -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,