|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Tomaj\NetteApi\Component; |
|
6
|
|
|
|
|
7
|
|
|
use Nette\Application\UI\Control; |
|
8
|
|
|
use Nette\Application\UI\Form; |
|
9
|
|
|
use Nette\Bridges\ApplicationLatte\Template; |
|
10
|
|
|
use Nette\Forms\IFormRenderer; |
|
11
|
|
|
use Nette\Http\IRequest; |
|
12
|
|
|
use Nette\Utils\ArrayHash; |
|
13
|
|
|
use Nette\Utils\Html; |
|
14
|
|
|
use Tomaj\Form\Renderer\BootstrapVerticalRenderer; |
|
15
|
|
|
use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface; |
|
16
|
|
|
use Tomaj\NetteApi\Authorization\BasicAuthentication; |
|
17
|
|
|
use Tomaj\NetteApi\Authorization\BearerTokenAuthorization; |
|
18
|
|
|
use Tomaj\NetteApi\Authorization\CookieApiKeyAuthentication; |
|
19
|
|
|
use Tomaj\NetteApi\Authorization\HeaderApiKeyAuthentication; |
|
20
|
|
|
use Tomaj\NetteApi\Authorization\NoAuthorization; |
|
21
|
|
|
use Tomaj\NetteApi\Authorization\QueryApiKeyAuthentication; |
|
22
|
|
|
use Tomaj\NetteApi\EndpointInterface; |
|
23
|
|
|
use Tomaj\NetteApi\Handlers\ApiHandlerInterface; |
|
24
|
|
|
use Tomaj\NetteApi\Link\ApiLink; |
|
25
|
|
|
use Tomaj\NetteApi\Misc\ConsoleRequest; |
|
26
|
|
|
|
|
27
|
|
|
class ApiConsoleControl extends Control |
|
28
|
|
|
{ |
|
29
|
|
|
private $request; |
|
30
|
|
|
|
|
31
|
|
|
private $endpoint; |
|
32
|
|
|
|
|
33
|
|
|
private $handler; |
|
34
|
|
|
|
|
35
|
|
|
private $authorization; |
|
36
|
|
|
|
|
37
|
|
|
private $apiLink; |
|
38
|
|
|
|
|
39
|
|
|
private $formRenderer; |
|
40
|
|
|
|
|
41
|
|
|
private $templateFilePath; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct(IRequest $request, EndpointInterface $endpoint, ApiHandlerInterface $handler, ApiAuthorizationInterface $authorization, ApiLink $apiLink = null) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->request = $request; |
|
46
|
|
|
$this->endpoint = $endpoint; |
|
47
|
|
|
$this->handler = $handler; |
|
48
|
|
|
$this->authorization = $authorization; |
|
49
|
|
|
$this->apiLink = $apiLink; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function render(): void |
|
53
|
|
|
{ |
|
54
|
|
|
/** @var Template $template */ |
|
55
|
|
|
$template = $this->getTemplate(); |
|
56
|
|
|
$template->setFile($this->getTemplateFilePath()); |
|
57
|
|
|
$template->add('handler', $this->handler); |
|
58
|
|
|
$template->render(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function createComponentConsoleForm(): Form |
|
62
|
|
|
{ |
|
63
|
|
|
$form = new Form(); |
|
64
|
|
|
|
|
65
|
|
|
$defaults = []; |
|
66
|
|
|
|
|
67
|
|
|
$form->setRenderer($this->getFormRenderer()); |
|
68
|
|
|
|
|
69
|
|
|
if ($this->apiLink) { |
|
70
|
|
|
$url = $this->apiLink->link($this->endpoint); |
|
71
|
|
|
} else { |
|
72
|
|
|
$uri = $this->request->getUrl(); |
|
73
|
|
|
$scheme = $uri->scheme; |
|
74
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) { |
|
75
|
|
|
$scheme = $_SERVER['HTTP_X_FORWARDED_PROTO']; |
|
76
|
|
|
} |
|
77
|
|
|
$port = ''; |
|
78
|
|
|
if ($uri->scheme === 'http' && $uri->port !== 80) { |
|
79
|
|
|
$port = ':' . $uri->port; |
|
80
|
|
|
} |
|
81
|
|
|
$url = $scheme . '://' . $uri->host . $port . '/api/' . $this->endpoint->getUrl(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$form->addText('api_url', 'Api Url'); |
|
85
|
|
|
$defaults['api_url'] = $url; |
|
86
|
|
|
|
|
87
|
|
|
$form->addText('api_method', 'Method'); |
|
88
|
|
|
$defaults['api_method'] = $this->endpoint->getMethod(); |
|
89
|
|
|
|
|
90
|
|
|
if ($this->authorization instanceof BearerTokenAuthorization) { |
|
91
|
|
|
$form->addText('token', 'Token') |
|
92
|
|
|
->setHtmlAttribute('placeholder', 'Enter token'); |
|
93
|
|
|
} elseif ($this->authorization instanceof BasicAuthentication) { |
|
94
|
|
|
$form->addText('basic_authentication_username', 'Username') |
|
95
|
|
|
->setHtmlAttribute('placeholder', 'Enter basic authentication username'); |
|
96
|
|
|
$form->addText('basic_authentication_password', 'Password') |
|
97
|
|
|
->setHtmlAttribute('placeholder', 'Enter basic authentication password'); |
|
98
|
|
|
} elseif ($this->authorization instanceof QueryApiKeyAuthentication) { |
|
99
|
|
|
$form->addText($this->authorization->getQueryParamName(), 'API key') |
|
100
|
|
|
->setHtmlAttribute('placeholder', 'Enter API key'); |
|
101
|
|
|
} elseif ($this->authorization instanceof HeaderApiKeyAuthentication) { |
|
102
|
|
|
$form->addText('header_api_key', 'API key') |
|
103
|
|
|
->setHtmlAttribute('placeholder', 'Enter API key'); |
|
104
|
|
|
} elseif ($this->authorization instanceof CookieApiKeyAuthentication) { |
|
105
|
|
|
$form->addText('cookie_api_key', 'API key') |
|
106
|
|
|
->setHtmlAttribute('placeholder', 'Enter API key'); |
|
107
|
|
|
} elseif ($this->authorization instanceof NoAuthorization) { |
|
108
|
|
|
$form->addText('authorization', 'Authorization') |
|
109
|
|
|
->setDisabled(true); |
|
110
|
|
|
$defaults['authorization'] = 'No authorization - global access'; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$form->addCheckbox('send_session_id', 'Send session id cookie'); |
|
114
|
|
|
|
|
115
|
|
|
$form->addTextArea('custom_headers', 'Custom headers') |
|
116
|
|
|
->setOption('description', Html::el()->setHtml('Each header on new line. For example: <code>User-agent: Mozilla/5.0</code>')); |
|
117
|
|
|
|
|
118
|
|
|
$form->addText('timeout', 'Timeout') |
|
119
|
|
|
->setDefaultValue(30); |
|
120
|
|
|
|
|
121
|
|
|
$params = $this->handler->params(); |
|
122
|
|
|
foreach ($params as $param) { |
|
123
|
|
|
$param->updateConsoleForm($form); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$form->addSubmit('send', 'Try api') |
|
127
|
|
|
->getControlPrototype() |
|
128
|
|
|
->setName('button') |
|
129
|
|
|
->setHtml('<i class="fa fa-cloud-upload"></i> Try api'); |
|
130
|
|
|
|
|
131
|
|
|
$form->setDefaults($defaults); |
|
132
|
|
|
|
|
133
|
|
|
$form->onSuccess[] = array($this, 'formSucceeded'); |
|
134
|
|
|
return $form; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function formSucceeded(Form $form, ArrayHash $values): void |
|
138
|
|
|
{ |
|
139
|
|
|
$url = $values['api_url']; |
|
140
|
|
|
|
|
141
|
|
|
$token = null; |
|
142
|
|
|
if (isset($values['token'])) { |
|
143
|
|
|
$token = $values['token']; |
|
144
|
|
|
unset($values['token']); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$method = $values['api_method']; |
|
148
|
|
|
unset($values['api_method']); |
|
149
|
|
|
|
|
150
|
|
|
$additionalValues = []; |
|
151
|
|
|
if (isset($values['send_session_id']) && $values['send_session_id']) { |
|
152
|
|
|
$additionalValues['cookieFields'][session_name()] = session_id(); |
|
153
|
|
|
session_write_close(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
if (isset($values['custom_headers']) && $values['custom_headers']) { |
|
157
|
|
|
$additionalValues['headers'] = array_filter(array_map('trim', explode("\n", $values['custom_headers']))); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$additionalValues['timeout'] = $values['timeout']; |
|
161
|
|
|
|
|
162
|
|
|
if ($this->authorization instanceof QueryApiKeyAuthentication) { |
|
163
|
|
|
$queryParamName = $this->authorization->getQueryParamName(); |
|
164
|
|
|
$additionalValues['getFields'][$queryParamName] = $values[$queryParamName] ?? null; |
|
165
|
|
|
} elseif ($this->authorization instanceof HeaderApiKeyAuthentication) { |
|
166
|
|
|
$headerName = $this->authorization->getHeaderName(); |
|
167
|
|
|
$additionalValues['headers'][] = $headerName . ':' . $values['header_api_key'] ?? null; |
|
168
|
|
|
} elseif ($this->authorization instanceof CookieApiKeyAuthentication) { |
|
169
|
|
|
$cookieName = $this->authorization->getCookieName(); |
|
170
|
|
|
$additionalValues['cookieFields'][$cookieName] = $values['cookie_api_key'] ?? null; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$consoleRequest = new ConsoleRequest($this->handler, $this->endpoint, $this->apiLink); |
|
174
|
|
|
$result = $consoleRequest->makeRequest($url, $method, (array) $values, $additionalValues, $token); |
|
175
|
|
|
|
|
176
|
|
|
/** @var Template $template */ |
|
177
|
|
|
$template = $this->getTemplate(); |
|
178
|
|
|
$template->add('response', $result); |
|
179
|
|
|
|
|
180
|
|
|
if ($this->getPresenter()->isAjax()) { |
|
181
|
|
|
$this->getPresenter()->redrawControl(); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function setFormRenderer(IFormRenderer $formRenderer): void |
|
186
|
|
|
{ |
|
187
|
|
|
$this->formRenderer = $formRenderer; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
private function getFormRenderer(): IFormRenderer |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->formRenderer ?: new BootstrapVerticalRenderer(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function setTemplateFilePath(string $templateFilePath): void |
|
196
|
|
|
{ |
|
197
|
|
|
$this->templateFilePath = $templateFilePath; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
private function getTemplateFilePath(): string |
|
201
|
|
|
{ |
|
202
|
|
|
return $this->templateFilePath ?: __DIR__ . '/console.latte'; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|