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

Make a request to one endpoint from another controller #634

Open
hymenoby opened this issue Jun 3, 2022 · 5 comments
Open

Make a request to one endpoint from another controller #634

hymenoby opened this issue Jun 3, 2022 · 5 comments

Comments

@hymenoby
Copy link

hymenoby commented Jun 3, 2022

Hello,
How do i make a request to one resource endpoint without using an http client?
Here is my problem I have create a controller to update the current user informations. I have a users ressource configured. When i try to create a request like this and pass it to the app()->handle() functions the request fails with a 500 error :

$request = Request::create(
'/api/v1/users/1',
'POST',
$data,
);
$response = app()->handle($request);

is there a way to call a ressource controller action without using an $httpClient?

@lindyhopchris
Copy link
Member

Hi! I wouldn't ever recommend creating a new request within an existing Laravel request, as Laravel assumes it is only handling one request at once.

Why don't you just update the user model yourself? Why do you have to pass it off as a JSON:API process?

@hymenoby
Copy link
Author

hymenoby commented Jun 3, 2022

@lindyhopchris
Okay, thanks for your reply.

I want to reuse the api endpoints. And i want also to benefit of the already implemented validations and rules in place in this api endpoint.

I already used this logic for the passport api without trouble like this:

<?php

namespace App\Http\Controllers\Api\V1\Auth;

use Illuminate\Support\Facades\DB;
use App\Http\Requests\Api\V1\Auth\LoginRequest;
use CloudCreativity\LaravelJsonApi\Document\Error\Error;
use CloudCreativity\LaravelJsonApi\Http\Controllers\JsonApiController;
use Illuminate\Support\Facades\Request;

class LoginController extends JsonApiController
{
    /**
     * Handle the incoming request.
     *
     * @param LoginRequest $request
     * @return mixed
     */
    public function __invoke(LoginRequest $request)
    {
        $client = DB::table('oauth_clients')->where('password_client', 1)->first();

        $oauthRequest = Request::create(
            route('passport.token', [], false),
            'POST',
            [
                'grant_type' => 'password',
                'client_id' => $client->id,
                'client_secret' => $client->secret,
                'username' => $request->email,
                'password' => $request->password,
                'scope' => '',
            ],
        );
        $response = app()->handle($oauthRequest);

        if ($response->status() == 200) {
            return json_decode((string)$response->getContent(), true);
        } else {
            $error = json_decode((string)$response->getContent(), true);
            return $this->reply()->errors([
                Error::fromArray([
                    'title' => 'Bad Request',
                    'detail' => $error['message'],
                    'status' => '400',
                ])
            ]);
        }
    }
}
```

@lindyhopchris
Copy link
Member

Sending a request within another request is just not something I'd recommend doing. Using the HTTP client would seem like a lot better idea.

I think this shows a need for a developer being able to execute a JSON:API "operation" wherever they want. This is something I'd support in a future version, as I need to separate out the operations to support the Atomic Operations extension. However, at the moment that's not something that's supportable in the current codebase as the operations are coupled to the HTTP requests due to using form request classes.

@hymenoby
Copy link
Author

hymenoby commented Jun 8, 2022

Okay thanks, i will go with the http client then.
Thanks

@lindyhopchris
Copy link
Member

I'm going to leave this open as a reminder that I need to make it easier for developers to manually trigger a JSON:API operation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants