Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Client::withConfig() #3078

Open
nicolas-grekas opened this issue Oct 21, 2022 · 7 comments
Open

Add Client::withConfig() #3078

nicolas-grekas opened this issue Oct 21, 2022 · 7 comments
Labels
lifecycle/stale No activity for a long time

Comments

@nicolas-grekas
Copy link

It'd be useful to add Client::withConfig() to allow derivating a custom-configured client from a preexisting one, eg to change the default timeout for a local need, etc.

@GrahamCampbell
Copy link
Member

Nice idea. A method that returns a new client, but retains the inner handler is what I had in mind here. That track with what you were thinking, too?

@nicolas-grekas
Copy link
Author

Absolutely!

@shahlin
Copy link

shahlin commented Dec 19, 2022

@GrahamCampbell Would this just be same as the constructor but via a static call in https://github.com/guzzle/guzzle/blob/master/src/Client.php?

public static function withConfig(array $config = [])
{
    return new static($config);
}

If yes, I'd be happy to work on this issue

@GrahamCampbell
Copy link
Member

No. That does not retain the inner handler.

@stale
Copy link

stale bot commented May 1, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you for your contributions.

@stale stale bot added the lifecycle/stale No activity for a long time label May 1, 2023
@stale stale bot closed this as completed May 18, 2023
@stale stale bot removed the lifecycle/stale No activity for a long time label May 18, 2023
@criskell
Copy link

No. That does not retain the inner handler.

Does this "retain the inner handler"?

    public function withConfig(array $config = []): self
    {
        $changedConfig = $config + $this->config;

        // Retain inner handler?
        $changedConfig["handler"] = $this->config["handler"];

        return new self($changedConfig);
    }
    public function testWithConfig()
    {
        $baseClient = new Client([
            RequestOptions::TIMEOUT => 10,
            RequestOptions::CONNECT_TIMEOUT => 20,
        ]);
        $baseConfig = Helpers::readObjectAttribute($baseClient, 'config');

        $mergedClient = $baseClient->withConfig([
            RequestOptions::TIMEOUT => 50,
            'handler' => function () {},
        ]);
        $mergedConfig = Helpers::readObjectAttribute($mergedClient, 'config');

        self::assertSame($mergedConfig[RequestOptions::TIMEOUT], 50);
        self::assertSame($mergedConfig[RequestOptions::CONNECT_TIMEOUT], 20);
        self::assertSame($baseConfig['handler'], $mergedConfig['handler']);
    }

Copy link

stale bot commented Mar 17, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you for your contributions.

@stale stale bot added the lifecycle/stale No activity for a long time label Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lifecycle/stale No activity for a long time
Projects
None yet
Development

No branches or pull requests

4 participants