Implement webhook inspector
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\CaptureWebhookRequestAction;
|
||||
use App\Actions\CreateAnonymousEndpointAction;
|
||||
use App\Models\WebhookEndpoint;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
class EndpointController extends Controller
|
||||
{
|
||||
public function storeAnonymous(CreateAnonymousEndpointAction $createAnonymousEndpoint): RedirectResponse
|
||||
{
|
||||
$endpoint = $createAnonymousEndpoint->handle();
|
||||
|
||||
return redirect()->route('inspect.show', ['token' => $endpoint->token]);
|
||||
}
|
||||
|
||||
public function receive(
|
||||
Request $request,
|
||||
string $token,
|
||||
CaptureWebhookRequestAction $captureWebhookRequest,
|
||||
): Response|JsonResponse {
|
||||
$endpoint = WebhookEndpoint::findByToken($token);
|
||||
|
||||
abort_unless($endpoint instanceof WebhookEndpoint && $endpoint->acceptsRequests(), 404);
|
||||
|
||||
if (strlen($request->getContent()) > (int) config('webhooks.max_body_bytes', 1024 * 1024)) {
|
||||
return response()->json(['message' => 'The webhook request body is too large.'], 413);
|
||||
}
|
||||
|
||||
$captureWebhookRequest->handle($endpoint, $request);
|
||||
$endpoint->refresh();
|
||||
|
||||
return response(
|
||||
$endpoint->response_body ?? '{}',
|
||||
$endpoint->response_status,
|
||||
$endpoint->response_headers ?? ['Content-Type' => 'application/json'],
|
||||
);
|
||||
}
|
||||
|
||||
public function destroy(string $token): RedirectResponse
|
||||
{
|
||||
$endpoint = WebhookEndpoint::findByToken($token);
|
||||
|
||||
abort_unless($endpoint instanceof WebhookEndpoint, 404);
|
||||
Gate::authorize('delete', $endpoint);
|
||||
$endpoint->delete();
|
||||
|
||||
return redirect()->route('home')->with('status', 'Endpoint deleted.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user