Skip to content

Commit

Permalink
downloader JSON request
Browse files Browse the repository at this point in the history
  • Loading branch information
cevro committed Oct 1, 2023
1 parent 37022e5 commit b45d42c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
35 changes: 18 additions & 17 deletions src/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use JsonSchema\Constraints\Constraint;
use JsonSchema\Validator;

require __DIR__ . '/../vendor/autoload.php';

class Downloader
{
private array $config;
Expand All @@ -19,46 +17,51 @@ 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
);
if ($result === false) {
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;
}

/**
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion tools/downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b45d42c

Please sign in to comment.