Avatar
Milan Gallas
Redaktor
Avatar
Milan Gallas:

Ahoj, mám problém s používáním rest api (https://github.com/drahak/Restful) s privátním klíčem.
Nastavení v configu (config.neon):

restful:
    convention: 'snake_case'
    cacheDir: '%tempDir%/cache'
    jsonpKey: 'jsonp'
    prettyPrintKey: 'pretty'
    routes:
        generateAtStart: FALSE
        prefix: resources
        module: 'RestApi'
        autoGenerated: TRUE
        panel: TRUE
    security:
        privateKey: xxxxxxxxxxx
        requestTimeKey: 'timestamp'
        requestTimeout: 300

php:
        date.timezone: Europe/Prague
        zlib.output_compression: yes

routa pro api:

$apiRouter = new RouteList('Api');
$apiRouter[] = new CrudRoute('api/v1/Api/<presenter>/<action>[/<id>]', 'Api:Application');

A samotný presenter:

<?php
        /**
         * Created by PhpStorm.
         * User: Milan Gallas
         * Date: 3.5.2016
         * Time: 16:48
         */

        namespace App\ApiModule\Presenters;

        use Drahak\Restful\Application\UI\SecuredResourcePresenter;


        /**
         * Class ApplicationPresenter
         *
         * @package App\ApiModule\Presenters
         *
         * http://apps.localhost:90/app-template/api/v1/Api/application/1
         *
         * http://localhost/nette/app-template/www/api/v1/Api/application
         *
         */
        class ApplicationPresenter extends SecuredResourcePresenter
        {
                protected $students = array(
                        array("name" => "Milan", "surname" => "Gallas", "age" => 20, "Job" => "php Programátor"),
                        array("name" => "Amdrea", "surname" => "Novotná", "age" => 17, "Job" => "java Programátor"),
                        array("name" => "Honza", "surname" => "Pulkert", "age" => 27, "Job" => "c# Programátor"),
                        array("name" => "Nikola", "surname" => "Poprachová", "age" => 23, "Job" => "php Programátor"),
                        array("name" => "Nikola", "surname" => "Poprachová", "age" => 23, "Job" => "php Programátor"),
                        array("name" => "Petr", "surname" => "KKKK", "age" => 31, "Job" => "java Programátor"),
                );

                /**
                 * @GET sample[.<type xml|json>]
                 */
                public function actionRead($type = 'json')
                {
                        $this->resource->title = 'Studenti';
                        $this->resource->Students = $this->students;
                        $this->sendResource($this->typeMap[$type]);
                }

                /**
                 * @POST sample[.<type xml|json>]
                 */
                public function actionCreate($type = 'json')
                {
                        $this->resource->title = 'Studenti';
                        //validace - nefunguje
                        //$this->input->field('name')->addRule(IValidator::MIN_LENGTH, NULL, 12)->addRule(IValidator::PATTERN, 'Please add at least one number to password', '/.*[0-9].*/');
                        $this->resource->NewStudent = $this->input;
                        $this->sendResource($this->typeMap[$type]);
                }

                /**
                 * @POST sample[.<type xml|json>]
                 */
                public function actionDelete( $type = 'json')
                {
                        $this->resource->title = 'Studenti';
                        $input = $this->getInput()->getData();

                        unset($this->students[$input['id']]);
                        $this->resource->Students = $this->students;
                        $this->sendResource($this->typeMap[$type]);
                }

                /**
                 * @PUT sample[.<type xml|json>]
                 */
                public function actionUpdate($type = 'json')
                {
                        $this->resource->title = 'Studenti';

                        $input = $this->getInput()->getData();
                        $id = $input['id'];
                        unset($input['id']);
                        $this->students[$id] = $input;

                        $this->resource->Students = $this->students;
                        $this->sendResource($this->typeMap[$type]);
                }
        }

Pokud podědím ze třídy ResourcePresenter, tak mi všechno funguje, ale komunikace není zabezpečená. Pokud podědím z SecuredResource­Presenter, tak mi to již nefunguje.

odpověď

{
  "code": 0,
  "status": "error",
  "message": "User not logged in"
}

a v posmanu volám api:
http://apps.localhost:90/…/application
s hlavičkou:
X-API-KEY xxxxxxxxxx

Nevite někdo v čem je problém? Chyba v kódu, nebo ve volání api?

 
Odpovědět 20.6.2016 15:58
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 1 zpráv z 1.