-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApiCallCommand.php
More file actions
136 lines (105 loc) · 4.74 KB
/
Copy pathApiCallCommand.php
File metadata and controls
136 lines (105 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Devhelp\PiwikBundle\Command;
use Devhelp\Piwik\Api\Method\Method;
use Devhelp\PiwikBundle\Command\Param\MethodFinder;
use Psr\Http\Message\ResponseInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
class ApiCallCommand extends ContainerAwareCommand
{
/**
* @var ConsoleLogger
*/
private $logger;
protected function configure()
{
// @codingStandardsIgnoreStart
$this
->setName('devhelp_piwik:api:call')
->setDescription('Calls piwik api method')
->addArgument(
'method',
InputArgument::REQUIRED,
'Method to call. Can be either a service id (without @) or method name (like "Actions.getPageUrls")'
)
->addOption(
'params',
'p',
InputOption::VALUE_OPTIONAL,
'Method parameters as string (must be valid for parse_str function). '.
'They will be merged with params from method service (if it has any) and will override existing ones if names are the same'
)
->addOption(
'api',
null,
InputOption::VALUE_OPTIONAL,
'Api that will be used to call the method (name from configuration). '.
'If not specified default api is used. Is ignored if "method" is passed as a service'
)->addOption(
'show-response',
'r',
InputOption::VALUE_NONE,
'If set then response is logged. '.
'Verbosity level must be at least -vv and used together with this option in order to display the response'
)
->setHelp(<<<HELP
The <info>%command.name%</info> command calls Piwik API method.
Method can be either service id or method name:
<info>php %command.full_name% SitesManager.getAllSites</info>
<info>php %command.full_name% my_method_service_id</info>
If method name is used then you can use <comment>--api</comment> to set api name from which the method is to be called (if not specified then default api is used):
<info>php %command.full_name% SitesManager.getAllSites --api=reader</info>
Use <comment>--params</comment> in order to set call parameters (they will be merged with default params defined for the method service):
<info>php %command.full_name% my_method_service_id --params='idSite=2&token_auth=MY_TOKEN'</info>
Use <comment>-vv</comment> in order to display relevant information about the ongoing request
<info>php %command.full_name% my_method_service_id -vv</info>
Use <comment>--show-response</comment> together with <comment>-vv</comment> in order to display the response. <comment>Response must be PSR-7 compatible</comment>
<info>php %command.full_name% my_method_service_id -vv --show-response</info>
HELP
);
// @codingStandardsIgnoreEnd
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$methodArg = $input->getArgument('method');
$apiOption = $input->getOption('api');
$params = [];
if ($input->hasOption('params')) {
parse_str($input->getOption('params'), $params);
}
$this->logger = new ConsoleLogger($output);
$method = $this->getMethod($methodArg, $apiOption);
$callDetails = [];
$callDetails[] = "Calling api...";
$callDetails[] = "- method: ".$method->name();
$callDetails[] = "- url: ".$method->url();
if ($params) {
$callDetails[] = "- extra params: ";
$callDetails[] = var_export($params, true);
}
$this->logger->info(implode(PHP_EOL, $callDetails));
$response = $method->call($params);
$resultDetails = [];
$resultDetails[] = "Finished";
if ($input->getOption('show-response') && $response instanceof ResponseInterface) {
$resultDetails[] = "Response:";
$resultDetails[] = "- status code: ".$response->getStatusCode();
$resultDetails[] = "- body: ".$response->getBody()->getContents();
}
$this->logger->info(implode(PHP_EOL, $resultDetails));
}
/**
* @param string $methodArg
* @param string $apiOption
* @return Method
*/
private function getMethod($methodArg, $apiOption)
{
$methodFinder = new MethodFinder($this->getContainer(), $this->logger);
return $methodFinder->find($methodArg, $apiOption);
}
}