mirror of
https://github.com/Kovah/LinkAce.git
synced 2025-03-20 06:39:38 +01:00
Refactor http request setup into single function
This commit is contained in:
parent
931859adc5
commit
11d6e96701
@ -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) {
|
||||
|
@ -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;
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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()) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user