diff --git a/CHANGELOG.md b/CHANGELOG.md index e6d8290..2f6f9fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,12 +10,14 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip * Added custom headers to API console * Added field for timeout to API console * OpenAPI handler +* Information about RESTful urls #### Fixed * Fixed sending empty string in multi params * UrlEncoding values sending through get param inputs * Fixed static url part `/api/` in console +* Fixed generating urls in console for RESTful urls using ApiLink and EndpointInterface ## 2.0.1 - 2020-03-24 diff --git a/README.md b/README.md index 798a729..58681cd 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,20 @@ And add route to you RouterFactory: $router[] = new Route('/api/v/[/][/]', 'Api:Api:default'); ``` +If you want to use RESTful urls you will need another route: +```php +$router[] = new Route('api/v//', [ + 'presenter' => 'Api:Api', + 'action' => 'default', + 'id' => [ + Route::FILTER_IN => function ($id) { + $_GET['id'] = $id; + return $id; + } + ], +]); +``` + After that you need only register your API handlers to *apiDecider* [ApiDecider](src/ApiDecider.php), register [ApiLink](src/Link/ApiLink.php) and [Tomaj\NetteApi\Misc\IpDetector](src/Misc/IpDetector.php). This can be done also with *config.neon*: ```neon @@ -73,7 +87,7 @@ Core of the Nette-Api are handlers. For this example you need to implement two c 2. App\MyApi\v1\Handlers\SendEmailHandler These handlers implement interface *[ApiHandlerInterface](src/Handlers/ApiHandlerInterface.php)* but for easier usage you can extend your handlers from [BaseHandler](src/Handlers/BaseHandler.php). -When someone reach your API these handlers will be triggered and *handle()* method will be called. +When someone reach your API, these handlers will be triggered and *handle()* method will be called. ```php namespace App\MyApi\v1\Handlers; diff --git a/src/Component/ApiConsoleControl.php b/src/Component/ApiConsoleControl.php index 9f01d72..56b4f14 100644 --- a/src/Component/ApiConsoleControl.php +++ b/src/Component/ApiConsoleControl.php @@ -56,7 +56,7 @@ protected function createComponentConsoleForm(): Form $defaults = []; $form->setRenderer(new BootstrapRenderer()); - + if ($this->apiLink) { $url = $this->apiLink->link($this->endpoint); } else { @@ -136,7 +136,7 @@ public function formSucceeded(Form $form, ArrayHash $values): void $additionalValues['timeout'] = $values['timeout']; - $consoleRequest = new ConsoleRequest($this->handler); + $consoleRequest = new ConsoleRequest($this->handler, $this->endpoint, $this->apiLink); $result = $consoleRequest->makeRequest($url, $method, (array) $values, $additionalValues, $token); /** @var Template $template */ diff --git a/src/Misc/ConsoleRequest.php b/src/Misc/ConsoleRequest.php index 94bb453..0e6b6ea 100644 --- a/src/Misc/ConsoleRequest.php +++ b/src/Misc/ConsoleRequest.php @@ -5,7 +5,9 @@ namespace Tomaj\NetteApi\Misc; use Nette\Http\FileUpload; +use Tomaj\NetteApi\EndpointInterface; use Tomaj\NetteApi\Handlers\ApiHandlerInterface; +use Tomaj\NetteApi\Link\ApiLink; use Tomaj\NetteApi\Params\InputParam; use Tomaj\NetteApi\Params\ParamInterface; @@ -14,9 +16,17 @@ class ConsoleRequest /** @var ApiHandlerInterface */ private $handler; - public function __construct(ApiHandlerInterface $handler) + /** @var EndpointInterface|null */ + private $endpoint; + + /** @var ApiLink|null */ + private $apiLink; + + public function __construct(ApiHandlerInterface $handler, ?EndpointInterface $endpoint = null, ?ApiLink $apiLink = null) { $this->handler = $handler; + $this->endpoint = $endpoint; + $this->apiLink = $apiLink; } public function makeRequest(string $url, string $method, array $values, array $additionalValues = [], ?string $token = null): ConsoleResponse @@ -32,7 +42,9 @@ public function makeRequest(string $url, string $method, array $values, array $a $getFields = $this->normalizeValues($getFields); $putFields = $this->normalizeValues($putFields); - if (count($getFields)) { + if ($this->endpoint && $this->apiLink) { + $url = $this->apiLink->link($this->endpoint, $getFields); + } elseif (count($getFields)) { $parts = []; foreach ($getFields as $key => $value) { $parts[] = "$key=$value";