Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle custom ApiTester class, fix config cache problem #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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"],
Expand All @@ -9,6 +9,10 @@
{
"name": "z-song",
"email": "[email protected]"
},
{
"name": "NastuzziSamy",
"email": "[email protected]"
}
],
"require": {
Expand Down
5 changes: 4 additions & 1 deletion resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ function renderResponse(response) {
}
}
},
error: function (data) {
toastr.error(data.responseJSON.message);
},
cache: false,
contentType: false,
processData: false
Expand Down Expand Up @@ -371,4 +374,4 @@ function renderResponse(response) {
</div>
</div>

</template>
</template>
27 changes: 20 additions & 7 deletions src/ApiTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.
*
Expand All @@ -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);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/ApiTesterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/ApiTesterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function boot()
);
}

ApiTester::boot();
config('admin.extensions.api-tester.class', ApiTest::class)::boot();
}
}