Skip to content

Commit

Permalink
feat: add support for timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioturdo committed Nov 22, 2024
1 parent b61de63 commit bd71df8
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Braze.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ public function setValidation(bool $active, bool $strict = false): void
$this->strictValidation = $active && $strict;
}

public function setConnectionTimeout(?float $connectionTimeout): void
{
$this->client->setConnectionTimeout($connectionTimeout);
}

public function setOverallTimeout(?float $overallTimeout): void
{
$this->client->setOverallTimeout($overallTimeout);
}

public function getDryRun(): bool
{
return $this->dryRun;
Expand All @@ -163,4 +173,14 @@ public function getStrictValidation(): bool
{
return $this->strictValidation;
}

public function getConnectionTimeout(): ?float
{
return $this->client->getConnectionTimeout();
}

public function getOverallTimeout(): ?float
{
return $this->client->getOverallTimeout();
}
}
20 changes: 20 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,24 @@ public function flush(): int

return $resolvedResponseCount;
}

public function setConnectionTimeout(?float $connectionTimeout): void
{
$this->adapter->setConnectionTimeout($connectionTimeout);
}

public function setOverallTimeout(?float $overallTimeout): void
{
$this->adapter->setOverallTimeout($overallTimeout);
}

public function getConnectionTimeout(): ?float
{
return $this->adapter->getConnectionTimeout();
}

public function getOverallTimeout(): ?float
{
return $this->adapter->getOverallTimeout();
}
}
8 changes: 8 additions & 0 deletions src/ClientAdapter/ClientAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ public function makeRequest(string $method, string $path, ?string $body = null):
* @throws ClientException
*/
public function resolveResponse(mixed $httpResponse): ClientResolvedResponse;

public function setConnectionTimeout(?float $connectionTimeout): void;

public function setOverallTimeout(?float $overallTimeout): void;

public function getConnectionTimeout(): ?float;

public function getOverallTimeout(): ?float;
}
23 changes: 23 additions & 0 deletions src/ClientAdapter/Psr18Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class Psr18Adapter implements ClientAdapterInterface

private ?UriInterface $baseURI = null;

private ?float $connectionTimeout = null;
private ?float $overallTimeout = null;

public function __construct(ClientInterface $client, UriFactoryInterface $uriFactory, RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory)
{
$this->client = $client;
Expand Down Expand Up @@ -92,4 +95,24 @@ public function resolveResponse($httpResponse): ClientResolvedResponse
{
return new ClientResolvedResponse($httpResponse->getStatusCode(), $httpResponse->getBody());
}

public function setConnectionTimeout(?float $connectionTimeout): void
{
throw new \RuntimeException('Psr18Adapter does not support timeouts');
}

public function setOverallTimeout(?float $overallTimeout): void
{
throw new \RuntimeException('Psr18Adapter does not support timeouts');
}

public function getConnectionTimeout(): ?float
{
throw new \RuntimeException('Psr18Adapter does not support timeouts');
}

public function getOverallTimeout(): ?float
{
throw new \RuntimeException('Psr18Adapter does not support timeouts');
}
}
25 changes: 25 additions & 0 deletions src/ClientAdapter/SymfonyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class SymfonyAdapter implements ClientAdapterInterface

private ?string $baseURI = null;

private ?float $connectionTimeout = null;
private ?float $overallTimeout = null;

public function __construct(HttpClientInterface $client)
{
$this->client = $client;
Expand All @@ -42,6 +45,8 @@ public function makeRequest(string $method, string $path, ?string $body = null):
'base_uri' => $this->baseURI,
'headers' => $this->headers,
'body' => $body,
'timeout' => $this->connectionTimeout,
'max_duration' => $this->overallTimeout ?? 0,
];

try {
Expand All @@ -66,4 +71,24 @@ public function resolveResponse($httpResponse): ClientResolvedResponse
throw new ClientException($clientException->getMessage(), 0, $clientException);
}
}

public function setConnectionTimeout(?float $connectionTimeout): void
{
$this->connectionTimeout = $connectionTimeout;
}

public function setOverallTimeout(?float $overallTimeout): void
{
$this->overallTimeout = $overallTimeout;
}

public function getConnectionTimeout(): ?float
{
return $this->connectionTimeout;
}

public function getOverallTimeout(): ?float
{
return $this->overallTimeout;
}
}
18 changes: 18 additions & 0 deletions tests/BrazeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,22 @@ public function testFlush(): void
$resolvedResponseCount = $this->instance->flush();
$this->assertSame(1, $resolvedResponseCount);
}

public function testSetConnectionTimeout(): void
{
$timeout = 1.2;

$this->instance->setConnectionTimeout($timeout);

$this->assertSame($timeout, $this->instance->getConnectionTimeout());
}

public function testSetOverallTimeout(): void
{
$timeout = 2.0;

$this->instance->setOverallTimeout($timeout);

$this->assertSame($timeout, $this->instance->getOverallTimeout());
}
}
31 changes: 31 additions & 0 deletions tests/ClientAdapter/Psr18AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,35 @@ public function testResolveResponse(): void

self::assertSame(200, $resolvedResponse->getStatusCode());
}

public function testSetConnectionTimeout(): void
{
self::expectException(\RuntimeException::class);

$this->psr18Adapter->setConnectionTimeout(1.0);
}


public function testGetConnectionTimeout(): void
{
self::expectException(\RuntimeException::class);

$this->psr18Adapter->getConnectionTimeout();
}


public function testSetOverallTimeout(): void
{
self::expectException(\RuntimeException::class);

$this->psr18Adapter->setOverallTimeout(1.0);
}


public function testGetOverallTimeout(): void
{
self::expectException(\RuntimeException::class);

$this->psr18Adapter->getOverallTimeout();
}
}

0 comments on commit bd71df8

Please sign in to comment.