From 034ff034df5433d3c5a384b91991f40bea3781a7 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Sat, 12 Jun 2021 15:29:16 +0200 Subject: [PATCH] chg: [inbox] Started integrating local tools within the inbox - WiP --- .../LocalToolRequestProcessor.php | 8 +++++++ src/Controller/InboxController.php | 2 +- src/Controller/LocalToolsController.php | 24 ++++++++++--------- src/Model/Table/InboxTable.php | 23 ++++++++++++++++++ src/Model/Table/LocalToolsTable.php | 2 +- templates/LocalTools/connection_request.php | 7 +++++- 6 files changed, 52 insertions(+), 14 deletions(-) diff --git a/libraries/default/RequestProcessors/LocalToolRequestProcessor.php b/libraries/default/RequestProcessors/LocalToolRequestProcessor.php index d672eec..e73c60c 100644 --- a/libraries/default/RequestProcessors/LocalToolRequestProcessor.php +++ b/libraries/default/RequestProcessors/LocalToolRequestProcessor.php @@ -117,6 +117,14 @@ class IncomingConnectionRequestProcessor extends LocalToolRequestProcessor imple { $connectionSuccessfull = false; $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); + if ($connectionSuccessfull) { $this->discard($id, $requestData); } diff --git a/src/Controller/InboxController.php b/src/Controller/InboxController.php index 411b155..66e6136 100644 --- a/src/Controller/InboxController.php +++ b/src/Controller/InboxController.php @@ -118,7 +118,7 @@ class InboxController extends AppController $this->set('data', $data); } - public function createProcessorInboxEntry($scope, $action) + public function createInboxEntry($scope, $action) { if (!$this->request->is('post')) { throw new MethodNotAllowedException(__('Only POST method is accepted')); diff --git a/src/Controller/LocalToolsController.php b/src/Controller/LocalToolsController.php index 61f6e92..195c689 100644 --- a/src/Controller/LocalToolsController.php +++ b/src/Controller/LocalToolsController.php @@ -186,11 +186,8 @@ class LocalToolsController extends AppController }, 'afterFind' => function($data) { foreach ($data as $connector) { - $connector = [ - 'id' => $connector['id'], - 'name' => $connector['name'], - 'connector' => $connector['connector'] - ]; + $connectorClass = array_values($this->LocalTools->getConnectorByConnectionId($connector['id']))[0]; + $connector['toolName'] = $connectorClass->name; } return $data; } @@ -222,18 +219,23 @@ class LocalToolsController extends AppController 'cerebrate_id' => $cerebrate_id, 'remote_tool_id' => $remote_tool_id ]; + $this->loadModel('Broods'); + $remoteCerebrate = $this->Broods->find()->where(['id' => $params['cerebrate_id']])->first(); if ($this->request->is(['post', 'put'])) { - $postParams = $this->ParamHandler->harvestParams(['local_tool_id']); + $postParams = $this->ParamHandler->harvestParams(['local_tool_id', 'tool_name']); if (empty($postParams['local_tool_id'])) { throw new MethodNotAllowedException(__('No local tool ID supplied.')); } + if (empty($postParams['tool_name'])) { + throw new MethodNotAllowedException(__('No local tool name supplied.')); + } $params['local_tool_id'] = $postParams['local_tool_id']; - $result = $this->LocalTools->encodeConnection($params); - // Send message to remote inbox - debug($result); + $encodingResult = $this->LocalTools->encodeConnection($params); + $encodingResult['toolName'] = $tool_name; + $urlPath = '/inbox/createInboxEntry/LocalTool/IncomingConnectionRequest'; + $response = $this->Inbox->sendRequest($remoteCerebrate, $urlPath, true, $encodingResult); + $this->redirect(); } else { - $this->loadModel('Broods'); - $remoteCerebrate = $this->Broods->find()->where(['id' => $params['cerebrate_id']])->first(); $remoteTool = $this->LocalTools->getRemoteToolById($params); $local_tools = $this->LocalTools->encodeConnectionChoice($params); if (empty($local_tools)) { diff --git a/src/Model/Table/InboxTable.php b/src/Model/Table/InboxTable.php index 93a7f22..ca4664f 100644 --- a/src/Model/Table/InboxTable.php +++ b/src/Model/Table/InboxTable.php @@ -62,4 +62,27 @@ class InboxTable extends AppTable return $rules; } + + public function sendRequest($brood, $urlPath, $methodPost = true, $data = []): boolean + { + $http = new Client(); + $config = [ + 'headers' => [ + 'AUTHORIZATION' => $brood->authkey, + 'Accept' => 'application/json' + ], + 'type' => 'json' + ]; + $url = $brood->url . $urlPath; + if ($methodPost) { + $response = $http->post($url, json_encode(data), $config); + } else { + $response = $http->get($brood->url, json_encode(data), $config); + } + if ($response->isOk()) { + return $response; + } else { + throw new NotFoundException(__('Could not post to the requested resource.')); + } + } } diff --git a/src/Model/Table/LocalToolsTable.php b/src/Model/Table/LocalToolsTable.php index 25d124e..22dadfb 100644 --- a/src/Model/Table/LocalToolsTable.php +++ b/src/Model/Table/LocalToolsTable.php @@ -112,7 +112,7 @@ class LocalToolsTable extends AppTable 'connector_description' => $connector_class->description ]; if ($includeConnections) { - $connector['connections'] = $this->healthCheck($connector_type, $connector_class); + // $connector['connections'] = $this->healthCheck($connector_type, $connector_class); } $connectors[] = $connector; } diff --git a/templates/LocalTools/connection_request.php b/templates/LocalTools/connection_request.php index 8f672e7..ddde00d 100644 --- a/templates/LocalTools/connection_request.php +++ b/templates/LocalTools/connection_request.php @@ -16,7 +16,12 @@ 'field' => 'local_tool_id', 'options' => $dropdown, 'type' => 'dropdown' - ] + ], + [ + 'field' => 'tool_name', + 'default' => $data['remoteTool']['toolName'], + 'type' => 'hidden' + ], ], 'submit' => [ 'action' => $this->request->getParam('action')