Skip to content

Commit

Permalink
Add Request class (#1623)
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric authored Nov 6, 2023
1 parent bf716d8 commit 4839e3b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ public function __construct(string $sdkIdentifier, string $sdkVersion)
$this->sdkVersion = $sdkVersion;
}

public function sendRequest(string $requestData, Options $options): Response
public function sendRequest(Request $request, Options $options): Response

Check warning on line 31 in src/HttpClient/HttpClient.php

View check run for this annotation

Codecov / codecov/patch

src/HttpClient/HttpClient.php#L31

Added line #L31 was not covered by tests
{
$dsn = $options->getDsn();
if ($dsn === null) {
throw new \RuntimeException('The DSN option must be set to use the HttpClient.');

Check warning on line 35 in src/HttpClient/HttpClient.php

View check run for this annotation

Codecov / codecov/patch

src/HttpClient/HttpClient.php#L33-L35

Added lines #L33 - L35 were not covered by tests
}

$requestData = $request->getStringBody();
if ($requestData === null) {
throw new \RuntimeException('The request data is empty.');

Check warning on line 40 in src/HttpClient/HttpClient.php

View check run for this annotation

Codecov / codecov/patch

src/HttpClient/HttpClient.php#L38-L40

Added lines #L38 - L40 were not covered by tests
}

$curlHandle = curl_init();

Check warning on line 43 in src/HttpClient/HttpClient.php

View check run for this annotation

Codecov / codecov/patch

src/HttpClient/HttpClient.php#L43

Added line #L43 was not covered by tests

$requestHeaders = Http::getRequestHeaders($dsn, $this->sdkIdentifier, $this->sdkVersion);

Check warning on line 45 in src/HttpClient/HttpClient.php

View check run for this annotation

Codecov / codecov/patch

src/HttpClient/HttpClient.php#L45

Added line #L45 was not covered by tests
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClient/HttpClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

interface HttpClientInterface
{
public function sendRequest(string $requestData, Options $options): Response;
public function sendRequest(Request $request, Options $options): Response;
}
26 changes: 26 additions & 0 deletions src/HttpClient/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Sentry\HttpClient;

/**
* @internal
*/
final class Request
{
/**
* @var string
*/
private $stringBody;

public function getStringBody(): ?string

Check warning on line 17 in src/HttpClient/Request.php

View check run for this annotation

Codecov / codecov/patch

src/HttpClient/Request.php#L17

Added line #L17 was not covered by tests
{
return $this->stringBody;

Check warning on line 19 in src/HttpClient/Request.php

View check run for this annotation

Codecov / codecov/patch

src/HttpClient/Request.php#L19

Added line #L19 was not covered by tests
}

public function setStringBody(string $stringBody): void
{
$this->stringBody = $stringBody;
}
}
6 changes: 5 additions & 1 deletion src/Transport/HttpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Psr\Log\NullLogger;
use Sentry\Event;
use Sentry\HttpClient\HttpClientInterface;
use Sentry\HttpClient\Request;
use Sentry\Options;
use Sentry\Serializer\PayloadSerializerInterface;

Expand Down Expand Up @@ -79,8 +80,11 @@ public function send(Event $event): Result
return new Result(ResultStatus::rateLimit());
}

$request = new Request();
$request->setStringBody($this->payloadSerializer->serialize($event));

try {
$response = $this->httpClient->sendRequest($this->payloadSerializer->serialize($event), $this->options);
$response = $this->httpClient->sendRequest($request, $this->options);
} catch (\Throwable $exception) {
$this->logger->error(
sprintf('Failed to send the event to Sentry. Reason: "%s".', $exception->getMessage()),
Expand Down
3 changes: 2 additions & 1 deletion tests/ClientBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Sentry\Event;
use Sentry\HttpClient\HttpClient;
use Sentry\HttpClient\HttpClientInterface;
use Sentry\HttpClient\Request;
use Sentry\HttpClient\Response;
use Sentry\Integration\IntegrationInterface;
use Sentry\Options;
Expand Down Expand Up @@ -123,7 +124,7 @@ public function setupOnce(): void

final class CustomHttpClient implements HttpClientInterface
{
public function sendRequest(string $requestData, Options $options): Response
public function sendRequest(Request $request, Options $options): Response
{
return new Response(0, [], '');
}
Expand Down

0 comments on commit 4839e3b

Please sign in to comment.