From 008b87c0c391ff5975e620c0fef19ce922409e40 Mon Sep 17 00:00:00 2001 From: NastuzziSamy Date: Wed, 5 Dec 2018 01:51:20 +0100 Subject: [PATCH] Handle custom ApiTester class, fix config cache problem --- composer.json | 6 +++++- resources/views/index.blade.php | 5 ++++- src/ApiTester.php | 27 ++++++++++++++++++++------- src/ApiTesterController.php | 3 ++- src/ApiTesterServiceProvider.php | 2 +- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index f9e6219..7a8fd0a 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "laravel-admin-ext/api-tester", + "name": "nastuzzi-samy/api-tester", "description": "Api tester for laravel", "type": "library", "keywords": ["laravel-admin", "api", "tester"], @@ -9,6 +9,10 @@ { "name": "z-song", "email": "zosong@126.com" + }, + { + "name": "NastuzziSamy", + "email": "samy@nastuzzi.fr" } ], "require": { diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php index f0347ad..b717fde 100644 --- a/resources/views/index.blade.php +++ b/resources/views/index.blade.php @@ -135,6 +135,9 @@ function renderResponse(response) { } } }, + error: function (data) { + toastr.error(data.responseJSON.message); + }, cache: false, contentType: false, processData: false @@ -371,4 +374,4 @@ function renderResponse(response) { - \ No newline at end of file + diff --git a/src/ApiTester.php b/src/ApiTester.php index f64cf50..3da3574 100644 --- a/src/ApiTester.php +++ b/src/ApiTester.php @@ -75,7 +75,7 @@ public function call($method, $uri, $parameters = [], $user = null) $symfonyRequest = SymfonyRequest::create( $uri, $method, $parameters, - [], $files, ['HTTP_ACCEPT' => 'application/json'] + [], $files, $this->getHeaders() ); $request = Request::createFromBase($symfonyRequest); @@ -91,6 +91,12 @@ public function call($method, $uri, $parameters = [], $user = null) return $response; } + protected function getHeaders() { + return [ + 'HTTP_ACCEPT' => 'application/json' + ]; + } + /** * Login a user by giving userid. * @@ -99,14 +105,21 @@ public function call($method, $uri, $parameters = [], $user = null) protected function loginUsing($userId) { $guard = static::config('guard', 'api'); - - if ($method = static::config('user_retriever')) { - $user = call_user_func($method, $userId); - } else { - $user = app('auth')->guard($guard)->getProvider()->retrieveById($userId); - } + $user = $this->getUser($guard, $userId); $this->app['auth']->guard($guard)->setUser($user); + + return $user; + } + + /** + * Get a user by giving userid. + * + * @param $userId + */ + protected function getUser($guard, $userId) + { + return app('auth')->guard($guard)->getProvider()->retrieveById($userId); } /** diff --git a/src/ApiTesterController.php b/src/ApiTesterController.php index 06af606..cfaabea 100644 --- a/src/ApiTesterController.php +++ b/src/ApiTesterController.php @@ -46,7 +46,8 @@ public function handle(Request $request) return $key !== ''; }, ARRAY_FILTER_USE_KEY); - $tester = new ApiTester(); + $class = config('admin.extensions.api-tester.class', ApiTest::class); + $tester = new $class(); $response = $tester->call($method, $uri, $parameters, $user); diff --git a/src/ApiTesterServiceProvider.php b/src/ApiTesterServiceProvider.php index b4b6143..103733e 100644 --- a/src/ApiTesterServiceProvider.php +++ b/src/ApiTesterServiceProvider.php @@ -17,6 +17,6 @@ public function boot() ); } - ApiTester::boot(); + config('admin.extensions.api-tester.class', ApiTest::class)::boot(); } }