Preskočiť na obsah

Webová konzola ​

Dva Nette komponenty vám umožnia preklikať sa API: ApiListingControl vypíše endpointy a ApiConsoleControl vykreslí formulár pre jeden z nich, pošle požiadavku cez cURL a ukáže odpoveď.

Nie do produkcie

Konzola vie zavolať každý endpoint s ľubovoľným tokenom, ktorý napíšete. Presenter chráňte, alebo ho v produkcii vynechajte.

Presenter ​

php
<?php

declare(strict_types=1);

namespace App\UI\Console;

use Nette\Application\UI\Presenter;
use Tomaj\NetteApi\ApiDecider;
use Tomaj\NetteApi\Component\ApiConsoleControl;
use Tomaj\NetteApi\Component\ApiListingControl;
use Tomaj\NetteApi\Link\ApiLink;

/**
 * The web console: a list of all endpoints and a generated form to call one.
 * Protect it (or leave it out) in production, it can call every endpoint.
 */
final class ConsolePresenter extends Presenter
{
    private string $method = '';
    private string $version = '';
    private string $package = '';
    private ?string $apiAction = null;

    public function __construct(private ApiDecider $apiDecider, private ApiLink $apiLink)
    {
        parent::__construct();
    }

    public function actionShow(string $method, string $version, string $package, ?string $apiAction = null): void
    {
        $this->method = $method;
        $this->version = $version;
        $this->package = $package;
        $this->apiAction = $apiAction;
        $this->template->endpoint = $this->apiDecider->getApi($method, $version, $package, $apiAction)->getEndpoint();
    }

    protected function createComponentApiListing(): ApiListingControl
    {
        $listing = new ApiListingControl($this->apiDecider);
        $listing->onClick[] = function (string $method, string $version, string $package, ?string $apiAction): void {
            $this->redirect('show', $method, $version, $package, $apiAction);
        };
        return $listing;
    }

    protected function createComponentApiConsole(): ApiConsoleControl
    {
        $api = $this->apiDecider->getApi($this->method, $this->version, $this->package, $this->apiAction);
        return new ApiConsoleControl(
            $this->getHttpRequest(),
            $api->getEndpoint(),
            $api->getHandler(),
            $api->getAuthorization(),
            $this->apiLink,
        );
    }
}

Pohľad default (default.latte) vykreslí zoznam endpointov. Klik na endpoint presmeruje na actionShow, ktorá vykreslí show.latte s formulárom konzoly pre daný endpoint.

latte
{block content}
<h1>API console</h1>
<p>
    OpenAPI document: <a href="{apiLink 'GET', '1', 'openapi'}">JSON</a>,
    <a href="{apiLink 'GET', '1', 'openapi', null, ['format' => 'yaml']}">YAML</a>
</p>
{control apiListing}
{/block}
latte
{block content}
<p><a n:href="default">All endpoints</a></p>
<h1>{$endpoint->getMethod()} /{$endpoint->getUrl()}</h1>
{control apiConsole}
{/block}

Komponenty ​

KomponentKonštruktorPoznámky
ApiListingControl(ApiDecider)onClick[] dostane method, version, package, apiAction
ApiConsoleControl(IRequest, EndpointInterface, ApiHandlerInterface, ApiAuthorizationInterface, ?ApiLink, ?ApiConsoleFormFactoryInterface)setTemplateFilePath(), setFormRenderer()

Formulár má jedno pole pre každý parameter, pole pre token alebo API kľúč (alebo meno a heslo pre Basic) a timeout. Ak ho chcete zmeniť, napíšte vlastný ApiConsoleFormFactoryInterface.

Časté otázky ​

Prečo odoslanie formulára zlyhá s chybou CSRF? ​

Nette formuláre prijmú POST len z tej istej stránky, s cookie _nss. Konzolu otvárajte v prehliadači, nie skriptom bez cookies.

Môže konzola volať iný server? ​

Volá URL vo formulári, predvolene endpoint na aktuálnom hoste.

Ako zmením vzhľad? ​

Použite setTemplateFilePath() s vlastnou Latte šablónou, alebo setFormRenderer().

Ukáže konzola požiadavku, ktorú poslala? ​

Áno, panel odpovede ukazuje URL, hlavičky, telo a čas.

Potrebujem konzolu, keď mám OpenAPI? ​

Nie, sú nezávislé. OpenAPI funguje s akýmkoľvek OpenAPI nástrojom.

Vydané pod licenciou MIT.