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\Http\IRequest; |
11
|
|
|
use Nette\Utils\ArrayHash; |
12
|
|
|
use Nette\Utils\Html; |
13
|
|
|
use Tomaj\Form\Renderer\BootstrapRenderer; |
14
|
|
|
use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface; |
15
|
|
|
use Tomaj\NetteApi\Authorization\BearerTokenAuthorization; |
16
|
|
|
use Tomaj\NetteApi\Authorization\NoAuthorization; |
17
|
|
|
use Tomaj\NetteApi\EndpointInterface; |
18
|
|
|
use Tomaj\NetteApi\Handlers\ApiHandlerInterface; |
19
|
|
|
use Tomaj\NetteApi\Misc\ConsoleRequest; |
20
|
|
|
|
21
|
|
|
class ApiConsoleControl extends Control |
22
|
|
|
{ |
23
|
|
|
private $request; |
24
|
|
|
|
25
|
|
|
private $endpoint; |
26
|
|
|
|
27
|
|
|
private $handler; |
28
|
|
|
|
29
|
|
|
private $authorization; |
30
|
|
|
|
31
|
|
|
public function __construct(IRequest $request, EndpointInterface $endpoint, ApiHandlerInterface $handler, ApiAuthorizationInterface $authorization) |
32
|
|
|
{ |
33
|
|
|
$this->request = $request; |
34
|
|
|
$this->endpoint = $endpoint; |
35
|
|
|
$this->handler = $handler; |
36
|
|
|
$this->authorization = $authorization; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function render(): void |
40
|
|
|
{ |
41
|
|
|
/** @var Template $template */ |
42
|
|
|
$template = $this->getTemplate(); |
43
|
|
|
$template->setFile(__DIR__ . '/console.latte'); |
44
|
|
|
$template->add('handler', $this->handler); |
45
|
|
|
$template->render(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function createComponentConsoleForm(): Form |
49
|
|
|
{ |
50
|
|
|
$form = new Form(); |
51
|
|
|
|
52
|
|
|
$defaults = []; |
53
|
|
|
|
54
|
|
|
$form->setRenderer(new BootstrapRenderer()); |
55
|
|
|
|
56
|
|
|
$uri = $this->request->getUrl(); |
57
|
|
|
$scheme = $uri->scheme; |
58
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) { |
59
|
|
|
$scheme = $_SERVER['HTTP_X_FORWARDED_PROTO']; |
60
|
|
|
} |
61
|
|
|
$port = ''; |
62
|
|
|
if ($uri->scheme === 'http' && $uri->port !== 80) { |
63
|
|
|
$port = ':' . $uri->port; |
64
|
|
|
} |
65
|
|
|
$url = $scheme . '://' . $uri->host . $port . '/api/' . $this->endpoint->getUrl(); |
66
|
|
|
|
67
|
|
|
$form->addText('api_url', 'Api Url'); |
68
|
|
|
$defaults['api_url'] = $url; |
69
|
|
|
|
70
|
|
|
$form->addText('api_method', 'Method'); |
71
|
|
|
$defaults['api_method'] = $this->endpoint->getMethod(); |
72
|
|
|
|
73
|
|
|
if ($this->authorization instanceof BearerTokenAuthorization) { |
74
|
|
|
$form->addText('token', 'Token') |
75
|
|
|
->setHtmlAttribute('placeholder', 'Enter token'); |
76
|
|
|
} elseif ($this->authorization instanceof NoAuthorization) { |
77
|
|
|
$form->addText('authorization', 'Authorization') |
78
|
|
|
->setDisabled(true); |
79
|
|
|
$defaults['authorization'] = 'No authorization - global access'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$form->addCheckbox('send_session_id', 'Send session id cookie'); |
83
|
|
|
|
84
|
|
|
$form->addTextArea('custom_headers', 'Custom headers') |
85
|
|
|
->setOption('description', Html::el()->setHtml('Each header on new line. For example: <code>User-agent: Mozilla/5.0</code>')); |
86
|
|
|
|
87
|
|
|
$form->addText('timeout', 'Timeout') |
88
|
|
|
->setDefaultValue(30); |
89
|
|
|
|
90
|
|
|
$params = $this->handler->params(); |
91
|
|
|
foreach ($params as $param) { |
92
|
|
|
$param->updateConsoleForm($form); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$form->addSubmit('send', 'Try api') |
96
|
|
|
->getControlPrototype() |
97
|
|
|
->setName('button') |
98
|
|
|
->setHtml('<i class="fa fa-cloud-upload"></i> Try api'); |
99
|
|
|
|
100
|
|
|
$form->setDefaults($defaults); |
101
|
|
|
|
102
|
|
|
$form->onSuccess[] = array($this, 'formSucceeded'); |
103
|
|
|
return $form; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function formSucceeded(Form $form, ArrayHash $values): void |
107
|
|
|
{ |
108
|
|
|
$url = $values['api_url']; |
109
|
|
|
|
110
|
|
|
$token = null; |
111
|
|
|
if (isset($values['token'])) { |
112
|
|
|
$token = $values['token']; |
113
|
|
|
unset($values['token']); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$method = $values['api_method']; |
117
|
|
|
unset($values['api_method']); |
118
|
|
|
|
119
|
|
|
$additionalValues = []; |
120
|
|
|
if (isset($values['send_session_id']) && $values['send_session_id']) { |
121
|
|
|
$additionalValues['cookieFields'][session_name()] = session_id(); |
122
|
|
|
session_write_close(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (isset($values['custom_headers']) && $values['custom_headers']) { |
126
|
|
|
$additionalValues['headers'] = array_filter(array_map('trim', explode("\n", $values['custom_headers']))); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$additionalValues['timeout'] = $values['timeout']; |
130
|
|
|
|
131
|
|
|
$consoleRequest = new ConsoleRequest($this->handler); |
132
|
|
|
$result = $consoleRequest->makeRequest($url, $method, (array) $values, $additionalValues, $token); |
133
|
|
|
|
134
|
|
|
/** @var Template $template */ |
135
|
|
|
$template = $this->getTemplate(); |
136
|
|
|
$template->add('response', $result); |
137
|
|
|
|
138
|
|
|
if ($this->getPresenter()->isAjax()) { |
139
|
|
|
$this->getPresenter()->redrawControl(); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|