Skip to content

Conversation

@veewee
Copy link
Contributor

@veewee veewee commented Dec 1, 2025

Q A
Type feature
BC Break no
Fixed issues

Summary

<?php
require __DIR__ . '/vendor/autoload.php';

use Http\Message\Authentication\BasicAuth;
use Http\Message\Formatter;
use Phpro\HttpTools\Client\ClientBuilder;
use Phpro\HttpTools\Client\Factory\SymfonyClientFactory;
use Phpro\HttpTools\Formatter\FormatterBuilder;
use Phpro\HttpTools\Formatter\RemoveSensitiveHeadersFormatter;
use Phpro\HttpTools\Request\Request;
use Phpro\HttpTools\Transport\Presets\RawPreset;
use Phpro\HttpTools\Uri\RawUriBuilder;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;

$client = ClientBuilder::default(SymfonyClientFactory::create([]))
    ->addBaseUri('https://www.google.com')
    ->addHeaders([
        'x-Foo' => 'bar',
    ])
    ->addAuthentication(new BasicAuth('user', 'pass'))
    ->addLogger(
        new ConsoleLogger(new ConsoleOutput(OutputInterface::VERBOSITY_DEBUG)),
        FormatterBuilder::default()
            ->withDebug(true)
            ->withMaxBodyLength(1000)
            ->addDecorator(RemoveSensitiveHeadersFormatter::createDecorator([
                'X-SENSITIVE-HEADER',
            ]))
            ->build()
    )
    ->addPluginWithCurrentlyConfiguredClient(
        static fn (\Psr\Http\Client\ClientInterface $client) => new class($client) implements \Http\Client\Common\Plugin
        {
            public function __construct(
                private \Psr\Http\Client\ClientInterface $client,
            ) {
            }

            public function handleRequest(\Psr\Http\Message\RequestInterface $request, callable $next, callable $first): \Http\Promise\Promise
            {
                // $token = $this->client->sendRequest('...;');

                return $next($request->withHeader('X-token', '$token'));
            }

        }
    )
    ->build();


$transport = RawPreset::create($client, RawUriBuilder::createWithAutodiscoveredPsrFactories());
$request = new Request('GET', '/', [], '');
$response = $transport($request);

echo $response;


@MetalArend
Copy link

Looks very promising 😍

@veewee veewee force-pushed the client-builder branch 6 times, most recently from c6ace9e to f94bd32 Compare December 2, 2025 07:27
@veewee veewee requested a review from Copilot December 2, 2025 07:28
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces reusable builder classes for configuring HTTP clients and formatters, simplifying the setup process for complex HTTP client configurations with plugins, authentication, logging, and sensitive data masking.

Key changes:

  • Added ClientBuilder class with fluent API for configuring HTTP clients with plugins, authentication, logging, base URIs, headers, and recording capabilities
  • Added FormatterBuilder class with fluent API for configuring message formatters with debugging and decorator support
  • Added static factory methods (createDecorator) to sensitive data formatter classes for easier integration with the builder pattern

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Client/ClientBuilder.php New builder class providing fluent API for HTTP client configuration with support for plugins, authentication, logging, and more
src/Formatter/FormatterBuilder.php New builder class for configuring message formatters with debug mode, body length limits, and decorators
src/Formatter/RemoveSensitiveHeadersFormatter.php Added createDecorator static factory method for builder integration
src/Formatter/RemoveSensitiveJsonKeysFormatter.php Added createDecorator static factory method for builder integration
src/Formatter/RemoveSensitiveQueryStringsFormatter.php Added createDecorator static factory method for builder integration
tests/Unit/Client/ClientBuilderTest.php Comprehensive test coverage for ClientBuilder functionality
tests/Unit/Formatter/FormatterBuilderTest.php Comprehensive test coverage for FormatterBuilder functionality
tests/Unit/Formatter/RemoveSensitiveHeaderKeysFormatterTest.php Added test for createDecorator factory method
tests/Unit/Formatter/RemoveSensitiveJsonKeysFormatterTest.php Added test for createDecorator factory method
tests/Unit/Formatter/RemoveSensitiveQueryStringsFormatterTest.php Added test for createDecorator factory method
README.md Added documentation and examples for using the new builder classes
docs/framework/symfony.md Removed Symfony-specific documentation (moved or consolidated elsewhere)
docs/framework/magento2.md Removed Magento2-specific documentation (moved or consolidated elsewhere)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@veewee veewee added the enhancement New feature or request label Dec 2, 2025
@veewee veewee force-pushed the client-builder branch 2 times, most recently from d3ffb00 to ad95bf7 Compare December 2, 2025 07:40
@veewee veewee merged commit ca65dea into phpro:v2.x Dec 2, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants