英文:
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
-
假设客户端调用了我们在服务器端定义的POST API 1,并且必须在服务器端使用POST API 1的响应。[注意:在这个POST API 1事件上,我们必须自动触发另一个POST API 2,但在返回202之后触发。][客户端将调用服务器API] (https://i.stack.imgur.com/FrSQ7.png)
-
现在作为服务器,我们必须以响应的形式向客户端发送一个确认,就像是“是的,我们收到了你的API请求”。以代码形式,我们将返回状态码=202,消息=已接受POST API 1的请求。服务器将向客户端发送确认
-
现在POST API 1返回202,因此POST API 2必须在服务器端触发,在请求体中,服务器将使用POST API 1的响应数据。服务器通过POST API 1调用了客户端的POST API 2
-
客户端也将与服务器一样操作,他也会发送状态码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
-
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
-
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
-
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
-
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
答案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()->json(['message' => 'Request Accepted'], 202)->send();
//send the api2 response
Queue::push(new Job($data));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论