putyourlightson/craft-sherlock

Send Guzzle configuration options through from the config file

Closed this issue · 5 comments

Please provide a clear and concise description of your feature request, including:

  • What the problem is and what you would like to have happen.

I would like to be able to configure Guzzle from the config file.

A perfect example of this type of implementation can be found with Feed Me:

https://docs.craftcms.com/feed-me/v4/get-started/configuration.html#example-requestoptions

This way, I can set an auth element with username and password (for example).

  • Any alternative solutions or features you have considered.

Do you need to configure Guzzle requests for Sherlock only? If not, then you should already be able to do this by configuring the Guzzle component, see https://craftcms.com/docs/4.x/config/#guzzle

I didn't know about this, so this is a big help, thank you!

However, we were looking to configure domain specific variables, such as username and password, so the global settings don't help here.

For now, I have edited line 140 of:

/vendor/putyourlightson/craft-sherlock/src/services/TestsService.php

To add the configuration we need to perform a single run, and then I will reinstall the plugin to remove my "patch".

I added a TestsService::EVENT_BEFORE_RUN_TESTS event that you should be able to use to override the Guzzle client via a custom module/plugin.

use Craft;
use putyourlightson\sherlock\events\RunTestsEvent;
use putyourlightson\sherlock\services\TestsService;
use yii\base\Event;

Event::on(TestsService::class, TestsService::EVENT_BEFORE_RUN_TESTS,
    function(RunTestsEvent $event) {
        if (Craft::$app->sites->currentSite->id === 1) {
            $event->client = Craft::createGuzzleClient([
                'auth' => ['username', 'password'],
                'timeout' => 10,
            ]);
        }
    }
);

You can test this by running composer require "putyourlightson/craft-sherlock:dev-develop as 4.4.0".

Excellent, thank you very much!

I don't suppose there is any chance of this also being rolled out for the Craft 3 version of the plugin, is there?

I understand if you are just adding new features to the Craft 4 version, and thank you for this, it will be a big help.

Released in version 4.4.0. Unfortunately, Sherlock 3 (for Craft 3) is only getting bug fixes and security updates but no new features.