Search code examples
phplaravellaravel-8lumen

How to call API at particular event after return in Controller, Laravel Lumen?


We implementing a system in Laravel Lumen:

complete flow Image: https://drive.google.com/file/d/1rFdkUwUaTQ4DGQaxXveu6CFylg1o1oLN/view?usp=sharing

  1. assume the client hit the POST API 1 that we defined at our server end and has to use that POST API 1 response at the server end. [Note: on this POST API 1 event we have to automatically trigger another POST API 2 but after return 202.] the client will call the server APIs

  2. Now as a server, we have to send the client as response a response as acknowledgment, like Yes we got your API hit. In the form of code, we will return status code = 202 with the message = Request Accepted for POST API 1. server will send an acknowledgment to the client

  3. now POST API 1 returns 202, so POST API 2 will have to trigger at the server end, in body server will use POST API 1 response data server called the POST API 2 of the client with the help of POST API 1

  4. the client will do the same as the server, he also sends acknowledgment with the status 202, with the message request accepted client acknowledgment to the server

Here the flow is completed.

api.php file

<?php
/** @var \Laravel\Lumen\Routing\Router $router */

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/

// clients will call this API
$router->group(['prefix' => 'api'], function () use ($router) {
    $router->post('API1', ['uses' => 'HIP\GatewayApi@api1']);
});

GatewayApi.php

namespace App\Http\Controllers\HIP;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Queue;
use App\Jobs\Job;

public function api1(){
    $data = Request::all();
    // here we use event, queues, jobs, but didnt work, i m showing you one of the example of it
    Queue::push(new Job($data));
    return response()->json(['message' => 'Request Accepted'], 202);
}

public function API2(){
    // server will call the client api and client will return 202 reponse
}

Job.php

<?php

namespace App\Jobs;

use App\Http\Controllers\HIP\GatewayApi;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Log;
use App\Events\OnCallBackApi;

class ProcessAbhaCallback implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $data;

    /**
     * Create a new job instance.
     *
     * @param  array  $data
     * @return void
     */
    public function __construct(array $data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
         event(new OnCallBackApi($this->data));        
    }
}

OnCallBackApi.php

<?php

namespace App\Events;
class OnCallBackApi extends Event
{
    /**
     * Create a new event instance.
     *
     * @return void
     */

    public $event;
    public function __construct($event)
    {
        $this->event = $event;  
    }
}

SendOnCallBackApi.php ---> Listener

<?php

namespace App\Listeners;

use App\Events\OnCallBackApi;
use App\Http\Controllers\HIP\GatewayApi;
use Log;

class SendOnCallBackApi
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  OnCallBackApi  $event
     * @return void
     */
    public function handle(OnCallBackApi $event)
    {
        $api = new GatewayApi();
        $api->API2($data);
    }
}

issue

so try to do the same as mentioned, but the API2 is hit first, and then the 202 status returns for API 1.

Excepting:

want API 1 returns first with 202 and then API 2 needs to be hit by the server to the client


Solution

  • you can send the api1 response 202 and send after that the api2 response like this :

    public function api1(){
        $data = Request::all();
        //send the api1 response to client.
        response()->json(['message' => 'Request Accepted'], 202)->send();
        //send the api2 response
        Queue::push(new Job($data));
    }