From 11d6e96701dadd4d958a93050130468adbe57d72 Mon Sep 17 00:00:00 2001 From: Kovah Date: Mon, 2 May 2022 20:28:40 +0200 Subject: [PATCH] Refactor http request setup into single function --- app/Console/Commands/CheckLinksCommand.php | 6 +----- app/Helper/UpdateHelper.php | 6 +----- app/Helper/WaybackMachine.php | 7 +------ app/Helper/functions.php | 21 +++++++++++++++++++-- app/Http/Controllers/FetchController.php | 6 +----- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/app/Console/Commands/CheckLinksCommand.php b/app/Console/Commands/CheckLinksCommand.php index 3dd99d64..dfd172ee 100644 --- a/app/Console/Commands/CheckLinksCommand.php +++ b/app/Console/Commands/CheckLinksCommand.php @@ -136,11 +136,7 @@ class CheckLinksCommand extends Command } try { - $request = Http::timeout(20); - if (config('html-meta.user_agents', false)) { - $agents = config('html-meta.user_agents'); - $request->withHeaders(['User-Agent' => $agents[array_rand($agents)]]); - } + $request = setupHttpRequest(20); $response = $request->head($link->url); $statusCode = $response->status(); } catch (Exception $e) { diff --git a/app/Helper/UpdateHelper.php b/app/Helper/UpdateHelper.php index 36d7a071..0ecb41f2 100644 --- a/app/Helper/UpdateHelper.php +++ b/app/Helper/UpdateHelper.php @@ -66,11 +66,7 @@ class UpdateHelper */ protected static function getCurrentVersionFromAPI(): ?string { - $request = Http::timeout(5); - if (config('html-meta.user_agents', false)) { - $agents = config('html-meta.user_agents'); - $request->withHeaders(['User-Agent' => $agents[array_rand($agents)]]); - } + $request = setupHttpRequest(5); $response = $request->get(self::RELEASE_API_URL); return $response->successful() ? $response->body() : null; diff --git a/app/Helper/WaybackMachine.php b/app/Helper/WaybackMachine.php index 936db02d..f15ff243 100644 --- a/app/Helper/WaybackMachine.php +++ b/app/Helper/WaybackMachine.php @@ -2,7 +2,6 @@ namespace App\Helper; -use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; class WaybackMachine @@ -24,11 +23,7 @@ class WaybackMachine $archiveUrl = self::$baseUrl . '/save/' . $url; - $request = Http::timeout(10); - if (config('html-meta.user_agents', false)) { - $agents = config('html-meta.user_agents'); - $request->withHeaders(['User-Agent' => $agents[array_rand($agents)]]); - } + $request = setupHttpRequest(); $response = $request->head($archiveUrl); try { diff --git a/app/Helper/functions.php b/app/Helper/functions.php index a22c0f7f..38046c3b 100644 --- a/app/Helper/functions.php +++ b/app/Helper/functions.php @@ -4,12 +4,12 @@ use App\Helper\Sharing; use App\Helper\WaybackMachine; use App\Models\Link; use App\Models\Setting; +use Illuminate\Http\Client\PendingRequest; use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; /** * Check if the setup was completed. @@ -209,3 +209,20 @@ function escapeSearchQuery(string $query): string $query ); } + +/** + * Set up an HTTP request with a random user agent + * + * @param int $timeout + * @return PendingRequest + */ +function setupHttpRequest(int $timeout = 10): PendingRequest +{ + $request = Http::timeout($timeout); + if (config('html-meta.user_agents', false)) { + $agents = config('html-meta.user_agents'); + $request->withHeaders(['User-Agent' => $agents[array_rand($agents)]]); + } + + return $request; +} diff --git a/app/Http/Controllers/FetchController.php b/app/Http/Controllers/FetchController.php index c6f3dfb5..76644d60 100644 --- a/app/Http/Controllers/FetchController.php +++ b/app/Http/Controllers/FetchController.php @@ -126,11 +126,7 @@ class FetchController extends Controller ]); $url = $request->input('url'); - $newRequest = Http::timeout(3); - if (config('html-meta.user_agents', false)) { - $agents = config('html-meta.user_agents'); - $newRequest->withHeaders(['User-Agent' => $agents[array_rand($agents)]]); - } + $newRequest = setupHttpRequest(3); $response = $newRequest->get($url); if ($response->successful()) {