diff --git a/src/FileRequest.php b/src/FileRequest.php new file mode 100644 index 0000000..896d3c8 --- /dev/null +++ b/src/FileRequest.php @@ -0,0 +1,47 @@ +url = $url; + $this->fallbackUrl = $fallbackUrl; + } + + public function url(string $url) + { + $this->url = $url; + + return $this; + } + + public function fallbackUrl(string $fallbackUrl) + { + $this->fallbackUrl = $fallbackUrl; + + return $this; + } + + public function getUrl(): string + { + return $this->url; + } + + public function getFallbackUrl(): ?string + { + return $this->fallbackUrl; + } + + public function toArray(): array + { + return [ + 'url' => $this->url, + 'fallback_url' => $this->fallbackUrl, + ]; + } +} diff --git a/src/MultiDownloaderClient.php b/src/MultiDownloaderClient.php index d4e1d20..b8ad78f 100644 --- a/src/MultiDownloaderClient.php +++ b/src/MultiDownloaderClient.php @@ -4,6 +4,7 @@ namespace Nwb\MultiDownloaderClient; class MultiDownloaderClient { + private string $apiUrl = 'https://multi-dl.kub.nwb.fr'; private string $apiKey; public function __construct(string $apiKey = null) @@ -16,4 +17,40 @@ class MultiDownloaderClient $this->apiKey = $apiKey; } + + public function download(array $files) + { + $files = array_map(function (FileRequest $file) { + return $file->toArray(); + }, $files); + + return $this->sendRequest($files); + } + + private function sendRequest(array $files) + { + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $this->apiUrl . '/v2/zip'); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POST, true); + + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($files)); + + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/json', + ]); + + $response = curl_exec($ch); + + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + if ($httpCode !== 200) { + throw new MultiDownloaderException('Error while requesting API: ' . $response); + } + + curl_close($ch); + + return $response; + } } diff --git a/src/MultiDownloaderException.php b/src/MultiDownloaderException.php new file mode 100644 index 0000000..30a3e76 --- /dev/null +++ b/src/MultiDownloaderException.php @@ -0,0 +1,7 @@ +getValue($object); } + public function testRequest() + { + $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->download($files); + + var_dump($response); + } + public function testApiKeyEnv() { putenv('MULTI_DOWNLOADER_API_KEY=1234567890');