diff --git a/src/Amplitude/Amplitude.php b/src/Amplitude/Amplitude.php index 44c20f2..15c81a4 100644 --- a/src/Amplitude/Amplitude.php +++ b/src/Amplitude/Amplitude.php @@ -2,8 +2,8 @@ namespace AmplitudeExperiment\Amplitude; -use AmplitudeExperiment\Http\FetchClientInterface; -use AmplitudeExperiment\Http\GuzzleFetchClient; +use AmplitudeExperiment\Http\HttpClientInterface; +use AmplitudeExperiment\Http\GuzzleHttpClient; use Psr\Http\Client\ClientExceptionInterface; use Psr\Log\LoggerInterface; @@ -17,7 +17,7 @@ class Amplitude * @var array> */ protected array $queue = []; - protected FetchClientInterface $httpClient; + protected HttpClientInterface $httpClient; private LoggerInterface $logger; private AmplitudeConfig $config; @@ -26,7 +26,7 @@ public function __construct(string $apiKey, LoggerInterface $logger, AmplitudeCo $this->apiKey = $apiKey; $this->logger = $logger; $this->config = $config ?? AmplitudeConfig::builder()->build(); - $this->httpClient = $this->config->fetchClient ?? $this->config->fetchClient ?? new GuzzleFetchClient($this->config->guzzleClientConfig); + $this->httpClient = $this->config->fetchClient ?? $this->config->fetchClient ?? new GuzzleHttpClient($this->config->guzzleClientConfig); } public function flush(): void diff --git a/src/Amplitude/AmplitudeConfig.php b/src/Amplitude/AmplitudeConfig.php index 1beea8e..04b73be 100644 --- a/src/Amplitude/AmplitudeConfig.php +++ b/src/Amplitude/AmplitudeConfig.php @@ -4,7 +4,7 @@ use AmplitudeExperiment\Assignment\AssignmentConfig; use AmplitudeExperiment\Assignment\AssignmentConfigBuilder; -use AmplitudeExperiment\Http\FetchClientInterface; +use AmplitudeExperiment\Http\HttpClientInterface; /** * Configuration options for Amplitude. The Amplitude object is created when you create an {@link AssignmentConfig}. @@ -34,12 +34,12 @@ class AmplitudeConfig */ public bool $useBatch; /** - * The underlying HTTP client to use for requests, if this is not set, the default {@link GuzzleFetchClient} will be used. + * The underlying HTTP client to use for requests, if this is not set, the default {@link GuzzleHttpClient} will be used. */ - public ?FetchClientInterface $fetchClient; + public ?HttpClientInterface $fetchClient; /** * @var array - * The configuration for the underlying default {@link GuzzleFetchClient} client (if used). See {@link GUZZLE_DEFAULTS} for defaults. + * The configuration for the underlying default {@link GuzzleHttpClient} client (if used). See {@link GUZZLE_DEFAULTS} for defaults. */ public array $guzzleClientConfig; @@ -67,13 +67,13 @@ class AmplitudeConfig * @param array $guzzleClientConfig */ public function __construct( - int $flushQueueSize, - int $minIdLength, - string $serverZone, - string $serverUrl, - bool $useBatch, - ?FetchClientInterface $fetchClient, - array $guzzleClientConfig + int $flushQueueSize, + int $minIdLength, + string $serverZone, + string $serverUrl, + bool $useBatch, + ?HttpClientInterface $fetchClient, + array $guzzleClientConfig ) { $this->flushQueueSize = $flushQueueSize; diff --git a/src/Amplitude/AmplitudeConfigBuilder.php b/src/Amplitude/AmplitudeConfigBuilder.php index 0c2b651..0ea1cc0 100644 --- a/src/Amplitude/AmplitudeConfigBuilder.php +++ b/src/Amplitude/AmplitudeConfigBuilder.php @@ -2,7 +2,7 @@ namespace AmplitudeExperiment\Amplitude; -use AmplitudeExperiment\Http\FetchClientInterface; +use AmplitudeExperiment\Http\HttpClientInterface; class AmplitudeConfigBuilder { @@ -11,7 +11,7 @@ class AmplitudeConfigBuilder protected string $serverZone = AmplitudeConfig::DEFAULTS['serverZone']; protected ?string $serverUrl = null; protected bool $useBatch = AmplitudeConfig::DEFAULTS['useBatch']; - protected ?FetchClientInterface $fetchClient = AmplitudeConfig::DEFAULTS['fetchClient']; + protected ?HttpClientInterface $fetchClient = AmplitudeConfig::DEFAULTS['fetchClient']; /** * @var array */ @@ -51,7 +51,7 @@ public function useBatch(bool $useBatch): AmplitudeConfigBuilder return $this; } - public function fetchClient(FetchClientInterface $fetchClient): AmplitudeConfigBuilder + public function fetchClient(HttpClientInterface $fetchClient): AmplitudeConfigBuilder { $this->fetchClient = $fetchClient; return $this; diff --git a/src/Flag/FlagConfigFetcher.php b/src/Flag/FlagConfigFetcher.php index b724474..25a08ca 100644 --- a/src/Flag/FlagConfigFetcher.php +++ b/src/Flag/FlagConfigFetcher.php @@ -2,7 +2,7 @@ namespace AmplitudeExperiment\Flag; -use AmplitudeExperiment\Http\FetchClientInterface; +use AmplitudeExperiment\Http\HttpClientInterface; use AmplitudeExperiment\Local\LocalEvaluationConfig; use Psr\Http\Client\ClientExceptionInterface; use Psr\Log\LoggerInterface; @@ -14,9 +14,9 @@ class FlagConfigFetcher private LoggerInterface $logger; private string $apiKey; private string $serverUrl; - private FetchClientInterface $httpClient; + private HttpClientInterface $httpClient; - public function __construct(string $apiKey, LoggerInterface $logger, FetchClientInterface $fetchClient, string $serverUrl = LocalEvaluationConfig::DEFAULTS["serverUrl"]) + public function __construct(string $apiKey, LoggerInterface $logger, HttpClientInterface $fetchClient, string $serverUrl = LocalEvaluationConfig::DEFAULTS["serverUrl"]) { $this->apiKey = $apiKey; $this->serverUrl = $serverUrl; diff --git a/src/Http/GuzzleFetchClient.php b/src/Http/GuzzleHttpClient.php similarity index 73% rename from src/Http/GuzzleFetchClient.php rename to src/Http/GuzzleHttpClient.php index c83e15b..d9046e1 100644 --- a/src/Http/GuzzleFetchClient.php +++ b/src/Http/GuzzleHttpClient.php @@ -14,35 +14,35 @@ /** * The request socket timeout, in milliseconds. */ - 'fetchTimeoutMillis' => 10000, + 'timeoutMillis' => 10000, /** * The number of retries to attempt before failing */ - 'fetchRetries' => 8, + 'retries' => 8, /** * Retry backoff minimum (starting backoff delay) in milliseconds. The minimum backoff is scaled by - * `fetchRetryBackoffScalar` after each retry failure. + * `retryBackoffScalar` after each retry failure. */ - 'fetchRetryBackoffMinMillis' => 500, + 'retryBackoffMinMillis' => 500, /** * Retry backoff maximum in milliseconds. If the scaled backoff is greater than the max, the max is * used for all subsequent retries. */ - 'fetchRetryBackoffMaxMillis' => 10000, + 'retryBackoffMaxMillis' => 10000, /** * Scales the minimum backoff exponentially. */ - 'fetchRetryBackoffScalar' => 1.5, + 'retryBackoffScalar' => 1.5, /** * The request timeout for retrying fetch requests. */ - 'fetchRetryTimeoutMillis' => 10000 + 'retryTimeoutMillis' => 10000 ]; /** - * A default FetchClientInterface implementation that uses Guzzle. + * A default {@link HttpClientInterface} implementation that uses Guzzle. */ -class GuzzleFetchClient implements FetchClientInterface +class GuzzleHttpClient implements HttpClientInterface { private Client $client; /** @@ -62,7 +62,7 @@ public function __construct(array $config) $handlerStack->push(Middleware::retry( function ($retries, Request $request, $response = null, $exception = null) { // Retry if the maximum number of retries is not reached and an exception occurred - return $retries < $this->config['fetchRetries'] && $exception instanceof Exception; + return $retries < $this->config['retries'] && $exception instanceof Exception; }, function ($retries) { // Calculate delay @@ -71,7 +71,7 @@ function ($retries) { )); // Create a Guzzle client with the custom handler stack - $this->client = new Client(['handler' => $handlerStack, RequestOptions::TIMEOUT => $this->config['fetchTimeoutMillis'] / 1000]); + $this->client = new Client(['handler' => $handlerStack, RequestOptions::TIMEOUT => $this->config['timeoutMillis'] / 1000]); } public function getClient(): ClientInterface @@ -87,12 +87,12 @@ public function createRequest(string $method, string $uri, ?string $body = null) protected function calculateDelayMillis(int $iteration): int { - $delayMillis = $this->config['fetchRetryBackoffMinMillis']; + $delayMillis = $this->config['retryBackoffMinMillis']; for ($i = 1; $i < $iteration; $i++) { $delayMillis = min( - $delayMillis * $this->config['fetchRetryBackoffScalar'], - $this->config['fetchRetryBackoffMaxMillis'] + $delayMillis * $this->config['retryBackoffScalar'], + $this->config['retryBackoffMaxMillis'] ); } return $delayMillis; diff --git a/src/Http/FetchClientInterface.php b/src/Http/HttpClientInterface.php similarity index 93% rename from src/Http/FetchClientInterface.php rename to src/Http/HttpClientInterface.php index 7b5a778..50de325 100644 --- a/src/Http/FetchClientInterface.php +++ b/src/Http/HttpClientInterface.php @@ -6,7 +6,7 @@ use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestInterface; -interface FetchClientInterface +interface HttpClientInterface { /** * Return the underlying PSR HTTP Client diff --git a/src/Local/LocalEvaluationClient.php b/src/Local/LocalEvaluationClient.php index 1a45b4c..7380c31 100644 --- a/src/Local/LocalEvaluationClient.php +++ b/src/Local/LocalEvaluationClient.php @@ -10,7 +10,7 @@ use AmplitudeExperiment\EvaluationCore\EvaluationEngine; use AmplitudeExperiment\Flag\FlagConfigFetcher; use AmplitudeExperiment\Flag\FlagConfigService; -use AmplitudeExperiment\Http\GuzzleFetchClient; +use AmplitudeExperiment\Http\GuzzleHttpClient; use AmplitudeExperiment\Logger\DefaultLogger; use AmplitudeExperiment\Logger\InternalLogger; use AmplitudeExperiment\User; @@ -36,7 +36,7 @@ public function __construct(string $apiKey, ?LocalEvaluationConfig $config = nul { $this->config = $config ?? LocalEvaluationConfig::builder()->build(); $this->logger = new InternalLogger($this->config->logger ?? new DefaultLogger(), $this->config->logLevel); - $httpClient = $config->fetchClient ?? $this->config->fetchClient ?? new GuzzleFetchClient($this->config->guzzleClientConfig); + $httpClient = $config->fetchClient ?? $this->config->fetchClient ?? new GuzzleHttpClient($this->config->guzzleClientConfig); $fetcher = new FlagConfigFetcher($apiKey, $this->logger, $httpClient, $this->config->serverUrl); $this->flagConfigService = new FlagConfigService($fetcher, $this->logger, $this->config->bootstrap); $this->initializeAssignmentService($this->config->assignmentConfig); diff --git a/src/Local/LocalEvaluationConfig.php b/src/Local/LocalEvaluationConfig.php index 23f4660..b9dbb06 100644 --- a/src/Local/LocalEvaluationConfig.php +++ b/src/Local/LocalEvaluationConfig.php @@ -3,7 +3,7 @@ namespace AmplitudeExperiment\Local; use AmplitudeExperiment\Assignment\AssignmentConfig; -use AmplitudeExperiment\Http\FetchClientInterface; +use AmplitudeExperiment\Http\HttpClientInterface; use AmplitudeExperiment\Logger\LogLevel; use Psr\Log\LoggerInterface; @@ -37,12 +37,12 @@ class LocalEvaluationConfig public array $bootstrap; public ?AssignmentConfig $assignmentConfig; /** - * The underlying HTTP client to use for requests, if this is not set, the default {@link GuzzleFetchClient} will be used. + * The underlying HTTP client to use for requests, if this is not set, the default {@link GuzzleHttpClient} will be used. */ - public ?FetchClientInterface $fetchClient; + public ?HttpClientInterface $fetchClient; /** * @var array - * The configuration for the underlying default {@link GuzzleFetchClient} client (if used). See {@link GUZZLE_DEFAULTS} for defaults. + * The configuration for the underlying default {@link GuzzleHttpClient} client (if used). See {@link GUZZLE_DEFAULTS} for defaults. */ public array $guzzleClientConfig; @@ -60,7 +60,7 @@ class LocalEvaluationConfig * @param array $guzzleClientConfig * @param array $bootstrap */ - public function __construct(?LoggerInterface $logger, int $logLevel, string $serverUrl, array $bootstrap, ?AssignmentConfig $assignmentConfig, ?FetchClientInterface $fetchClient, array $guzzleClientConfig) + public function __construct(?LoggerInterface $logger, int $logLevel, string $serverUrl, array $bootstrap, ?AssignmentConfig $assignmentConfig, ?HttpClientInterface $fetchClient, array $guzzleClientConfig) { $this->logger = $logger; $this->logLevel = $logLevel; diff --git a/src/Local/LocalEvaluationConfigBuilder.php b/src/Local/LocalEvaluationConfigBuilder.php index 901e616..0014156 100644 --- a/src/Local/LocalEvaluationConfigBuilder.php +++ b/src/Local/LocalEvaluationConfigBuilder.php @@ -3,7 +3,7 @@ namespace AmplitudeExperiment\Local; use AmplitudeExperiment\Assignment\AssignmentConfig; -use AmplitudeExperiment\Http\FetchClientInterface; +use AmplitudeExperiment\Http\HttpClientInterface; use Psr\Log\LoggerInterface; class LocalEvaluationConfigBuilder @@ -16,7 +16,7 @@ class LocalEvaluationConfigBuilder */ protected array $bootstrap = LocalEvaluationConfig::DEFAULTS['bootstrap']; protected ?AssignmentConfig $assignmentConfig = LocalEvaluationConfig::DEFAULTS['assignmentConfig']; - protected ?FetchClientInterface $fetchClient = LocalEvaluationConfig::DEFAULTS['fetchClient']; + protected ?HttpClientInterface $fetchClient = LocalEvaluationConfig::DEFAULTS['fetchClient']; /** * @var array */ @@ -59,7 +59,7 @@ public function assignmentConfig(AssignmentConfig $assignmentConfig): LocalEvalu return $this; } - public function fetchClient(FetchClientInterface $fetchClient): LocalEvaluationConfigBuilder + public function fetchClient(HttpClientInterface $fetchClient): LocalEvaluationConfigBuilder { $this->fetchClient = $fetchClient; return $this; diff --git a/src/Remote/RemoteEvaluationClient.php b/src/Remote/RemoteEvaluationClient.php index 882bfb4..babed0f 100644 --- a/src/Remote/RemoteEvaluationClient.php +++ b/src/Remote/RemoteEvaluationClient.php @@ -2,8 +2,8 @@ namespace AmplitudeExperiment\Remote; -use AmplitudeExperiment\Http\FetchClientInterface; -use AmplitudeExperiment\Http\GuzzleFetchClient; +use AmplitudeExperiment\Http\HttpClientInterface; +use AmplitudeExperiment\Http\GuzzleHttpClient; use AmplitudeExperiment\Logger\DefaultLogger; use AmplitudeExperiment\Logger\InternalLogger; use AmplitudeExperiment\User; @@ -22,7 +22,7 @@ class RemoteEvaluationClient { private string $apiKey; private RemoteEvaluationConfig $config; - private FetchClientInterface $httpClient; + private HttpClientInterface $httpClient; private LoggerInterface $logger; /** @@ -35,7 +35,7 @@ public function __construct(string $apiKey, ?RemoteEvaluationConfig $config = nu { $this->apiKey = $apiKey; $this->config = $config ?? RemoteEvaluationConfig::builder()->build(); - $this->httpClient = $config->fetchClient ?? $this->config->fetchClient ?? new GuzzleFetchClient($this->config->guzzleClientConfig); + $this->httpClient = $config->fetchClient ?? $this->config->fetchClient ?? new GuzzleHttpClient($this->config->guzzleClientConfig); $this->logger = new InternalLogger($this->config->logger ?? new DefaultLogger(), $this->config->logLevel); } diff --git a/src/Remote/RemoteEvaluationConfig.php b/src/Remote/RemoteEvaluationConfig.php index 2a7971c..ed02cd9 100644 --- a/src/Remote/RemoteEvaluationConfig.php +++ b/src/Remote/RemoteEvaluationConfig.php @@ -2,7 +2,7 @@ namespace AmplitudeExperiment\Remote; -use AmplitudeExperiment\Http\FetchClientInterface; +use AmplitudeExperiment\Http\HttpClientInterface; use AmplitudeExperiment\Logger\DefaultLogger; use AmplitudeExperiment\Logger\LogLevel; use Psr\Log\LoggerInterface; @@ -30,12 +30,12 @@ class RemoteEvaluationConfig */ public string $serverUrl; /** - * The underlying HTTP client to use for requests, if this is not set, the default {@link GuzzleFetchClient} will be used. + * The underlying HTTP client to use for requests, if this is not set, the default {@link GuzzleHttpClient} will be used. */ - public ?FetchClientInterface $fetchClient; + public ?HttpClientInterface $fetchClient; /** * @var array - * The configuration for the underlying default {@link GuzzleFetchClient} (if used). See {@link GUZZLE_DEFAULTS} for defaults. + * The configuration for the underlying default {@link GuzzleHttpClient} (if used). See {@link GUZZLE_DEFAULTS} for defaults. */ public array $guzzleClientConfig; @@ -53,11 +53,11 @@ class RemoteEvaluationConfig * @param array $guzzleClientConfig */ public function __construct( - ?LoggerInterface $logger, - int $logLevel, - string $serverUrl, - ?FetchClientInterface $fetchClient, - array $guzzleClientConfig + ?LoggerInterface $logger, + int $logLevel, + string $serverUrl, + ?HttpClientInterface $fetchClient, + array $guzzleClientConfig ) { $this->logger = $logger; diff --git a/src/Remote/RemoteEvaluationConfigBuilder.php b/src/Remote/RemoteEvaluationConfigBuilder.php index 1b64c5b..1c18d98 100644 --- a/src/Remote/RemoteEvaluationConfigBuilder.php +++ b/src/Remote/RemoteEvaluationConfigBuilder.php @@ -2,7 +2,7 @@ namespace AmplitudeExperiment\Remote; -use AmplitudeExperiment\Http\FetchClientInterface; +use AmplitudeExperiment\Http\HttpClientInterface; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; @@ -12,7 +12,7 @@ class RemoteEvaluationConfigBuilder protected int $logLevel = RemoteEvaluationConfig::DEFAULTS['logLevel']; protected bool $debug = RemoteEvaluationConfig::DEFAULTS['debug']; protected string $serverUrl = RemoteEvaluationConfig::DEFAULTS['serverUrl']; - protected ?FetchClientInterface $fetchClient = RemoteEvaluationConfig::DEFAULTS['fetchClient']; + protected ?HttpClientInterface $fetchClient = RemoteEvaluationConfig::DEFAULTS['fetchClient']; /** * @var array */ @@ -40,7 +40,7 @@ public function serverUrl(string $serverUrl): RemoteEvaluationConfigBuilder return $this; } - public function fetchClient(FetchClientInterface $fetchClient): RemoteEvaluationConfigBuilder + public function fetchClient(HttpClientInterface $fetchClient): RemoteEvaluationConfigBuilder { $this->fetchClient = $fetchClient; return $this; diff --git a/tests/Amplitude/AmplitudeTest.php b/tests/Amplitude/AmplitudeTest.php index 9304f4c..694b8a3 100644 --- a/tests/Amplitude/AmplitudeTest.php +++ b/tests/Amplitude/AmplitudeTest.php @@ -7,7 +7,7 @@ use AmplitudeExperiment\Logger\DefaultLogger; use AmplitudeExperiment\Logger\InternalLogger; use AmplitudeExperiment\Logger\LogLevel; -use AmplitudeExperiment\Test\Util\MockGuzzleFetchClient; +use AmplitudeExperiment\Test\Util\MockGuzzleHttpClient; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; @@ -58,7 +58,7 @@ public function testEmptyQueueAfterFlushSuccess() new Response(200, ['X-Foo' => 'Bar']), ]); $handlerStack = HandlerStack::create($mock); - $httpClient = new MockGuzzleFetchClient([], $handlerStack); + $httpClient = new MockGuzzleHttpClient([], $handlerStack); $client->setHttpClient($httpClient); $event1 = new Event('test1'); $event2 = new Event('test2'); @@ -92,7 +92,7 @@ function (RequestInterface $request, array $options) use (&$requestCounter) { $handlerStack = HandlerStack::create($mockHandler); // Create an instance of GuzzleFetchClient with the custom handler stack - $fetchClient = new MockGuzzleFetchClient([], $handlerStack); + $fetchClient = new MockGuzzleHttpClient([], $handlerStack); $client->setHttpClient($fetchClient); $event1 = new Event('test1'); $event2 = new Event('test2'); @@ -119,7 +119,7 @@ public function testBackoffRetriesToFailure() })); $handlerStack = HandlerStack::create($mockHandler); - $httpClient = new MockGuzzleFetchClient(['fetchRetries' => 4], $handlerStack); + $httpClient = new MockGuzzleHttpClient(['retries' => 4], $handlerStack); $client->setHttpClient($httpClient); $event1 = new Event('test'); @@ -153,7 +153,7 @@ function (RequestInterface $request, array $options) use (&$requestCounter) { ]); $handlerStack = HandlerStack::create($mockHandler); - $httpClient = new MockGuzzleFetchClient(['fetchRetries' => 4], $handlerStack); + $httpClient = new MockGuzzleHttpClient(['retries' => 4], $handlerStack); $client->setHttpClient($httpClient); $event1 = new Event('test'); $event1->userId = 'user_id'; diff --git a/tests/Amplitude/MockAmplitude.php b/tests/Amplitude/MockAmplitude.php index fab3e05..dfa8e07 100644 --- a/tests/Amplitude/MockAmplitude.php +++ b/tests/Amplitude/MockAmplitude.php @@ -4,7 +4,7 @@ use AmplitudeExperiment\Amplitude\Amplitude; use AmplitudeExperiment\Amplitude\AmplitudeConfig; -use AmplitudeExperiment\Http\FetchClientInterface; +use AmplitudeExperiment\Http\HttpClientInterface; use Psr\Log\LoggerInterface; class MockAmplitude extends Amplitude @@ -13,7 +13,7 @@ public function __construct(string $apiKey, LoggerInterface $logger, AmplitudeCo { parent::__construct($apiKey, $logger, $config); } - public function setHttpClient(FetchClientInterface $httpClient) { + public function setHttpClient(HttpClientInterface $httpClient) { $this->httpClient = $httpClient; } public function __destruct() { diff --git a/tests/Remote/RemoteEvaluationClientTest.php b/tests/Remote/RemoteEvaluationClientTest.php index 4ff180c..db6a167 100644 --- a/tests/Remote/RemoteEvaluationClientTest.php +++ b/tests/Remote/RemoteEvaluationClientTest.php @@ -5,7 +5,7 @@ use AmplitudeExperiment\Experiment; use AmplitudeExperiment\Remote\RemoteEvaluationClient; use AmplitudeExperiment\Remote\RemoteEvaluationConfig; -use AmplitudeExperiment\Test\Util\MockGuzzleFetchClient; +use AmplitudeExperiment\Test\Util\MockGuzzleHttpClient; use AmplitudeExperiment\User; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Handler\MockHandler; @@ -38,7 +38,7 @@ public function testFetchSuccess() public function testFetchWithNoRetriesTimeoutFailure() { - $guzzleConfig = ['fetchRetries' => 0, 'fetchTimeoutMillis' => 1]; + $guzzleConfig = ['retries' => 0, 'timeoutMillis' => 1]; $config = RemoteEvaluationConfig::builder() ->guzzleClientConfig($guzzleConfig) ->build(); @@ -72,12 +72,12 @@ function (RequestInterface $request, array $options) use (&$requestCounter) { $handlerStack = HandlerStack::create($mockHandler); // Create an instance of GuzzleFetchClient with the custom handler stack - $fetchClient = new MockGuzzleFetchClient([ - 'fetchRetries' => 1, - 'fetchTimeoutMillis' => 10000, - 'fetchRetryBackoffMinMillis' => 100, - 'fetchRetryBackoffScalar' => 2, - 'fetchRetryBackoffMaxMillis' => 500, + $fetchClient = new MockGuzzleHttpClient([ + 'retries' => 1, + 'timeoutMillis' => 10000, + 'retryBackoffMinMillis' => 100, + 'retryBackoffScalar' => 2, + 'retryBackoffMaxMillis' => 500, ], $handlerStack); $client = new RemoteEvaluationClient($this->apiKey, RemoteEvaluationConfig::builder()->fetchClient($fetchClient)->build()); @@ -92,7 +92,7 @@ function (RequestInterface $request, array $options) use (&$requestCounter) { $this->assertEquals(2, $requestCounter); } - public function testFetchRetryOnceTimeoutFirstThenSucceedWithZeroBackoff() + public function testretryOnceTimeoutFirstThenSucceedWithZeroBackoff() { // Initialize the request counter $requestCounter = 0; @@ -117,12 +117,12 @@ function (RequestInterface $request, array $options) use (&$requestCounter) { $handlerStack = HandlerStack::create($mockHandler); // Create an instance of GuzzleFetchClient with the custom handler stack - $fetchClient = new MockGuzzleFetchClient([ - 'fetchRetries' => 1, - 'fetchTimeoutMillis' => 10000, - 'fetchRetryBackoffMinMillis' => 0, - 'fetchRetryBackoffScalar' => 2, - 'fetchRetryBackoffMaxMillis' => 0, + $fetchClient = new MockGuzzleHttpClient([ + 'retries' => 1, + 'timeoutMillis' => 10000, + 'retryBackoffMinMillis' => 0, + 'retryBackoffScalar' => 2, + 'retryBackoffMaxMillis' => 0, ], $handlerStack); $client = new RemoteEvaluationClient($this->apiKey, RemoteEvaluationConfig::builder()->fetchClient($fetchClient)->build()); diff --git a/tests/Util/MockGuzzleFetchClient.php b/tests/Util/MockGuzzleHttpClient.php similarity index 75% rename from tests/Util/MockGuzzleFetchClient.php rename to tests/Util/MockGuzzleHttpClient.php index 4b97bc9..d046f0d 100644 --- a/tests/Util/MockGuzzleFetchClient.php +++ b/tests/Util/MockGuzzleHttpClient.php @@ -2,7 +2,7 @@ namespace AmplitudeExperiment\Test\Util; -use AmplitudeExperiment\Http\FetchClientInterface; +use AmplitudeExperiment\Http\HttpClientInterface; use Exception; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; @@ -12,7 +12,7 @@ use Psr\Http\Client\ClientInterface; use const AmplitudeExperiment\Http\GUZZLE_DEFAULTS; -class MockGuzzleFetchClient implements FetchClientInterface +class MockGuzzleHttpClient implements HttpClientInterface { private Client $client; private array $config; @@ -25,7 +25,7 @@ public function __construct(array $config, HandlerStack $handlerStack) $handlerStack->push(Middleware::retry( function ($retries, Request $request, $response = null, $exception = null) { // Retry if the maximum number of retries is not reached and an exception occurred - return $retries < $this->config['fetchRetries'] && $exception instanceof Exception; + return $retries < $this->config['retries'] && $exception instanceof Exception; }, function ($retries) { // Calculate delay @@ -34,7 +34,7 @@ function ($retries) { )); // Create a Guzzle client with the custom handler stack - $this->client = new Client(['handler' => $handlerStack, RequestOptions::TIMEOUT => $this->config['fetchTimeoutMillis'] / 1000]); + $this->client = new Client(['handler' => $handlerStack, RequestOptions::TIMEOUT => $this->config['timeoutMillis'] / 1000]); } public function getClient(): ClientInterface @@ -49,12 +49,12 @@ public function createRequest(string $method, string $uri, ?string $body = null) protected function calculateDelayMillis($iteration): int { - $delayMillis = $this->config['fetchRetryBackoffMinMillis']; + $delayMillis = $this->config['retryBackoffMinMillis']; for ($i = 0; $i < $iteration; $i++) { $delayMillis = min( - $delayMillis * $this->config['fetchRetryBackoffScalar'], - $this->config['fetchRetryBackoffMaxMillis'] + $delayMillis * $this->config['retryBackoffScalar'], + $this->config['retryBackoffMaxMillis'] ); } return $delayMillis;