diff --git a/src/Keboola/StorageApi/Client.php b/src/Keboola/StorageApi/Client.php index 74f6d4594..be06fd244 100644 --- a/src/Keboola/StorageApi/Client.php +++ b/src/Keboola/StorageApi/Client.php @@ -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', @@ -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)); } diff --git a/tests/Common/GetToFileTest.php b/tests/Common/GetToFileTest.php new file mode 100644 index 000000000..6ddcd7053 --- /dev/null +++ b/tests/Common/GetToFileTest.php @@ -0,0 +1,72 @@ +_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); + } +}