Skip to content

Commit

Permalink
Add wrapHttpClientWithPlugins method to ClientBuilder.
Browse files Browse the repository at this point in the history
getsentry#787

This should ease the process of registering a custom transport.
  • Loading branch information
RageZBla committed Aug 18, 2019
1 parent 19e629d commit 60074bf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
47 changes: 32 additions & 15 deletions src/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,22 +286,9 @@ private function createHttpClientInstance(): PluginClient
throw new \RuntimeException('The PSR-18 HTTP client must be set.');
}

if (null !== $this->options->getDsn()) {
$this->addHttpClientPlugin(new BaseUriPlugin($this->uriFactory->createUri($this->options->getDsn())));
}

$this->addHttpClientPlugin(new HeaderSetPlugin(['User-Agent' => $this->sdkIdentifier . '/' . $this->getSdkVersion()]));
$this->addHttpClientPlugin(new AuthenticationPlugin(new SentryAuthentication($this->options, $this->sdkIdentifier, $this->getSdkVersion())));

if ($this->options->isCompressionEnabled()) {
$this->addHttpClientPlugin(new GzipEncoderPlugin());
$this->addHttpClientPlugin(new DecoderPlugin());
}
$this->registerDefaultPlugins();

$this->addHttpClientPlugin(new RetryPlugin(['retries' => $this->options->getSendAttempts()]));
$this->addHttpClientPlugin(new ErrorPlugin());

return new PluginClient($this->httpClient, $this->httpClientPlugins);
return $this->wrapHttpClientWithPlugins($this->httpClient);
}

/**
Expand Down Expand Up @@ -357,4 +344,34 @@ private function createEventFactory(): EventFactoryInterface

return new EventFactory($this->serializer, $this->representationSerializer, $this->options, $this->sdkIdentifier, $this->getSdkVersion());
}

public function wrapHttpClientWithPlugins(HttpAsyncClient $client): PluginClient
{
return new PluginClient($client, $this->httpClientPlugins);
}

private function registerDefaultPlugins(): void
{
if (0 !== \count($this->httpClientPlugins)) {
throw new \LogicException('This should be called only if no plugins are registered');
}

$this->uriFactory = $this->uriFactory ?? UriFactoryDiscovery::find();
if (null === $this->uriFactory) {
throw new \RuntimeException('The PSR-7 URI factory must be set.');
}

if (null !== $this->options->getDsn()) {
$this->addHttpClientPlugin(new BaseUriPlugin($this->uriFactory->createUri($this->options->getDsn())));
}

$this->addHttpClientPlugin(new HeaderSetPlugin(['User-Agent' => $this->sdkIdentifier . '/' . $this->getSdkVersion()]));
$this->addHttpClientPlugin(new AuthenticationPlugin(new SentryAuthentication($this->options, $this->sdkIdentifier, $this->getSdkVersion())));
$this->addHttpClientPlugin(new RetryPlugin(['retries' => $this->options->getSendAttempts()]));
$this->addHttpClientPlugin(new ErrorPlugin());

if ($this->options->isCompressionEnabled()) {
$this->addHttpClientPlugin(new DecoderPlugin());
}
}
}
10 changes: 10 additions & 0 deletions tests/ClientBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,16 @@ public function testCreateWithHttpProxyAndCustomTransportThrowsException(): void

$clientBuilder->getClient();
}

public function testWrapHttpClientWithDefaultPlugins(): void
{
$clientBuilder = new ClientBuilder(new Options());

/** @var PluginClient $wrappedClient */
$wrappedClient = $clientBuilder->wrapHttpClientWithPlugins($this->createMock(HttpAsyncClient::class));

$this->assertInstanceOf(PluginClient::class, $wrappedClient);
}
}

final class StubIntegration implements IntegrationInterface
Expand Down

0 comments on commit 60074bf

Please sign in to comment.