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