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