Skip to content

Commit

Permalink
Merge pull request #83 from tomaj/open-api
Browse files Browse the repository at this point in the history
Open api + RESTful urls info
  • Loading branch information
lulco authored May 12, 2020
2 parents fbf5661 + 6df8ee0 commit 079bfa5
Show file tree
Hide file tree
Showing 13 changed files with 591 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
* Added API rate limit
* 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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"nette/di": "^3.0",
"latte/latte": "^2.4",
"phpunit/phpunit": ">7.0",
"symfony/yaml": "^4.4|5.0",
"squizlabs/php_codesniffer": "^3.2"
},
"autoload": {
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
3 changes: 2 additions & 1 deletion src/Handlers/DefaultHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tomaj\NetteApi\Handlers;

use Nette\Http\IResponse;
use Tomaj\NetteApi\Response\JsonApiResponse;
use Tomaj\NetteApi\Response\ResponseInterface;

Expand All @@ -14,6 +15,6 @@ class DefaultHandler extends BaseHandler
*/
public function handle(array $params): ResponseInterface
{
return new JsonApiResponse(500, ['status' => 'error', 'message' => 'Unknown api endpoint']);
return new JsonApiResponse(IResponse::S400_BAD_REQUEST, ['status' => 'error', 'message' => 'Unknown api endpoint']);
}
}
Loading

0 comments on commit 079bfa5

Please sign in to comment.