Skip to content

Commit c6a4885

Browse files
committed
[HttpClient][Messenger] add PingWebhook and PingWebhookHandler
1 parent 510e51f commit c6a4885

File tree

7 files changed

+159
-0
lines changed

7 files changed

+159
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
use Symfony\Component\HtmlSanitizer\HtmlSanitizer;
8484
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;
8585
use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface;
86+
use Symfony\Component\HttpClient\Messenger\PingWebhookHandler;
8687
use Symfony\Component\HttpClient\MockHttpClient;
8788
use Symfony\Component\HttpClient\Retry\GenericRetryStrategy;
8889
use Symfony\Component\HttpClient\RetryableHttpClient;
@@ -2455,6 +2456,10 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
24552456
unset($options['vars']);
24562457
$container->getDefinition('http_client.transport')->setArguments([$options, $config['max_host_connections'] ?? 6]);
24572458

2459+
if (!class_exists(PingWebhookHandler::class)) {
2460+
$container->removeDefinition('http_client.messenger.ping_webhook_handler');
2461+
}
2462+
24582463
if (!$hasPsr18 = ContainerBuilder::willBeAvailable('psr/http-client', ClientInterface::class, ['symfony/framework-bundle', 'symfony/http-client'])) {
24592464
$container->removeDefinition('psr18.http_client');
24602465
$container->removeAlias(ClientInterface::class);

src/Symfony/Bundle/FrameworkBundle/Resources/config/http_client.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Psr\Http\Message\StreamFactoryInterface;
1818
use Symfony\Component\HttpClient\HttpClient;
1919
use Symfony\Component\HttpClient\HttplugClient;
20+
use Symfony\Component\HttpClient\Messenger\PingWebhookHandler;
2021
use Symfony\Component\HttpClient\Psr18Client;
2122
use Symfony\Component\HttpClient\Retry\GenericRetryStrategy;
2223
use Symfony\Component\HttpClient\UriTemplateHttpClient;
@@ -90,5 +91,11 @@
9091
->args([
9192
[inline_service(\Rize\UriTemplate::class), 'expand'],
9293
])
94+
95+
->set('http_client.messenger.ping_webhook_handler', PingWebhookHandler::class)
96+
->args([
97+
service('http_client'),
98+
])
99+
->tag('messenger.message_handler')
93100
;
94101
};

src/Symfony/Component/HttpClient/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add `HarFileResponseFactory` testing utility, allow to replay responses from `.har` files
88
* Add `max_retries` option to `RetryableHttpClient` to adjust the retry logic on a per request level
9+
* Add `PingWehook` and `PingWebhookHandler`
910

1011
6.3
1112
---
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Messenger;
13+
14+
/**
15+
* @author Kevin Bond <kevinbond@gmail.com>
16+
*/
17+
final class PingWebhook implements \Stringable
18+
{
19+
public function __construct(
20+
public readonly string $method,
21+
public readonly string $url,
22+
public readonly array $options = [],
23+
public readonly bool $throw = true,
24+
) {
25+
}
26+
27+
public function __toString(): string
28+
{
29+
return "[{$this->method}] {$this->url}";
30+
}
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Messenger;
13+
14+
use Symfony\Contracts\HttpClient\HttpClientInterface;
15+
use Symfony\Contracts\HttpClient\ResponseInterface;
16+
17+
/**
18+
* @author Kevin Bond <kevinbond@gmail.com>
19+
*/
20+
final class PingWebhookHandler
21+
{
22+
public function __construct(private readonly HttpClientInterface $httpClient)
23+
{
24+
}
25+
26+
public function __invoke(PingWebhook $message): ResponseInterface
27+
{
28+
$response = $this->httpClient->request($message->method, $message->url, $message->options);
29+
$response->getHeaders($message->throw);
30+
31+
return $response;
32+
}
33+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Tests\Messenger;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpClient\Exception\ClientException;
16+
use Symfony\Component\HttpClient\Exception\ServerException;
17+
use Symfony\Component\HttpClient\Messenger\PingWebhook;
18+
use Symfony\Component\HttpClient\Messenger\PingWebhookHandler;
19+
use Symfony\Component\HttpClient\MockHttpClient;
20+
use Symfony\Component\HttpClient\Response\MockResponse;
21+
22+
/**
23+
* @author Kevin Bond <kevinbond@gmail.com>
24+
*/
25+
final class PingWebhookHandlerTest extends TestCase
26+
{
27+
public function testSuccessfulPing()
28+
{
29+
$client = new MockHttpClient([
30+
function ($method, $url) {
31+
$this->assertSame('POST', $method);
32+
$this->assertSame('https://endpoint.com/key', $url);
33+
34+
return new MockResponse('a response');
35+
},
36+
]);
37+
$handler = new PingWebhookHandler($client);
38+
$response = $handler(new PingWebhook('POST', 'https://endpoint.com/key'));
39+
40+
$this->assertSame(200, $response->getStatusCode());
41+
$this->assertSame('a response', $response->getContent());
42+
$this->assertSame('https://endpoint.com/key', $response->getInfo('url'));
43+
}
44+
45+
public function testPingErrorThrowsException()
46+
{
47+
$client = new MockHttpClient([
48+
function ($method, $url) {
49+
$this->assertSame('POST', $method);
50+
$this->assertSame('https://endpoint.com/key', $url);
51+
52+
return new MockResponse('a response', ['http_code' => 404]);
53+
},
54+
]);
55+
56+
$handler = new PingWebhookHandler($client);
57+
58+
$this->expectException(ClientException::class);
59+
60+
$handler(new PingWebhook('POST', 'https://endpoint.com/key'));
61+
}
62+
63+
public function testPingErrorDoesNotThrowException()
64+
{
65+
$client = new MockHttpClient([
66+
function ($method, $url) {
67+
$this->assertSame('POST', $method);
68+
$this->assertSame('https://endpoint.com/key', $url);
69+
70+
return new MockResponse('a response', ['http_code' => 404]);
71+
},
72+
]);
73+
74+
$handler = new PingWebhookHandler($client);
75+
$response = $handler(new PingWebhook('POST', 'https://endpoint.com/key', throw: false));
76+
77+
$this->assertSame(404, $response->getStatusCode());
78+
$this->assertSame('a response', $response->getContent(false));
79+
$this->assertSame('https://endpoint.com/key', $response->getInfo('url'));
80+
}
81+
}

src/Symfony/Component/HttpClient/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"psr/http-client": "^1.0",
4040
"symfony/dependency-injection": "^5.4|^6.0|^7.0",
4141
"symfony/http-kernel": "^5.4|^6.0|^7.0",
42+
"symfony/messenger": "^5.4|^6.0|^7.0",
4243
"symfony/process": "^5.4|^6.0|^7.0",
4344
"symfony/stopwatch": "^5.4|^6.0|^7.0"
4445
},

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy