Start implementing Trello API service

Implement name of system to import
Implement need validate data
Fix allowed system list
Start implementing Trello API service

Signed-off-by: Vitor Mattos <vitor@php.rio>
This commit is contained in:
Vitor Mattos
2021-07-24 20:26:34 -03:00
committed by Julius Härtl
parent c7a37ea425
commit 202ea30090
14 changed files with 219 additions and 38 deletions

View File

@@ -33,8 +33,11 @@ use OCP\AppFramework\Db\Entity;
use OCP\Comments\IComment;
abstract class ABoardImportService {
/** @var string */
public static $name = '';
/** @var BoardImportService */
private $boardImportService;
protected $needValidateData = true;
/** @var Stack[] */
protected $stacks = [];
/** @var Label[] */
@@ -123,4 +126,8 @@ abstract class ABoardImportService {
public function getImportService(): BoardImportService {
return $this->boardImportService;
}
public function needValidateData(): bool {
return $this->needValidateData;
}
}

View File

@@ -126,19 +126,24 @@ class BoardImportCommandService extends BoardImportService {
} catch (\Throwable $th) {
}
$helper = $this->getCommand()->getHelper('question');
$allowedSystems = $this->getAllowedImportSystems();
$names = array_column($allowedSystems, 'name');
$question = new ChoiceQuestion(
'Please inform a source system',
$this->getAllowedImportSystems(),
$names,
0
);
$question->setErrorMessage('System %s is invalid.');
$system = $helper->ask($this->getInput(), $this->getOutput(), $question);
$this->getInput()->setOption('system', $system);
$this->setSystem($system);
$selectedName = $helper->ask($this->getInput(), $this->getOutput(), $question);
$className = $allowedSystems[array_flip($names)[$selectedName]]['internalName'];
$this->setSystem($className);
return;
}
protected function validateData(): void {
if (!$this->getImportSystem()->needValidateData()) {
return;
}
$data = $this->getInput()->getOption('data');
if (is_string($data)) {
$data = json_decode(file_get_contents($data));

View File

@@ -67,7 +67,7 @@ class BoardImportService {
private $system = '';
/** @var null|ABoardImportService */
private $systemInstance;
/** @var string[] */
/** @var array */
private $allowedSystems = [];
/**
* Data object created from config JSON
@@ -142,7 +142,9 @@ class BoardImportService {
}
public function validateSystem(): void {
if (!in_array($this->getSystem(), $this->getAllowedImportSystems())) {
$allowedSystems = $this->getAllowedImportSystems();
$allowedSystems = array_column($allowedSystems, 'internalName');
if (!in_array($this->getSystem(), $allowedSystems)) {
throw new NotFoundException('Invalid system');
}
}
@@ -173,9 +175,23 @@ class BoardImportService {
}
return true;
});
$allowedSystems = array_map(function ($name) {
preg_match('/\/BoardImport(?<system>\w+)Service\.php$/', $name, $matches);
return lcfirst($matches['system']);
$allowedSystems = array_map(function ($filename) {
preg_match('/\/(?<class>BoardImport(?<system>\w+)Service)\.php$/', $filename, $matches);
$className = 'OCA\Deck\Service\\'.$matches['class'];
if (!class_exists($className)) {
/** @psalm-suppress UnresolvableInclude */
require_once $name;
}
/** @psalm-suppress InvalidPropertyFetch */
$name = $className::$name;
if (empty($name)) {
$name = lcfirst($matches['system']);
}
return [
'name' => $name,
'class' => $className,
'internalName' => lcfirst($matches['system'])
];
}, $allowedSystems);
$this->allowedSystems = array_values($allowedSystems);
}

View File

@@ -0,0 +1,99 @@
<?php
/**
* @copyright Copyright (c) 2021 Vitor Mattos <vitor@php.rio>
*
* @author Vitor Mattos <vitor@php.rio>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Deck\Service;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\RequestException;
use OCP\AppFramework\Http;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IUserManager;
class BoardImportTrelloApiService extends BoardImportTrelloJsonService {
/** @var string */
public static $name = 'Trello API';
protected $needValidateData = false;
/** @var IClient */
private $httpClient;
/** @var ILogger */
protected $logger;
/** @var string */
private $baseApiUrl = 'https://api.trello.com/1';
public function __construct(
IUserManager $userManager,
IL10N $l10n,
ILogger $logger,
IClientService $httpClientService
) {
parent::__construct($userManager, $l10n);
$this->logger = $logger;
$this->httpClient = $httpClientService->newClient();
}
public function bootstrap(): void {
$this->getBoards();
parent::bootstrap();
}
private function getBoards() {
$boards = $this->doRequest('/members/me/boards');
}
private function doRequest($path, $queryString = []) {
try {
$target = $this->baseApiUrl . $path;
$result = $this->httpClient
->get($target, $this->getQueryString($queryString))
->getBody();
$data = json_decode($result);
} catch (ClientException $e) {
$status = $e->getCode();
if ($status === Http::STATUS_FORBIDDEN) {
$this->logger->info($target . ' refused.', ['app' => 'deck']);
} else {
$this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'deck']);
}
} catch (RequestException $e) {
$this->logger->logException($e, [
'message' => 'Could not connect to ' . $target,
'level' => ILogger::INFO,
'app' => 'deck',
]);
} catch (\Throwable $e) {
$this->logger->logException($e, ['app' => 'deck']);
}
return $data;
}
private function getQueryString($params = []): array {
$apiSettings = $this->getImportService()->getConfig('api');
$params['key'] = $apiSettings->key;
$params['value'] = $apiSettings->token;
return $params;
}
}

View File

@@ -35,7 +35,9 @@ use OCP\IL10N;
use OCP\IUser;
use OCP\IUserManager;
class BoardImportTrelloService extends ABoardImportService {
class BoardImportTrelloJsonService extends ABoardImportService {
/** @var string */
public static $name = 'Trello JSON';
/** @var IUserManager */
private $userManager;
/** @var IL10N */

View File

@@ -0,0 +1,44 @@
{
"type": "object",
"properties": {
"api": {
"type": "object",
"properties": {
"key": {
"type": "string",
"pattern": "^\\w{32}$"
},
"token": {
"type": "string",
"pattern": "^\\w{1,}$"
}
}
},
"boards": {
"type": "array",
"items": {
"type": "string",
"pattern": "^\\w{1,}$"
}
},
"uidRelation": {
"type": "object",
"comment": "Relationship between Trello and Nextcloud usernames",
"example": {
"johndoe": "admin"
}
},
"owner": {
"type": "string",
"required": true,
"comment": "Nextcloud owner username"
},
"color": {
"type": "string",
"required": true,
"pattern": "^[0-9a-fA-F]{6}$",
"comment": "Default color for the board. If you don't inform, the default color will be used.",
"default": "0800fd"
}
}
}