Код: Выделить всё
$httpClient = Http::withBasicAuth($this->apiKey, $this->apiSecret)
->retry(3,100,null,false)
->withRequestMiddleware(function (RequestInterface $request) {
$identifier = $request->getHeaderLine('X-RequestId');
$attempt = $request->getAttribute('retry_attempts',1);
$authMethod = $request->getHeaderLine('Authorization')??null;
$authMethod = is_string($authMethod)?preg_replace("/\s*(\w+)\s+.*/","\$1",$authMethod):null;
DB::table('api_call_log')->insert([
'identifier'=>$identifier,
'attempt'=>$attempt,
'method'=>$request->getMethod(),
'url' => $request->getUri(),
'request_auth_method'=>$authMethod,
'headers' => serialize($request->getHeaders()),
'body' => serialize($request->getBody()),
'request_start_unix' => now()->unix(),
]);
return $request;
})
->withResponseMiddleware(function(ResponseInterface $response) {
// How I can get requestId and attempt here
});
}
Код: Выделить всё
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::dropIfExists('api_call_log');
Schema::create('api_call_log', function (Blueprint $table) {
$table->string('identifier', 32);
$table->integer('attempt')->default(1)->unsigned();
$table->primary(['identifier','attempt']);
$table->string('method', 32);
$table->string('url', 32);
$table->string('request_auth_method', 32)->nullable();
$table->binary('headers')->nullable();
$table->binary('body')->nullable();
$table->integer('request_start_unix')->unsigned();
$table->integer('request_end_unix')->unsigned()->nullable();
$table->smallInteger('response_code')->unsigned()->nullable();
$table->binary('response_body')->nullable();
$table->binary('response_headers')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('api_call_log');
}
};
Код: Выделить всё
$requestId = Str::uuid()->toString();
$endpoint = $this->apiUrl.'/myendpoint';
$response = $this->httpClient->withHeaders(['X-RequestId'=>$requestId])
->put($endpoint,['name'=>"Hello"]);
Вопрос в том, как Я могу связать необходимый запрос с моим ответом при закрытии withResponseMiddleware?
Подробнее здесь: https://stackoverflow.com/questions/790 ... middleware
Мобильная версия