php-spark/plugins/com.noccy.apiclient/Commands/ApiRequestCommand.php

135 lines
4.6 KiB
PHP
Raw Normal View History

<?php
namespace SparkPlug\Com\Noccy\ApiClient\Commands;
use Spark\Commands\Command;
use SparkPlug;
use SparkPlug\Com\Noccy\ApiClient\Api\Method;
use SparkPlug\Com\Noccy\ApiClient\ApiClientPlugin;
use SparkPlug\Com\Noccy\ApiClient\Request\RequestBuilder;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class ApiRequestCommand extends Command
{
protected function configure()
{
$this->setName("api:request")
->setDescription("Send a request")
->addOption("profile", "p", InputOption::VALUE_REQUIRED, "Use profile for request")
->addOption("save", "s", InputOption::VALUE_NONE, "Save to catalog")
->addArgument("method", InputArgument::OPTIONAL, "Request URL or catalog.method")
->addArgument("props", InputArgument::IS_ARRAY, "Parameter key=value pairs")
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var ApiClientPlugin */
$plugin = get_plugin('com.noccy.apiclient');
$separator = str_repeat("\u{2500}", 40);
$method = $input->getArgument("method");
$builder = new RequestBuilder();
if (str_contains($method, "://")) {
$builder->setProperties([
'url' => $method
]);
} else {
if (str_contains($method, '.')) {
[$catalog,$method] = explode(".", $method, 2);
$catalogObj = $plugin->getCatalog($catalog);
// if (!$catalogObj) {
// $output->writeln("<error>No such catalog {$catalog}</>");
// return Command::FAILURE;
// }
$methodObj = $catalogObj->getMethod($method);
// if (!$methodObj) {
// $output->writeln("<error>No such method {$method} in catalog {$catalog}</>");
// return Command::FAILURE;
// }
$builder->setCatalog($catalogObj);
$builder->setMethod($methodObj);
}
}
$props = [];
$propstr = $input->getArgument("props");
foreach ($propstr as $str) {
if (!str_contains($str,"=")) {
$output->writeln("<error>Ignoring parameter argument '{$str}'</>");
} else {
[$k,$v] = explode("=",$str,2);
$props[$k] = $v;
}
}
$builder->addProperties($props);
if ($input->getOption("save")) {
$catalogObj = $plugin->getCatalog($catalog);
$methodObj = new Method([
'name' => $method,
'info' => $props['method.info']??null,
'props' => $props
]);
$catalogObj->addMethod($method, $methodObj);
$plugin->saveCatalog($catalogObj);
$output->writeln("<info>Saved method {$method} to catalog {$catalog}</>");
return self::SUCCESS;
}
if ($profile = $input->getOption("profile")) {
$profileObj = $plugin->getProfile($profile);
$builder->setProfile($profileObj);
}
$request = $builder->getRequest();
$table = new Table($output);
$table->setStyle('compact');
$table->setHeaders([ "Request Info", "" ]);
foreach ($request->getInfo() as $i=>$v) {
$table->addRow([$i,$v]);
}
$table->render();
$table = new Table($output);
$table->setStyle('compact');
$table->setHeaders([ "Request Headers", "" ]);
foreach ($request->getHeaders() as $i=>$v) {
$table->addRow([$i,join("\n",$v)]);
}
$table->render();
$output->writeln($separator);
$response = $request->send();
$rheaders = $response->getHeaders();
$table = new Table($output);
$table->setStyle('compact');
$table->setHeaders([ "Response headers", "" ]);
foreach ($rheaders as $h=>$v) {
$table->addRow([$h,join("\n",$v)]);
}
$table->render();
$body = (string)$response->getBody();
$output->writeln($separator);
$parseAs = $builder->getCalculatedProperty('response.parse');
if ($parseAs == 'json') {
dump(json_decode($body));
} else {
$output->writeln($body);
}
$output->writeln($separator);
$output->writeln(strlen($body)." bytes");
return Command::SUCCESS;
}
}