From b45d42cd65253b932930569b72f1a903464bfd0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Sun, 1 Oct 2023 14:24:11 +0200 Subject: [PATCH] downloader JSON request --- src/Downloader.php | 35 ++++++++++++++++++----------------- tools/downloader.php | 2 +- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/Downloader.php b/src/Downloader.php index c9e1e8d..0062c99 100644 --- a/src/Downloader.php +++ b/src/Downloader.php @@ -7,8 +7,6 @@ use JsonSchema\Constraints\Constraint; use JsonSchema\Validator; -require __DIR__ . '/../vendor/autoload.php'; - class Downloader { private array $config; @@ -19,27 +17,26 @@ public function __construct(array $config) } /** - * @return mixed * @throws DownloaderException */ - public function download(string $app, string $source, array $arguments) + public function download(string $app, string $source, array $arguments): array { $config = $this->config[$app]; $this->validateRequest($arguments, $app, $source); $options = [ 'http' => [ - 'header' => "Content-type: application/x-www-form-urlencoded\r\n" . + 'header' => "Content-type: application/json\r\n" . + "Accept: application/json\r\n" . "Authorization: Basic " . base64_encode($config['username'] . ':' . $config['password']), 'method' => 'GET', - 'content' => http_build_query($arguments), + 'content' => json_encode($arguments), ], ]; $context = stream_context_create($options); $result = file_get_contents( - $this->config[$app]['url'] . $source - . '?' . http_build_query($arguments), + $this->config[$app]['url'] . $source, false, $context ); @@ -47,18 +44,24 @@ public function download(string $app, string $source, array $arguments) restore_error_handler(); throw new DownloaderException(error_get_last()['message']); } - $this->validateResponse(json_decode($result), $app, $source); - return $result; + $data = json_decode($result, true); + $this->validateResponse($data, $app, $source); + return $data; } /** * @throws DownloaderException */ - private function validateRequest(array $arguments, string $app, string $source): void + public function validateRequest(array $arguments, string $app, string $source): void { - $this->validate(__DIR__ . DIRECTORY_SEPARATOR . + $this->validate($this->resolveDir($app, $source) . '/request.json', $arguments); + } + + private function resolveDir(string $app, string $source): string + { + return __DIR__ . DIRECTORY_SEPARATOR . $app . DIRECTORY_SEPARATOR . - $source . DIRECTORY_SEPARATOR . 'request.json', $arguments); + $source; } /** @@ -90,10 +93,8 @@ private function validate(string $schemaFile, array $arguments): void /** * @throws DownloaderException */ - private function validateResponse(array $arguments, string $app, string $source): void + public function validateResponse(array $arguments, string $app, string $source): void { - $this->validate(__DIR__ . DIRECTORY_SEPARATOR . - $app . DIRECTORY_SEPARATOR . - $source . DIRECTORY_SEPARATOR . 'response.json', $arguments); + $this->validate($this->resolveDir($app, $source) . '/response.json', $arguments); } } diff --git a/tools/downloader.php b/tools/downloader.php index fc0a97c..aa4c9d9 100644 --- a/tools/downloader.php +++ b/tools/downloader.php @@ -16,7 +16,7 @@ 'password' => FKSDB_PASS, ], ]); -echo $newDownloader->download('fksdb', 'GetOrganizers', ['contest_id' => 1, 'year' => 37]); +var_dump($newDownloader->download('fksdb', 'GetOrganizers', ['contest_id' => 1, 'year' => 37])); return $newDownloader; /* return function (Request $request, bool $soap = true) use ($new): ?string {