diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 415e0b3..9281f12 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -2,7 +2,8 @@ "name": "multi-downloader-client", "image": "rg.fr-par.scw.cloud/kubernetes/devcontainer:php-8.3", "containerEnv": { - "MULTI_DOWNLOADER_API_KEY": "YOUR_API_KEY" + "MULTI_DOWNLOADER_API_KEY": "YOUR_API_KEY", + "MULTI_DOWNLOADER_URL": "https://multi-dl.kub.nwb.fr" }, "customizations": { "vscode": { diff --git a/README.md b/README.md index bb0b8a3..f4c6ed6 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,8 @@ # multi-downloader-client -## Utilisation - ## Installation -### Composer -Ajouter les modules communs dans composer.json + +Add repository in composer.json ```json { "repositories": [{ @@ -14,7 +12,53 @@ Ajouter les modules communs dans composer.json ] } ``` -Lancer la commande +Then run composer require ```bash composer require nwb/multi-downloader-client ``` + +## Env +The following env variables are used by default +```dotenv +MULTI_DOWNLOADER_API_KEY +MULTI_DOWNLOADER_URL +``` +They can be overriden in the constructor +```php +use Nwb\MultiDownloaderClient\MultiDownloaderClient; + +$client = new MultiDownloaderClient([ + 'apiKey' => '', + 'url' => '', +]) +``` + +## Usage + +Ajouter des fichiers +```php +use Nwb\MultiDownloaderClient\MultiDownloaderClient; +use Nwb\MultiDownloaderClient\FileRequest; + +$client = new MultiDownloaderClient(); + +$client->addFiles([ + new FileRequest('http://example.com/image.png') + new FileRequest('http://example.com/image1.png') +]); +``` + +Get zip as string +```php +$zip = $client->downloadAsString(); +``` + +Save zip to file (stream) +```php +$client->downloadTo('/tmp/my-zip.zip'); +``` + +Get html form +```php +$form = $client->htmlForm(); +``` diff --git a/src/MultiDownloaderClient.php b/src/MultiDownloaderClient.php index 031537c..e48f0e7 100644 --- a/src/MultiDownloaderClient.php +++ b/src/MultiDownloaderClient.php @@ -4,7 +4,7 @@ namespace Nwb\MultiDownloaderClient; class MultiDownloaderClient { - private string $apiUrl = 'https://multi-dl.kub.nwb.fr'; + private string $url; private string $apiKey; private array $files = []; @@ -19,9 +19,13 @@ class MultiDownloaderClient $this->apiKey = $apiKey; - if (!empty($options['apiUrl'])) { - $this->apiUrl = $options['apiUrl']; + $url = $options['url'] ?? getenv('MULTI_DOWNLOADER_URL'); + + if (!$url) { + throw new \InvalidArgumentException('API URL is required'); } + + $this->url = $url; } public function setFiles(array $files) @@ -82,7 +86,7 @@ class MultiDownloaderClient $ch = curl_init(); $options = array_replace($options, [ - CURLOPT_URL => $this->apiUrl . '/v2/zip', + CURLOPT_URL => $this->url . '/v2/zip', CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($this->buildRequest()), diff --git a/src/htmlForm.php b/src/htmlForm.php index 519e850..66f77b6 100644 --- a/src/htmlForm.php +++ b/src/htmlForm.php @@ -24,7 +24,7 @@ use Nwb\MultiDownloaderClient\MultiDownloaderClient;