-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
296 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fykosak\FKSDBDownloaderCore; | ||
|
||
use JsonSchema\Constraints\Constraint; | ||
use JsonSchema\Validator; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
class Downloader | ||
{ | ||
private array $config; | ||
|
||
public function __construct(array $config) | ||
{ | ||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
* @throws DownloaderException | ||
*/ | ||
public function download(string $app, string $source, array $arguments) | ||
{ | ||
$config = $this->config[$app]; | ||
$this->validateRequest($arguments, $app, $source); | ||
|
||
$options = [ | ||
'http' => [ | ||
'header' => "Content-type: application/x-www-form-urlencoded\r\n" . | ||
"Authorization: Basic " . base64_encode($config['username'] . ':' . $config['password']), | ||
'method' => 'GET', | ||
'content' => http_build_query($arguments), | ||
], | ||
]; | ||
$context = stream_context_create($options); | ||
|
||
$result = file_get_contents( | ||
$this->config[$app]['url'] . $source | ||
. '?' . http_build_query($arguments), | ||
false, | ||
$context | ||
); | ||
if ($result === false) { | ||
restore_error_handler(); | ||
throw new DownloaderException(error_get_last()['message']); | ||
} | ||
$this->validateResponse(json_decode($result), $app, $source); | ||
return $result; | ||
} | ||
|
||
/** | ||
* @throws DownloaderException | ||
*/ | ||
private function validateRequest(array $arguments, string $app, string $source): void | ||
{ | ||
$this->validate(__DIR__ . DIRECTORY_SEPARATOR . | ||
$app . DIRECTORY_SEPARATOR . | ||
$source . DIRECTORY_SEPARATOR . 'request.json', $arguments); | ||
} | ||
|
||
/** | ||
* @throws DownloaderException | ||
*/ | ||
private function validate(string $schemaFile, array $arguments): void | ||
{ | ||
try { | ||
$validator = new Validator(); | ||
$validator->validate($arguments, (object)[ | ||
'$ref' => 'file://' . $schemaFile, | ||
], Constraint::CHECK_MODE_TYPE_CAST); | ||
|
||
if ($validator->isValid()) { | ||
return; | ||
} else { | ||
$errors = ''; | ||
foreach ($validator->getErrors() as $error) { | ||
var_dump($error); | ||
$errors .= join(', ', $error); | ||
} | ||
throw new DownloaderException($errors); | ||
} | ||
} catch (\Throwable$exception) { | ||
throw new DownloaderException($exception->getMessage(), $exception->getCode(), $exception); | ||
} | ||
} | ||
|
||
/** | ||
* @throws DownloaderException | ||
*/ | ||
private function validateResponse(array $arguments, string $app, string $source): void | ||
{ | ||
$this->validate(__DIR__ . DIRECTORY_SEPARATOR . | ||
$app . DIRECTORY_SEPARATOR . | ||
$source . DIRECTORY_SEPARATOR . 'response.json', $arguments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fykosak\FKSDBDownloaderCore; | ||
|
||
class DownloaderException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"type": "object", | ||
"required": [ | ||
"contest_id" | ||
], | ||
"properties": { | ||
"contest_id": { | ||
"type": "integer" | ||
}, | ||
"year": { | ||
"type": "integer" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"type": "array", | ||
"items": { | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"personId": { | ||
"type": "integer", | ||
"min": 0 | ||
}, | ||
"email": { | ||
"type": [ | ||
"null", | ||
"string" | ||
] | ||
}, | ||
"academicDegreePrefix": { | ||
"type": [ | ||
"string", | ||
"null" | ||
] | ||
}, | ||
"academicDegreeSuffix": { | ||
"type": [ | ||
"string", | ||
"null" | ||
] | ||
}, | ||
"career": { | ||
"type": [ | ||
"string", | ||
"null" | ||
] | ||
}, | ||
"contribution": { | ||
"type": [ | ||
"string", | ||
"null" | ||
] | ||
}, | ||
"order": { | ||
"type": "integer", | ||
"min": 0, | ||
"max": 10 | ||
}, | ||
"role": { | ||
"type": [ | ||
"string", | ||
"null" | ||
] | ||
}, | ||
"since": { | ||
"type": "integer" | ||
}, | ||
"until": { | ||
"type": [ | ||
"integer", | ||
"null" | ||
] | ||
}, | ||
"texSignature": { | ||
"type": [ | ||
"string", | ||
"null" | ||
] | ||
}, | ||
"domainAlias": { | ||
"type": [ | ||
"string", | ||
"null" | ||
] | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters