如何在Controller中的返回后的特定事件中调用API,Laravel Lumen?

huangapple go评论104阅读模式
英文:

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

问题

我们正在使用Laravel Lumen实现一个系统:

完整的流程图:
https://drive.google.com/file/d/1rFdkUwUaTQ4DGQaxXveu6CFylg1o1oLN/view?usp=sharing

  1. 假设客户端调用了我们在服务器端定义的POST API 1,并且必须在服务器端使用POST API 1的响应。[注意:在这个POST API 1事件上,我们必须自动触发另一个POST API 2,但在返回202之后触发。][客户端将调用服务器API] (https://i.stack.imgur.com/FrSQ7.png)

  2. 现在作为服务器,我们必须以响应的形式向客户端发送一个确认,就像是“是的,我们收到了你的API请求”。以代码形式,我们将返回状态码=202,消息=已接受POST API 1的请求。服务器将向客户端发送确认

  3. 现在POST API 1返回202,因此POST API 2必须在服务器端触发,在请求体中,服务器将使用POST API 1的响应数据。服务器通过POST API 1调用了客户端的POST API 2

  4. 客户端也将与服务器一样操作,他也会发送状态码202的确认,并附带消息“请求已接受”。客户端向服务器发送确认

流程到此结束。

api.php 文件

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

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| 这里是您可以为应用程序注册所有路由的地方。
| 这很简单。只需告诉Lumen它应该响应的URI
| 并提供在请求该URI时要调用的闭包。
|
*/

// 客户端将调用此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();
    // 这里我们使用了事件、队列、任务,但没有起作用,我向您展示了其中的一个示例
    Queue::push(new Job($data));
    return response()->json(['message' => '请求已接受'], 202);
}

public function API2(){
    // 服务器将调用客户端API,客户端将返回202响应
}

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 ---> 监听器

<?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);
    }
}

问题:
尝试按照所述操作,但API2首先被调用,然后返回202状态给API1。

期望:
希望API1首先返回202,然后由服务器调用客户端的API2。

英文:

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
>
>
&gt; &lt;?php
&gt; /** @var \Laravel\Lumen\Routing\Router $router */
&gt;
&gt; /*
&gt; |--------------------------------------------------------------------------
&gt; | Application Routes
&gt; |--------------------------------------------------------------------------
&gt; |
&gt; | Here is where you can register all of the routes for an application.
&gt; | It is a breeze. Simply tell Lumen the URIs it should respond to
&gt; | and give it the Closure to call when that URI is requested.
&gt; |
&gt; */
&gt;
&gt; // clients will call this API
&gt; $router-&gt;group([&#39;prefix&#39; =&gt; &#39;api&#39;], function () use ($router) {
&gt; $router-&gt;post(&#39;API1&#39;, [&#39;uses&#39; =&gt; &#39;HIP\GatewayApi@api1&#39;]);
&gt; });
&gt;

> GatewayApi.php
>
>
&gt; namespace App\Http\Controllers\HIP;
&gt; use Illuminate\Support\Facades\Request;
&gt; use Illuminate\Support\Facades\Queue;
&gt; use App\Jobs\Job;
&gt;
&gt; public function api1(){
&gt; $data = Request::all();
&gt; // here we use event, queues, jobs, but didnt work, i m showing you one of the example of it
&gt; Queue::push(new Job($data));
&gt; return response()-&gt;json([&#39;message&#39; =&gt; &#39;Request Accepted&#39;], 202);
&gt; }
&gt;
&gt; public function API2(){
&gt; // server will call the client api and client will return 202 reponse
&gt; }
&gt;

> Job.php
>
>
&gt; &lt;?php
&gt;
&gt; namespace App\Jobs;
&gt;
&gt; use App\Http\Controllers\HIP\GatewayApi;
&gt; use Illuminate\Contracts\Queue\ShouldQueue;
&gt; use Illuminate\Queue\InteractsWithQueue;
&gt; use Illuminate\Queue\SerializesModels;
&gt; use Log;
&gt; use App\Events\OnCallBackApi;
&gt;
&gt; class ProcessAbhaCallback implements ShouldQueue
&gt; {
&gt; use InteractsWithQueue, SerializesModels;
&gt;
&gt; protected $data;
&gt;
&gt; /**
&gt; * Create a new job instance.
&gt; *
&gt; * @param array $data
&gt; * @return void
&gt; */
&gt; public function __construct(array $data)
&gt; {
&gt; $this-&gt;data = $data;
&gt; }
&gt;
&gt; /**
&gt; * Execute the job.
&gt; *
&gt; * @return void
&gt; */
&gt; public function handle()
&gt; {
&gt; event(new OnCallBackApi($this-&gt;data));
&gt; }
&gt; }
&gt;

> OnCallBackApi.php
>
>
&gt; &lt;?php
&gt;
&gt; namespace App\Events;
&gt; class OnCallBackApi extends Event
&gt; {
&gt; /**
&gt; * Create a new event instance.
&gt; *
&gt; * @return void
&gt; */
&gt;
&gt; public $event;
&gt; public function __construct($event)
&gt; {
&gt; $this-&gt;event = $event;
&gt; }
&gt; }
&gt;

> SendOnCallBackApi.php ---&gt; Listener
>
>
&gt; &lt;?php
&gt;
&gt; namespace App\Listeners;
&gt;
&gt; use App\Events\OnCallBackApi;
&gt; use App\Http\Controllers\HIP\GatewayApi;
&gt; use Log;
&gt;
&gt; class SendOnCallBackApi
&gt; {
&gt; /**
&gt; * Create the event listener.
&gt; *
&gt; * @return void
&gt; */
&gt; public function __construct()
&gt; {
&gt; //
&gt; }
&gt;
&gt; /**
&gt; * Handle the event.
&gt; *
&gt; * @param OnCallBackApi $event
&gt; * @return void
&gt; */
&gt; public function handle(OnCallBackApi $event)
&gt; {
&gt; $api = new GatewayApi();
&gt; $api-&gt;API2($data);
&gt; }
&gt; }
&gt;

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

答案1

得分: 0

你可以发送api1的响应202,然后像这样发送api2的响应:

public function api1(){
    $data = Request::all();
    //将api1的响应发送给客户端。
    response()->json(['message' => '请求已接受'], 202)->send();
    //发送api2的响应
    Queue::push(new Job($data));
}
英文:

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()-&gt;json([&#39;message&#39; =&gt; &#39;Request Accepted&#39;], 202)-&gt;send();
    //send the api2 response
    Queue::push(new Job($data));
}

huangapple
  • 本文由 发表于 2023年8月10日 19:19:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定