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

feature request: allow to configure middleware on the handler #321

Open
dbu opened this issue Sep 14, 2022 · 0 comments
Open

feature request: allow to configure middleware on the handler #321

dbu opened this issue Sep 14, 2022 · 0 comments

Comments

@dbu
Copy link

dbu commented Sep 14, 2022

In our application, we have some clients where we use specific middleware services. We configure them as symfony service. To allow us to configure them in an eightpoints guzzle client, i found no way to do that and ended up writing a plugin. The plugin is so simple that i think it would make sense to integrate it directly into the bundle to allow a configuration section middleware on each client.

if there is interest, i can do a pull request to integrate middleware configuration. but maybe it is not currently supported on purpose?

The whole code for the plugin is this:

use EightPoints\Bundle\GuzzleBundle\PluginInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

/**
 * A plugin to allow adding middleware service definitions to clients.
 */
class EightPointsGuzzleMiddlewarePlugin implements PluginInterface
{
    public function getPluginName(): string
    {
        return 'middleware';
    }

    public function addConfiguration(ArrayNodeDefinition $pluginNode): void
    {
        $pluginNode->prototype('scalar');
    }

    public function load(array $configs, ContainerBuilder $container): void
    {
    }

    public function loadForClient(array $config, ContainerBuilder $container, string $clientName, Definition $handler): void
    {
        foreach ($config as $middleware) {
            $handler->addMethodCall('push', [new Reference($middleware)]);
        }
    }

    public function build(ContainerBuilder $container)
    {
    }

    public function boot()
    {
    }
}

and then in the configuration, i can do:

eight_points_guzzle:
    clients:
        my-client:
            base_url: https://example.org
            options:
                connect_timeout: '5'
                headers:
                    Accept: 'application/json'
            plugin:
                middleware:
                    - 'my-custom-guzzle-middleware-service'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant