Skip to content

Commit

Permalink
Merge pull request #82 from tomaj/restfull-urls
Browse files Browse the repository at this point in the history
RESTful urls info + console fix
  • Loading branch information
tomaj authored May 12, 2020
2 parents e2f458f + 3f7c849 commit 6df8ee0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ And add route to you RouterFactory:
$router[] = new Route('/api/v<version>/<package>[/<apiAction>][/<params>]', 'Api:Api:default');
```

If you want to use RESTful urls you will need another route:
```php
$router[] = new Route('api/v<version>/<package>/<id>', [
'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
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Component/ApiConsoleControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function createComponentConsoleForm(): Form
$defaults = [];

$form->setRenderer(new BootstrapRenderer());

if ($this->apiLink) {
$url = $this->apiLink->link($this->endpoint);
} else {
Expand Down Expand Up @@ -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 */
Expand Down
16 changes: 14 additions & 2 deletions src/Misc/ConsoleRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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";
Expand Down

0 comments on commit 6df8ee0

Please sign in to comment.