This repository was archived by the owner on Oct 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathApiListCommand.php
More file actions
203 lines (171 loc) · 5.62 KB
/
ApiListCommand.php
File metadata and controls
203 lines (171 loc) · 5.62 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
declare(strict_types=1);
namespace PCextreme\Cloudstack\Console;
use PCextreme\Cloudstack\Client;
use Symfony\Component\Cache\Simple\FilesystemCache;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
class ApiListCommand extends Command
{
/**
* @var FilesystemCache
*/
private $cache;
/**
* Configures the current command.
*
* @return void
*/
protected function configure()
{
$this->setName('api:list')
->setDescription('Generate API list cache.')
->setHelp("This will generate the API list cache file usign the 'listApis' command.");
}
/**
* Executes the current command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output) : void
{
$urlApi = $this->askUrlApi($input, $output);
$apiKey = $this->askApiKey($input, $output);
$secretKey = $this->askSecretKey($input, $output);
$output->writeLn('');
$output->writeLn("<info>Processing API list. Please Wait...</info>");
$client = new Client([
'urlApi' => $urlApi,
'apiKey' => $apiKey,
'secretKey' => $secretKey,
]);
$command = 'listApis';
$method = $client->getCommandMethod($command);
$url = $client->getCommandUrl($command, []);
$request = $client->getRequest($method, $url, []);
$list = $client->getResponse($request);
// We expect an array from the getResponse method, when this returns
// with a string we most likely got a server error.
if (is_string($list)) {
throw new \RuntimeException(sprintf(
"Invalid API response, received: %s",
$list
));
}
$this->processList($output, $list);
}
/**
* Ask for URL API.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return string
*/
protected function askUrlApi(InputInterface $input, OutputInterface $output) : string
{
$question = new Question(
'Enter target API url [default: https://api.auroracompute.eu/ams]: ',
'https://api.auroracompute.eu/ams'
);
return $this->getHelper('question')->ask($input, $output, $question);
}
/**
* Ask for API key.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return string
*/
protected function askApiKey(InputInterface $input, OutputInterface $output) : string
{
$question = (new Question('Enter API key: '))
->setValidator(function ($answer) {
if (is_null($answer)) {
throw new \InvalidArgumentException("API key can't be null.");
}
return $answer;
})
->setMaxAttempts(2);
return $this->getHelper('question')->ask($input, $output, $question);
}
/**
* Ask for secret key.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return string
*/
protected function askSecretKey(InputInterface $input, OutputInterface $output) : string
{
$question = (new Question('Enter secret key: '))
->setValidator(function ($answer) {
if (is_null($answer)) {
throw new \InvalidArgumentException("Secret key can't be null.");
}
return $answer;
})
->setMaxAttempts(2);
return $this->getHelper('question')->ask($input, $output, $question);
}
/**
* Dump cache file of APIs list.
*
* @param OutputInterface $output
* @param array $list
* @return void
*/
protected function processList(OutputInterface $output, array $list = []) : void
{
if (empty($list)) {
throw new \RuntimeException("API list is empty.");
}
$progress = new ProgressBar($output, $list['listapisresponse']['count']);
$progress->start();
$commands = [];
foreach ($list['listapisresponse']['api'] as $api) {
$commands = array_merge($commands, $this->parseCommand($api));
$progress->advance();
}
$this->cache()->set('api.list', $commands);
$progress->finish();
}
/**
* Parse command data into expected structure
* @param array $command
* @return array
*/
protected function parseCommand(array $command) : array
{
$params = [];
foreach ($command['params'] as $param) {
$params = array_merge($params, [
$param['name'] => [
'description' => $param['description'],
'required' => $param['required'],
'type' => $param['type'],
]
]);
}
return [$command['name'] => [
'description' => $command['description'],
'isasync' => $command['isasync'],
'params' => $params
]];
}
/**
* Get cache driver instance
* @return FilesystemCache
*/
private function cache() : FilesystemCache
{
if (! isset($this->cache)) {
$this->cache = new FilesystemCache('', 0, __DIR__.'/../../cache');
}
return $this->cache;
}
}