cloudcreativity / laravel-json-api

JSON API (jsonapi.org) package for Laravel applications.
http://laravel-json-api.readthedocs.io/en/latest/
Apache License 2.0
778 stars 109 forks source link

Make a request to one endpoint from another controller #634

Open hymenoby opened 2 years ago

hymenoby commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

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 commented 2 years ago

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

lindyhopchris commented 2 years ago

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.