Home > Blockchain >  Laravel http facade conditionally add methods
Laravel http facade conditionally add methods

Time:05-23

I'm working in Laravel 8 and am using the Http facade client to make requests. Currently, my set up has a database table where I store a auth_type enum column with the different auth types, I also have a method column stored as an enum.

With a Laravel model I know you can do Model::query(); and append methods dynamically, but can this be done with the Http facade as my code is extremely long, covering an if statement for each method, and in each method is more if statements for the auth types and it seems like there could be a cleaner solution to my working code:

if ($monitor->method == 'get') {
    if ($monitor->auth_type == 'basic') {
        return Http::withBasicAuth($monitor->username, $monitor->password)
                   ->withHeaders($headers)
                   ->timeout($timeout)
                   ->get($monitor->url);
    }
    if ($monitor->auth_type == 'digest') {
        return Http::withDigestAuth($monitor->username, $monitor->password)
                   ->withHeaders($headers)
                   ->timeout($timeout)
                   ->get($monitor->url);
    }
    if ($monitor->auth_type == 'token') {
        return Http::withToken($monitor->password)
                   ->withHeaders($headers)
                   ->timeout($timeout)
                   ->get($monitor->url);
    }

    return Http::withHeaders($headers)
               ->timeout($timeout)
               ->get($monitor->url);
}

CodePudding user response:

Http uses Illuminate\Http\Client\Factory & Illuminate\Http\Client\PendingRequest.

While Guzzle would actually take the HTTP verb as first argument:

// just for example.
$options = [
    RequestOptions::HEADERS => $headers,
    RequestOptions::JSON => $post_fields
];

$client = new GuzzleHttp\Client();
return $client->request(strtoupper($monitor->method), $monitor->url, $options);

Maybe store the string as uppercase?!

CodePudding user response:

Did you tried something like this?

Edit 01: there's no need to return in if statements, since I've forgot to remove them when i was copying them in other statements.

$http = Http::withHeaders([
    'content-type'  =>  'application-json',
    'accept'  =>  'application-json',
]);
return $http->withBasicAuth('likeUser', 'password')
    ->timeout(3000)
    ->get('google.com');

I've done something like this and worked, and now base on what you've asked, you can do something like below :

if ($monitor->method == 'get') {
    $http = Http::withHeaders($headers)
        ->timeout($timeout);
    if ($monitor->auth_type == 'basic') {
        $http->withBasicAuth($monitor->username, $monitor->password);
    }
    if ($monitor->auth_type == 'digest') {
        $http->withDigestAuth($monitor->username, $monitor->password);
    }
    if ($monitor->auth_type == 'token') {
        $http->withToken($monitor->password);
    }
    return $http->get($monitor->url);
}

Or using Switch Statement:

if ($monitor->method == 'get') {
    $http = Http::withHeaders($headers)
        ->timeout($timeout);
    switch ($monitor->auth_type) {
        case 'basic':
            $http->withBasicAuth($monitor->username, $monitor->password);
            break;
        case 'digest':
            $http->withDigestAuth($monitor->username, $monitor->password);
            break;
        case 'token':
            $http->withToken($monitor->password);
            break;
    }
    return $http->get($monitor->url);
}

And ofc match in PHP 8, my favorite:

if ($monitor->method == 'get') {
    $http = Http::withHeaders($headers)
        ->timeout($timeout);
    match ($monitor->auth_type) {
        'basic' => $http->withBasicAuth($monitor->username, $monitor->password),
        'digest' => $http->withDigestAuth($monitor->username, $monitor->password),
        'token' => $http->withToken($monitor->password),
    };
    return $http->get($monitor->url);
}

In case, you're curios about match in PHP 8...

  • Related