chg: [requestProcessor] Improved integration with local tool connectors

pull/59/head
mokaddem 2021-06-14 13:30:26 +02:00
parent 63e9dd1644
commit 1d293c350f
2 changed files with 141 additions and 29 deletions

View File

@ -60,7 +60,7 @@ class LocalToolRequestProcessor extends GenericRequestProcessor
try {
$connectorClasses = $this->LocalTools->getConnectorByToolName($request->local_tool_name);
if (!empty($connectorClasses)) {
$connector = $this->LocalTools->extractMeta($connectorClasses)[0];
$connector = array_values($connectorClasses)[0];
}
} catch (Cake\Http\Exception\NotFoundException $e) {
$connector = null;
@ -68,12 +68,25 @@ class LocalToolRequestProcessor extends GenericRequestProcessor
return $connector;
}
protected function getConnectorMeta($request)
{
try {
$connectorClasses = $this->LocalTools->getConnectorByToolName($request->local_tool_name);
if (!empty($connectorClasses)) {
$connectorMeta = $this->LocalTools->extractMeta($connectorClasses)[0];
}
} catch (Cake\Http\Exception\NotFoundException $e) {
$connectorMeta = null;
}
return !is_null($connectorMeta) ? $connectorMeta : [];
}
protected function addBaseValidatorRules($validator)
{
return $validator
->requirePresence('toolName')
->notEmpty('toolName', 'A url must be provided')
->requirePresence('url')
->requirePresence('url') // url -> cerebrate_url
->notEmpty('url', 'A url must be provided');
// ->add('url', 'validFormat', [
// 'rule' => 'url',
@ -106,7 +119,7 @@ class IncomingConnectionRequestProcessor extends LocalToolRequestProcessor imple
public function getViewVariables($request)
{
$request->brood = $this->getIssuerBrood($request);
$request->connector = $this->getConnector($request);
$request->connector = $this->getConnectorMeta($request);
return [
'request' => $request,
'progressStep' => 0,
@ -115,32 +128,76 @@ class IncomingConnectionRequestProcessor extends LocalToolRequestProcessor imple
public function process($id, $requestData, $inboxRequest)
{
$connectionSuccessfull = false;
/**
* /!\ Should how should sent message be? be fire and forget? Only for delined?
*/
$interConnectionResult = [];
$remoteCerebrate = $this->getIssuerBrood($request);
$connector = $this->getConnector($request);
$connectorResult = $connector->acceptConnection($requestData['data']);
$connectorResult['toolName'] = $requestData->local_tool_name;
$urlPath = '/inbox/createInboxEntry/LocalTool/AcceptedRequest';
$response = $this->Inbox->sendRequest($remoteCerebrate, $urlPath, true, $connectorResult);
$remoteCerebrate = $this->getIssuerBrood($inboxRequest);
$connector = $this->getConnector($inboxRequest);
if (!empty($requestData['is_discard'])) { // -> declined
$connectorResult = $this->declineConnection($connector, $remoteCerebrate, $inboxRequest['data']); // Fire-and-forget?
$connectionSuccessfull = true;
$resultTitle = __('Could not sent declined message to `{0}`\'s for {1}', $inboxRequest['origin'], $inboxRequest['local_tool_name']);
$errors = [];
if ($connectionSuccessfull) {
$this->discard($id, $requestData);
$resultTitle = __('Declined message successfully sent to `{0}`\'s for {1}', $inboxRequest['origin'], $inboxRequest['local_tool_name']);
$this->discard($id, $inboxRequest);
}
} else {
$connectorResult = $this->acceptConnection($connector, $remoteCerebrate, $inboxRequest['data']);
$connectionSuccessfull = false;
$connectionData = [];
$resultTitle = __('Could not inter-connect `{0}`\'s {1}', $inboxRequest['origin'], $inboxRequest['local_tool_name']);
$errors = [];
if ($connectionSuccessfull) {
$resultTitle = __('Interconnection for `{0}`\'s {1} created', $inboxRequest['origin'], $inboxRequest['local_tool_name']);
$this->discard($id, $inboxRequest);
}
}
return $this->genActionResult(
$interConnectionResult,
$connectionData,
$connectionSuccessfull,
$connectionSuccessfull ? __('Interconnection for `{0}`\'s {1} created',$requestData['origin'], $requestData['local_tool_name']) : __('Could not inter-connect `{0}`\'s {1}', $requestData['origin'], $requestData['local_tool_name']),
[]
$resultTitle,
$errors
);
}
public function discard($id, $requestData)
{
// /!\ TODO: send decline message to remote cerebrate
return parent::discard($id, $requestData);
}
protected function acceptConnection($connector, $remoteCerebrate, $requestData)
{
$connectorResult = $connector->acceptConnection($requestData['data']);
$connectorResult['toolName'] = $requestData->local_tool_name;
$response = $this->sendAcceptedRequestToRemote($remoteCerebrate, $connectorResult);
// change state if sending fails
// add the entry to the outbox if sending fails.
return $response;
}
protected function declineConnection($connector, $remoteCerebrate, $requestData)
{
$connectorResult = $connector->declineConnection($requestData['data']);
$connectorResult['toolName'] = $requestData->local_tool_name;
$response = $this->sendDeclinedRequestToRemote($remoteCerebrate, $connectorResult);
return $response;
}
protected function sendAcceptedRequestToRemote($remoteCerebrate, $connectorResult)
{
$urlPath = '/inbox/createInboxEntry/LocalTool/AcceptedRequest';
$response = $this->Inbox->sendRequest($remoteCerebrate, $urlPath, true, $connectorResult);
return $response;
}
protected function sendDeclinedRequestToRemote($remoteCerebrate, $connectorResult)
{
$urlPath = '/inbox/createInboxEntry/LocalTool/DeclinedRequest';
$response = $this->Inbox->sendRequest($remoteCerebrate, $urlPath, true, $connectorResult);
return $response;
}
}
class AcceptedRequestProcessor extends LocalToolRequestProcessor implements GenericProcessorActionI {
@ -168,7 +225,7 @@ class AcceptedRequestProcessor extends LocalToolRequestProcessor implements Gene
public function getViewVariables($request)
{
$request->brood = $this->getIssuerBrood($request);
$request->connector = $this->getConnector($request);
$request->connector = $this->getConnectorMeta($request);
return [
'request' => $request,
'progressStep' => 1,
@ -177,16 +234,22 @@ class AcceptedRequestProcessor extends LocalToolRequestProcessor implements Gene
public function process($id, $requestData, $inboxRequest)
{
$connector = $this->getConnector($request);
$remoteCerebrate = $this->getIssuerBrood($request);
$connectorResult = $this->finalizeConnection($connector, $remoteCerebrate, $requestData['data']);
$connectionSuccessfull = false;
$interConnectionResult = [];
$connectionData = [];
$resultTitle = __('Could not finalize inter-connection for `{0}`\'s {1}', $requestData['origin'], $requestData['local_tool_name']);
$errors = [];
if ($connectionSuccessfull) {
$resultTitle = __('Interconnection for `{0}`\'s {1} finalized', $requestData['origin'], $requestData['local_tool_name']);
$this->discard($id, $requestData);
}
return $this->genActionResult(
$interConnectionResult,
$connectionData,
$connectionSuccessfull,
$connectionSuccessfull ? __('Interconnection for `{0}`\'s {1} finalized', $requestData['origin'], $requestData['local_tool_name']) : __('Could not inter-connect `{0}`\'s {1}', $requestData['origin'], $requestData['local_tool_name']),
[]
$resultTitle,
$errors
);
}
@ -194,6 +257,12 @@ class AcceptedRequestProcessor extends LocalToolRequestProcessor implements Gene
{
return parent::discard($id, $requestData);
}
protected function finalizeConnection($connector, $remoteCerebrate, $requestData)
{
$connectorResult = $connector->finaliseConnection($requestData['data']);
return $connectorResult;
}
}
class DeclinedRequestProcessor extends LocalToolRequestProcessor implements GenericProcessorActionI {
@ -220,7 +289,7 @@ class DeclinedRequestProcessor extends LocalToolRequestProcessor implements Gene
public function getViewVariables($request)
{
$request->brood = $this->getIssuerBrood($request);
$request->connector = $this->getConnector($request);
$request->connector = $this->getConnectorMeta($request);
return [
'request' => $request,
'progressStep' => 1,

View File

@ -4,7 +4,8 @@ $defaultSteps = [
'text' => __('Request Sent'),
'icon' => 'paper-plane',
'title' => __(''),
'confirmButton' => __('Accept Request')
'confirmButton' => __('Accept Request'),
'canDiscard' => true,
],
[
'text' => __('Request Accepted'),
@ -19,6 +20,8 @@ $defaultSteps = [
]
];
$footerButtons = [];
$progressVariant = !empty($progressVariant) ? $progressVariant : 'info';
$finalSteps = array_replace($defaultSteps, $steps ?? []);
$currentStep = $finalSteps[$progressStep];
@ -28,6 +31,23 @@ $progress = $this->Bootstrap->progressTimeline([
'steps' => $finalSteps,
]);
$footerButtons[] = [
'clickFunction' => 'cancel',
'variant' => 'secondary',
'text' => __('Cancel'),
];
if (!empty($currentStep['canDiscard'])) {
$footerButtons[] = [
'clickFunction' => 'discard',
'variant' => 'danger',
'text' => __('Decline Request'),
];
}
$footerButtons[] = [
'clickFunction' => 'accept',
'text' => $currentStep['confirmButton'] ?? __('Submit'),
];
$table = $this->Bootstrap->table(['small' => true, 'bordered' => false, 'striped' => false, 'hover' => false], [
'fields' => [
['key' => 'connector', 'label' => __('Tool Name'), 'formatter' => function($connector, $row) {
@ -55,7 +75,13 @@ $form = $this->element('genericElements/Form/genericForm', [
'raw' => true,
'data' => [
'model' => 'Inbox',
'fields' => [],
'fields' => [
[
'field' => 'is_discard',
'type' => 'checkbox',
'default' => false
]
],
'submit' => [
'action' => $this->request->getParam('action')
]
@ -70,7 +96,7 @@ $requestData = $this->Bootstrap->collapse(
sprintf('<pre class="p-2 rounded mb-0" style="background: #eee;"><code>%s</code></pre>', json_encode($request['data'], JSON_PRETTY_PRINT))
);
$bodyHtml = sprintf('<div class="py-2"><div>%s</div>%s</div>%s',
$bodyHtml = sprintf('<div class="py-2"><div>%s</div>%s</div><div class="d-none">%s</div>',
$table,
$requestData,
$form
@ -79,10 +105,27 @@ $bodyHtml = sprintf('<div class="py-2"><div>%s</div>%s</div>%s',
echo $this->Bootstrap->modal([
'title' => __('Interconnection Request for {0}', h($request->data['toolName'])),
'size' => 'lg',
'type' => 'confirm',
'type' => 'custom',
'bodyHtml' => sprintf('<div class="p-3">%s</div><div class="description-container">%s</div>',
$progress,
$bodyHtml
),
'confirmText' => $currentStep['confirmButton'] ?? __('Submit'),
'footerButtons' => $footerButtons
]);
?>
<script>
function accept(modalObject, tmpApi) {
const $form = modalObject.$modal.find('form')
return tmpApi.postForm($form[0])
}
function discard(modalObject, tmpApi) {
const $form = modalObject.$modal.find('form')
const $discardField = $form.find('input#is_discard-field')
$discardField.prop('checked', true)
return tmpApi.postForm($form[0])
}
function cancel(modalObject, tmpApi) {
}
</script>