Skip to content

Commit

Permalink
Add a default cURL HTTP client
Browse files Browse the repository at this point in the history
Fix PHP 7.2 - 7.3 tests

Fix CS

Remove .relay folder
  • Loading branch information
cleptric committed Sep 20, 2023
1 parent faec023 commit fa5145d
Show file tree
Hide file tree
Showing 40 changed files with 590 additions and 1,416 deletions.
11 changes: 2 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,15 @@
"php": "^7.2|^8.0",
"ext-json": "*",
"ext-mbstring": "*",
"guzzlehttp/promises": "^1.5.3|^2.0",
"ext-curl": "*",
"jean85/pretty-package-versions": "^1.5|^2.0.4",
"php-http/async-client-implementation": "^1.0",
"php-http/client-common": "^1.5|^2.0",
"php-http/discovery": "^1.15",
"php-http/httplug": "^1.1|^2.0",
"php-http/message": "^1.5",
"php-http/message-factory": "^1.1",
"psr/http-factory": "^1.0",
"psr/http-factory-implementation": "^1.0",
"psr/log": "^1.0|^2.0|^3.0",
"symfony/options-resolver": "^3.4.43|^4.4.30|^5.0.11|^6.0",
"symfony/polyfill-php80": "^1.17"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.19|3.4.*",
"guzzlehttp/promises": "^1.0|^2.0",
"guzzlehttp/psr7": "^1.8.4|^2.1.1",
"http-interop/http-factory-guzzle": "^1.0",
"monolog/monolog": "^1.6|^2.0|^3.0",
Expand Down
60 changes: 15 additions & 45 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -35,51 +35,6 @@ parameters:
count: 1
path: src/Dsn.php

-
message: "#^Access to constant CONNECT_TIMEOUT on an unknown class GuzzleHttp\\\\RequestOptions\\.$#"
count: 1
path: src/HttpClient/HttpClientFactory.php

-
message: "#^Access to constant PROXY on an unknown class GuzzleHttp\\\\RequestOptions\\.$#"
count: 1
path: src/HttpClient/HttpClientFactory.php

-
message: "#^Access to constant TIMEOUT on an unknown class GuzzleHttp\\\\RequestOptions\\.$#"
count: 1
path: src/HttpClient/HttpClientFactory.php

-
message: "#^Call to static method create\\(\\) on an unknown class Symfony\\\\Component\\\\HttpClient\\\\HttpClient\\.$#"
count: 1
path: src/HttpClient/HttpClientFactory.php

-
message: "#^Call to static method createWithConfig\\(\\) on an unknown class Http\\\\Adapter\\\\Guzzle6\\\\Client\\.$#"
count: 1
path: src/HttpClient/HttpClientFactory.php

-
message: "#^Constructor of class Sentry\\\\HttpClient\\\\HttpClientFactory has an unused parameter \\$responseFactory\\.$#"
count: 1
path: src/HttpClient/HttpClientFactory.php

-
message: "#^Constructor of class Sentry\\\\HttpClient\\\\HttpClientFactory has an unused parameter \\$uriFactory\\.$#"
count: 1
path: src/HttpClient/HttpClientFactory.php

-
message: "#^Method Sentry\\\\HttpClient\\\\HttpClientFactory\\:\\:resolveClient\\(\\) should return Http\\\\Client\\\\HttpAsyncClient\\|Psr\\\\Http\\\\Client\\\\ClientInterface but returns Http\\\\Client\\\\Curl\\\\Client\\.$#"
count: 1
path: src/HttpClient/HttpClientFactory.php

-
message: "#^Method Sentry\\\\HttpClient\\\\HttpClientFactory\\:\\:resolveClient\\(\\) should return Http\\\\Client\\\\HttpAsyncClient\\|Psr\\\\Http\\\\Client\\\\ClientInterface but returns Symfony\\\\Component\\\\HttpClient\\\\HttplugClient\\.$#"
count: 1
path: src/HttpClient/HttpClientFactory.php

-
message: "#^Property Sentry\\\\Integration\\\\IgnoreErrorsIntegration\\:\\:\\$options \\(array\\{ignore_exceptions\\: array\\<int, class\\-string\\<Throwable\\>\\>, ignore_tags\\: array\\<string, string\\>\\}\\) does not accept array\\.$#"
count: 1
Expand Down Expand Up @@ -140,6 +95,11 @@ parameters:
count: 1
path: src/Options.php

-
message: "#^Method Sentry\\\\Options\\:\\:getHttpClient\\(\\) should return Sentry\\\\HttpClient\\\\HttpClientInterface\\|null but returns mixed\\.$#"
count: 1
path: src/Options.php

-
message: "#^Method Sentry\\\\Options\\:\\:getHttpConnectTimeout\\(\\) should return float but returns mixed\\.$#"
count: 1
Expand All @@ -150,6 +110,11 @@ parameters:
count: 1
path: src/Options.php

-
message: "#^Method Sentry\\\\Options\\:\\:getHttpProxyAuthentication\\(\\) should return string\\|null but returns mixed\\.$#"
count: 1
path: src/Options.php

-
message: "#^Method Sentry\\\\Options\\:\\:getHttpTimeout\\(\\) should return float but returns mixed\\.$#"
count: 1
Expand Down Expand Up @@ -245,6 +210,11 @@ parameters:
count: 1
path: src/Options.php

-
message: "#^Method Sentry\\\\Options\\:\\:getTransport\\(\\) should return Sentry\\\\Transport\\\\TransportInterface\\|null but returns mixed\\.$#"
count: 1
path: src/Options.php

-
message: "#^Method Sentry\\\\Options\\:\\:hasDefaultIntegrations\\(\\) should return bool but returns mixed\\.$#"
count: 1
Expand Down
14 changes: 9 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Sentry;

use GuzzleHttp\Promise\PromiseInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Sentry\Integration\IntegrationInterface;
Expand All @@ -13,6 +12,7 @@
use Sentry\Serializer\RepresentationSerializerInterface;
use Sentry\Serializer\SerializerInterface;
use Sentry\State\Scope;
use Sentry\Transport\Result;
use Sentry\Transport\TransportInterface;

/**
Expand Down Expand Up @@ -173,14 +173,18 @@ public function captureEvent(Event $event, ?EventHint $hint = null, ?Scope $scop
}

try {
/** @var Response $response */
$response = $this->transport->send($event)->wait();
$event = $response->getEvent();
/** @var Result $result */
$result = $this->transport->send($event);
$event = $result->getEvent();

if (null !== $event) {
return $event->getId();
}
} catch (\Throwable $exception) {
$this->logger->error(
sprintf('Failed to send the event to Sentry. Reason: "%s".', $exception->getMessage()),
['exception' => $exception, 'event' => $event]
);

Check warning on line 187 in src/Client.php

View check run for this annotation

Codecov / codecov/patch

src/Client.php#L184-L187

Added lines #L184 - L187 were not covered by tests
}

return null;
Expand Down Expand Up @@ -216,7 +220,7 @@ public function getIntegration(string $className): ?IntegrationInterface
/**
* {@inheritdoc}
*/
public function flush(?int $timeout = null): PromiseInterface
public function flush(?int $timeout = null): Result
{
return $this->transport->close($timeout);
}
Expand Down
78 changes: 30 additions & 48 deletions src/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Sentry;

use Http\Discovery\Psr17FactoryDiscovery;
use Psr\Log\LoggerInterface;
use Sentry\HttpClient\HttpClientFactory;
use Sentry\HttpClient\HttpClient;
use Sentry\HttpClient\HttpClientInterface;
use Sentry\Serializer\PayloadSerializer;
use Sentry\Serializer\RepresentationSerializerInterface;
use Sentry\Serializer\SerializerInterface;
use Sentry\Transport\DefaultTransportFactory;
use Sentry\Transport\TransportFactoryInterface;
use Sentry\Transport\HttpTransport;
use Sentry\Transport\TransportInterface;

/**
Expand All @@ -26,14 +26,14 @@ final class ClientBuilder implements ClientBuilderInterface
private $options;

/**
* @var TransportFactoryInterface|null The transport factory
* @var TransportInterface The transport
*/
private $transportFactory;
private $transport;

/**
* @var TransportInterface|null The transport
* @var HttpClientInterface The HTTP client
*/
private $transport;
private $httpClient;

/**
* @var SerializerInterface|null The serializer to be injected in the client
Expand Down Expand Up @@ -68,6 +68,13 @@ final class ClientBuilder implements ClientBuilderInterface
public function __construct(Options $options = null)
{
$this->options = $options ?? new Options();

$this->httpClient = $this->options->getHttpClient() ?? new HttpClient($this->options, $this->sdkIdentifier, $this->sdkVersion);
$this->transport = $this->options->getTransport() ?? new HttpTransport(
$this->httpClient,
new PayloadSerializer($this->options),
$this->logger
);
}

/**
Expand Down Expand Up @@ -136,60 +143,35 @@ public function setSdkVersion(string $sdkVersion): ClientBuilderInterface
return $this;
}

/**
* {@inheritdoc}
*/
public function setTransportFactory(TransportFactoryInterface $transportFactory): ClientBuilderInterface
public function getTransport(): TransportInterface

Check warning on line 146 in src/ClientBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/ClientBuilder.php#L146

Added line #L146 was not covered by tests
{
$this->transportFactory = $transportFactory;

return $this;
return $this->transport;

Check warning on line 148 in src/ClientBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/ClientBuilder.php#L148

Added line #L148 was not covered by tests
}

/**
* {@inheritdoc}
*/
public function getClient(): ClientInterface
public function setTransport(TransportInterface $transport): ClientBuilderInterface
{
$this->transport = $this->transport ?? $this->createTransportInstance();
$this->transport = $transport;

return new Client($this->options, $this->transport, $this->sdkIdentifier, $this->sdkVersion, $this->serializer, $this->representationSerializer, $this->logger);
return $this;
}

/**
* Creates a new instance of the transport mechanism.
*/
private function createTransportInstance(): TransportInterface
public function getHttpClient(): HttpClientInterface

Check warning on line 158 in src/ClientBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/ClientBuilder.php#L158

Added line #L158 was not covered by tests
{
if (null !== $this->transport) {
return $this->transport;
}
return $this->httpClient;

Check warning on line 160 in src/ClientBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/ClientBuilder.php#L160

Added line #L160 was not covered by tests
}

$transportFactory = $this->transportFactory ?? $this->createDefaultTransportFactory();
public function setHttpClient(HttpClientInterface $httpClient): ClientBuilderInterface

Check warning on line 163 in src/ClientBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/ClientBuilder.php#L163

Added line #L163 was not covered by tests
{
$this->httpClient = $httpClient;

Check warning on line 165 in src/ClientBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/ClientBuilder.php#L165

Added line #L165 was not covered by tests

return $transportFactory->create($this->options);
return $this;

Check warning on line 167 in src/ClientBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/ClientBuilder.php#L167

Added line #L167 was not covered by tests
}

/**
* Creates a new instance of the {@see DefaultTransportFactory} factory.
* {@inheritdoc}
*/
private function createDefaultTransportFactory(): DefaultTransportFactory
public function getClient(): ClientInterface
{
$streamFactory = Psr17FactoryDiscovery::findStreamFactory();
$httpClientFactory = new HttpClientFactory(
null,
null,
$streamFactory,
null,
$this->sdkIdentifier,
$this->sdkVersion
);

return new DefaultTransportFactory(
$streamFactory,
Psr17FactoryDiscovery::findRequestFactory(),
$httpClientFactory,
$this->logger
);
return new Client($this->options, $this->transport, $this->sdkIdentifier, $this->sdkVersion, $this->serializer, $this->representationSerializer, $this->logger);
}
}
10 changes: 0 additions & 10 deletions src/ClientBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Psr\Log\LoggerInterface;
use Sentry\Serializer\RepresentationSerializerInterface;
use Sentry\Serializer\SerializerInterface;
use Sentry\Transport\TransportFactoryInterface;

/**
* A configurable builder for Client objects.
Expand Down Expand Up @@ -64,15 +63,6 @@ public function setRepresentationSerializer(RepresentationSerializerInterface $r
*/
public function setLogger(LoggerInterface $logger): ClientBuilderInterface;

/**
* Sets the transport factory.
*
* @param TransportFactoryInterface $transportFactory The transport factory
*
* @return $this
*/
public function setTransportFactory(TransportFactoryInterface $transportFactory): ClientBuilderInterface;

/**
* Sets the SDK identifier to be passed onto {@see Event} and HTTP User-Agent header.
*
Expand Down
4 changes: 2 additions & 2 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Sentry;

use GuzzleHttp\Promise\PromiseInterface;
use Sentry\Integration\IntegrationInterface;
use Sentry\State\Scope;
use Sentry\Transport\Result;

/**
* This interface must be implemented by all Raven client classes.
Expand Down Expand Up @@ -78,5 +78,5 @@ public function getIntegration(string $className): ?IntegrationInterface;
*
* @param int|null $timeout Maximum time in seconds the client should wait
*/
public function flush(?int $timeout = null): PromiseInterface;
public function flush(?int $timeout = null): Result;
}
Loading

0 comments on commit fa5145d

Please sign in to comment.