diff --git a/src/MultiDownloaderClient.php b/src/MultiDownloaderClient.php index 3fc8996..031537c 100644 --- a/src/MultiDownloaderClient.php +++ b/src/MultiDownloaderClient.php @@ -7,6 +7,8 @@ class MultiDownloaderClient private string $apiUrl = 'https://multi-dl.kub.nwb.fr'; private string $apiKey; + private array $files = []; + public function __construct(array $options = []) { $apiKey = $options['apiKey'] ?? getenv('MULTI_DOWNLOADER_API_KEY'); @@ -22,21 +24,45 @@ class MultiDownloaderClient } } - private function buildRequest(array $files): array + public function setFiles(array $files) + { + $this->files = []; + $this->addFiles($files); + + return $this; + } + + public function addFile(FileRequest $file) + { + $this->files[] = $file; + + return $this; + } + + public function addFiles(array $files) + { + foreach ($files as $file) { + $this->addFile($file); + } + + return $this; + } + + private function buildRequest(): array { return array_map(function (FileRequest $file) { return $file->toArray(); - }, $files); + }, $this->files); } - public function downloadAsString(array $files): string + public function downloadAsString(): string { - return $this->sendRequest($this->buildRequest($files)); + return $this->sendRequest(); } - public function downloadTo(string $path, array $files): string + public function downloadTo(string $path): string { - $response = $this->sendRequest($this->buildRequest($files), [ + $response = $this->sendRequest([ CURLOPT_RETURNTRANSFER => false, CURLOPT_FILE => fopen($path, 'w'), ]); @@ -44,14 +70,14 @@ class MultiDownloaderClient return $response; } - public function htmlForm(array $files): string + public function htmlForm(): string { ob_start(); include __DIR__ . '/htmlForm.php'; return ob_get_clean(); } - private function sendRequest(array $request, array $options = []): string + private function sendRequest(array $options = []): string { $ch = curl_init(); @@ -59,7 +85,7 @@ class MultiDownloaderClient CURLOPT_URL => $this->apiUrl . '/v2/zip', CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, - CURLOPT_POSTFIELDS => json_encode($request), + CURLOPT_POSTFIELDS => json_encode($this->buildRequest()), CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'Authorization: Bearer ' . $this->apiKey, diff --git a/src/htmlForm.php b/src/htmlForm.php index fdb0510..519e850 100644 --- a/src/htmlForm.php +++ b/src/htmlForm.php @@ -25,7 +25,7 @@ use Nwb\MultiDownloaderClient\MultiDownloaderClient;