From c00c6d1dab290706ffba74a329fbef48bcf65f82 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Thu, 11 Jan 2024 13:05:59 +0000 Subject: [PATCH] htmlForm --- src/MultiDownloaderClient.php | 29 ++++++++++++-------- src/htmlForm.php | 41 +++++++++++++++++++++++++++++ tests/MultiDownloaderClientTest.php | 31 +++++++++++++--------- 3 files changed, 78 insertions(+), 23 deletions(-) create mode 100644 src/htmlForm.php diff --git a/src/MultiDownloaderClient.php b/src/MultiDownloaderClient.php index fb4b99e..3fc8996 100644 --- a/src/MultiDownloaderClient.php +++ b/src/MultiDownloaderClient.php @@ -22,22 +22,21 @@ class MultiDownloaderClient } } - public function downloadAsString(array $files) + private function buildRequest(array $files): array { - $files = array_map(function (FileRequest $file) { + return array_map(function (FileRequest $file) { return $file->toArray(); }, $files); - - return $this->sendRequest($files); } - public function downloadTo(string $path, array $files) + public function downloadAsString(array $files): string { - $files = array_map(function (FileRequest $file) { - return $file->toArray(); - }, $files); + return $this->sendRequest($this->buildRequest($files)); + } - $response = $this->sendRequest($files, [ + public function downloadTo(string $path, array $files): string + { + $response = $this->sendRequest($this->buildRequest($files), [ CURLOPT_RETURNTRANSFER => false, CURLOPT_FILE => fopen($path, 'w'), ]); @@ -45,7 +44,14 @@ class MultiDownloaderClient return $response; } - private function sendRequest(array $files, array $options = []) + public function htmlForm(array $files): string + { + ob_start(); + include __DIR__ . '/htmlForm.php'; + return ob_get_clean(); + } + + private function sendRequest(array $request, array $options = []): string { $ch = curl_init(); @@ -53,9 +59,10 @@ class MultiDownloaderClient CURLOPT_URL => $this->apiUrl . '/v2/zip', CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, - CURLOPT_POSTFIELDS => json_encode($files), + CURLOPT_POSTFIELDS => json_encode($request), CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', + 'Authorization: Bearer ' . $this->apiKey, ], ]); diff --git a/src/htmlForm.php b/src/htmlForm.php new file mode 100644 index 0000000..fdb0510 --- /dev/null +++ b/src/htmlForm.php @@ -0,0 +1,41 @@ + + + + + + + Forwarding ... + + + + + + + + + + + + + diff --git a/tests/MultiDownloaderClientTest.php b/tests/MultiDownloaderClientTest.php index 594b08d..07f7f70 100644 --- a/tests/MultiDownloaderClientTest.php +++ b/tests/MultiDownloaderClientTest.php @@ -8,6 +8,18 @@ use PHPUnit\Framework\TestCase; class MultiDownloaderClientTest extends TestCase { + private $testFiles = [ + 'https://s3.eu-central-1.wasabisys.com/dev-data-nwb/multi-downloader-sat/tests/img/watermarks/portrait.png', + 'https://s3.eu-central-1.wasabisys.com/dev-data-nwb/multi-downloader-sat/tests/img/watermarks/paysage.png' + ]; + + private function testFiles() + { + return array_map(function ($url) { + return new FileRequest($url); + }, $this->testFiles); + } + public static function getProperty($object, $property) { $reflectedClass = new \ReflectionClass($object); @@ -21,12 +33,7 @@ class MultiDownloaderClientTest extends TestCase { $client = new MultiDownloaderClient(); - $files = [ - new FileRequest('https://s3.eu-central-1.wasabisys.com/dev-data-nwb/multi-downloader-sat/tests/img/watermarks/portrait.png'), - new FileRequest('https://s3.eu-central-1.wasabisys.com/dev-data-nwb/multi-downloader-sat/tests/img/watermarks/paysage.png') - ]; - - $response = $client->downloadAsString($files); + $response = $client->downloadAsString($this->testFiles()); $md5 = md5($response); @@ -37,14 +44,9 @@ class MultiDownloaderClientTest extends TestCase { $client = new MultiDownloaderClient(); - $files = [ - new FileRequest('https://s3.eu-central-1.wasabisys.com/dev-data-nwb/multi-downloader-sat/tests/img/watermarks/portrait.png'), - new FileRequest('https://s3.eu-central-1.wasabisys.com/dev-data-nwb/multi-downloader-sat/tests/img/watermarks/paysage.png') - ]; - $path = sys_get_temp_dir() . '/testDownloadTo.zip'; - $client->downloadTo($path, $files); + $client->downloadTo($path, $this->testFiles()); $md5 = md5_file($path); @@ -61,6 +63,11 @@ class MultiDownloaderClientTest extends TestCase $this->assertEquals('1234567890', $apiKey); } + public function testForm() + { + echo (new MultiDownloaderClient())->htmlForm($this->testFiles()); + } + public function testApiKeyParam() { $client = new MultiDownloaderClient([