Skip to content

Commit

Permalink
Merge pull request #184 from keboola/download-to-file
Browse files Browse the repository at this point in the history
fix - download to file streaming FIXES #183
  • Loading branch information
Halama authored Feb 17, 2018
2 parents fbf7d79 + a6caeb5 commit 2478807
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Keboola/StorageApi/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1908,6 +1908,10 @@ protected function request($method, $url, $options = array(), $responseFileName
'timeout' => $this->getTimeout(),
]);

if ($responseFileName !== null) {
$requestOptions['stream'] = true;
}

$defaultHeaders = [
'X-StorageApi-Token' => $this->token,
'Accept-Encoding' => 'gzip',
Expand Down Expand Up @@ -1956,7 +1960,6 @@ protected function request($method, $url, $options = array(), $responseFileName
throw new ClientException("Cannot open file {$responseFileName}");
}
$body = $response->getBody();
$body->seek(0);
while (!$body->eof()) {
fwrite($responseFile, $body->read(1024 * 10));
}
Expand Down
72 changes: 72 additions & 0 deletions tests/Common/GetToFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Keboola\Test\Common;

use Keboola\Test\StorageApiTestCase;
use Keboola\StorageApi\Options\Components\ListComponentsOptions;
use Keboola\StorageApi\Components;
use Keboola\StorageApi\Options\Components\Configuration;
use Keboola\StorageApi\Options\Components\ConfigurationRow;
use Nette\Utils\Json;

class GetToFileTest extends StorageApiTestCase
{
private $downloadPath;

public function setUp()
{
parent::setUp();
$this->_initEmptyTestBuckets();
$this->downloadPath = __DIR__ . '/../_tmp/downloaded.json';

// cleanup
$components = new Components($this->_client);
foreach ($components->listComponents() as $component) {
foreach ($component['configurations'] as $configuration) {
$components->deleteConfiguration($component['id'], $configuration['id']);
}
}

// erase all deleted configurations
foreach ($components->listComponents((new ListComponentsOptions())->setIsDeleted(true)) as $component) {
foreach ($component['configurations'] as $configuration) {
$components->deleteConfiguration($component['id'], $configuration['id']);
}
}
}

public function testGetToFile()
{
// prepare data
$config = new Configuration();
$config->setComponentId('transformation');
$config->setDescription('Test Configuration');
$config->setConfigurationId('sapi-php-test');
$config->setName('test-configuration');
$component = new Components($this->_client);
$configData = $component->addConfiguration($config);
$config->setConfigurationId($configData['id']);

$largeRowConfiguration = [
'values' => []
];
$valuesCount = 100;
for ($i = 0; $i < $valuesCount; $i++) {
$largeRowConfiguration['values'][] = sha1(random_bytes(128));
}

$configurationRowsCount = 100;
for ($i = 0; $i < $configurationRowsCount; $i++) {
$row = new ConfigurationRow($config);
$row->setChangeDescription('Row 1');
$row->setConfiguration($largeRowConfiguration);
$component->addConfigurationRow($row);
}

// download
$this->_client->apiGet('storage/components?include=configuration,rows,state', $this->downloadPath);

$configurations = \GuzzleHttp\json_decode(file_get_contents($this->downloadPath));
$this->assertCount($configurationRowsCount, $configurations[0]->configurations[0]->rows);
}
}

0 comments on commit 2478807

Please sign in to comment.