Commit 122d061a authored by Roman Ondráček's avatar Roman Ondráček

IQRF Net: add JSON API request validation on Send JSON request page

Signed-off-by: Roman Ondráček's avatarRoman Ondráček <ondracek.roman@centrum.cz>
parent 76074b6b
Pipeline #2114 passed with stages
in 14 minutes and 51 seconds
......@@ -20,6 +20,7 @@ declare(strict_types = 1);
namespace App\IqrfNetModule\Forms;
use App\CoreModule\Exceptions\InvalidJsonException;
use App\CoreModule\Exceptions\NonExistingJsonSchemaException;
use App\CoreModule\Forms\FormFactory;
use App\IqrfNetModule\Exceptions\DpaErrorException;
......@@ -29,7 +30,9 @@ use App\IqrfNetModule\Models\WebSocketClient;
use App\IqrfNetModule\Presenters\SendJsonPresenter;
use App\IqrfNetModule\Requests\DpaRequest;
use App\IqrfNetModule\Responses\ApiResponse;
use Contributte\Translation\Wrappers\NotTranslate;
use Nette\Application\UI\Form;
use Nette\Forms\Controls\TextArea;
use Nette\SmartObject;
use Nette\Utils\Json;
use Nette\Utils\JsonException;
......@@ -66,6 +69,11 @@ class SendJsonFormFactory {
*/
private $wsClient;
/**
* Translation prefix
*/
private const PREFIX = 'iqrfnet.send-json.';
/**
* Constructor
* @param ApiSchemaManager $schemaManager API JSON schema manager
......@@ -87,34 +95,48 @@ class SendJsonFormFactory {
*/
public function create(SendJsonPresenter $presenter): Form {
$this->presenter = $presenter;
$form = $this->factory->create('iqrfnet.send-json');
$form->addTextArea('json', 'json')
->setRequired('messages.json');
$form->addSubmit('send', 'send')
->setHtmlAttribute('class', 'ajax');
$form = $this->factory->create();
$form->addTextArea('json', self::PREFIX . 'json')
->setRequired(self::PREFIX . 'messages.json');
$form->addSubmit('send', self::PREFIX . 'send')
->setHtmlAttribute('class', self::PREFIX . 'ajax');
$form->addProtection('core.errors.form-timeout');
$form->onSuccess[] = [$this, 'onSuccess'];
$form->onValidate[] = [$this, 'validate'];
$form->onSuccess[] = [$this, 'send'];
return $form;
}
/**
* Sends IQRF JSON API request
* Validates the IQRF JSON API request
* @param Form $form Send IQRF JSON request form
*/
public function onSuccess(Form $form): void {
$values = $form->getValues();
$json = [];
public function validate(Form $form): void {
/**
* @var TextArea $request JSON request textarea
*/
$request = $form['json'];
try {
$json = Json::decode($values->json);
$json = Json::decode($form->getValues()->json);
if (isset($json->mType)) {
$this->jsonSchemaManager->setSchemaForRequest($json->mType);
$this->jsonSchemaManager->validate($json, true);
$this->jsonSchemaManager->validate($json);
}
} catch (JsonException $e) {
$this->presenter->flashError('iqrfnet.send-json.messages.invalidJson');
$request->addError(self::PREFIX . 'messages.invalidJson');
} catch (NonExistingJsonSchemaException $e) {
$this->presenter->flashError('iqrfnet.send-json.messages.missingSchema');
$request->addError(self::PREFIX . 'messages.missingSchema');
} catch (InvalidJsonException $e) {
$request->addError(new NotTranslate($e->getMessage()));
}
$this->presenter->redrawControl('form');
}
/**
* Sends IQRF JSON API request
* @param Form $form Send IQRF JSON request form
*/
public function send(Form $form): void {
$json = Json::decode($form->getValues()->json);
try {
$this->request->set($json);
$response = $this->wsClient->sendSync($this->request, false);
......
......@@ -21,7 +21,7 @@
<header class='panel-heading'>
<a class='btn btn-primary btn-xs' href='https://docs.iqrf.org/iqrf-gateway/api.html' role='button' target='_blank'>{_iqrfnet.send-json.documentation}</a>
</header>
<div class='panel-body'>
<div n:snippet='form' class='panel-body'>
{control sendJsonForm}
</div>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment