testrequest
This commit is contained in:
parent
3d17d2ff64
commit
d719bc283c
47
src/FileRequest.php
Normal file
47
src/FileRequest.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Nwb\MultiDownloaderClient;
|
||||
|
||||
class FileRequest
|
||||
{
|
||||
private string $url;
|
||||
private ?string $fallbackUrl;
|
||||
|
||||
public function __construct(string $url, string $fallbackUrl = null)
|
||||
{
|
||||
$this->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,
|
||||
];
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
7
src/MultiDownloaderException.php
Normal file
7
src/MultiDownloaderException.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Nwb\MultiDownloaderClient;
|
||||
|
||||
class MultiDownloaderException extends \Exception
|
||||
{
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
include __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use Nwb\MultiDownloaderClient\FileRequest;
|
||||
use Nwb\MultiDownloaderClient\MultiDownloaderClient;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
@ -16,6 +17,20 @@ class MultiDownloaderClientTest extends TestCase
|
||||
return $reflection->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');
|
||||
|
Loading…
Reference in New Issue
Block a user