Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for timeouts #59

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:

strategy:
matrix:
php: ['8.0', '8.1', '8.2', '8.3']
php: ['8.1', '8.2', '8.3']

name: PHP ${{ matrix.php }}

Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ You can then build the images:
docker compose up --build -d
```

Now you can run commands needed to work on the project. For example, say you want to install the dependencies on PHP 8.0:
Now you can run commands needed to work on the project. For example, say you want to install the dependencies on PHP 8.2:

```console
docker compose run php-80 composer install
docker compose run php-82 composer install
```

or for enter in the container

```console
docker compose exec php-80 sh
docker compose exec php-82 sh
```

## Testing
Expand Down
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ $request->events = [$event];
$response = $braze->users()->track($request, false);
```

You can also set connection and overall timeout.

```php
$braze->setConnectionTimeout(2.0);
$braze->setOverallTimeout(3.0);
```

Currently, SymfonyAdapter supports them, while Psr18Adapter does not.

You can see a few complete [examples](./examples) in the repository.

### Custom user attributes
Expand Down Expand Up @@ -185,11 +194,11 @@ The response objects will be filled with the values obtained only after the call

## Compatibility

| Version | Status | PHP compatibility |
|--- |--- |--- |
| 1.x | maintained | `>=7.2` |
| 2.x | maintained | `>=8.0` |

| Version | Status | PHP compatibility |
|---------|--------------|-------------------|
| 1.x | unmaintained | `>=7.2` |
| 2.x | maintained | `>=8.0` |
| 3.x | maintained | `>=8.1` |

## Requirements

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"ext-json": "*",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0 || ^2.0",
"symfony/http-client-contracts": "^2.4 || ^3.0"
},
"require-dev": {
"symfony/http-client": "^5.3 || ^6.0",
"symfony/http-client": "^5.3 || ^6.0 || ^7.0",
"nyholm/psr7": "^1.4",
"phpunit/phpunit": "^8.5 || ^9.3",
"phpunit/phpunit": "^9.3",
"friendsofphp/php-cs-fixer": "^3.3"
},
"suggest": {
"symfony/http-client": "^5.3 || ^6.0",
"symfony/http-client": "^5.3 || ^6.0 || ^7.0",
"nyholm/psr7": "^1.4",
"psr/http-client-implementation": "^1.0",
"psr/http-factory-implementation": "^1.0",
Expand Down
10 changes: 3 additions & 7 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
services:
php-80: &php
build: docker/php-8.0
container_name: braze-php-sdk-8.0
php-81: &php
build: docker/php-8.1
container_name: braze-php-sdk-8.1
working_dir: /app
tty: true
volumes:
- .:/app:cached
php-81:
<<: *php
build: docker/php-8.1
container_name: braze-php-sdk-8.1
php-82:
<<: *php
build: docker/php-8.2
Expand Down
3 changes: 0 additions & 3 deletions docker/php-8.0/Dockerfile

This file was deleted.

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();
}
}
Loading