Skip to content

Commit

Permalink
refactor: custom HTTP client params naming
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc committed Jan 9, 2024
1 parent af5d592 commit b416164
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 91 deletions.
8 changes: 4 additions & 4 deletions src/Amplitude/Amplitude.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,7 +17,7 @@ class Amplitude
* @var array<array<string,mixed>>
*/
protected array $queue = [];
protected FetchClientInterface $httpClient;
protected HttpClientInterface $httpClient;
private LoggerInterface $logger;
private AmplitudeConfig $config;

Expand All @@ -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
Expand Down
22 changes: 11 additions & 11 deletions src/Amplitude/AmplitudeConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -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<string, mixed>
* 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;

Expand Down Expand Up @@ -67,13 +67,13 @@ class AmplitudeConfig
* @param array<string, mixed> $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;
Expand Down
6 changes: 3 additions & 3 deletions src/Amplitude/AmplitudeConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace AmplitudeExperiment\Amplitude;

use AmplitudeExperiment\Http\FetchClientInterface;
use AmplitudeExperiment\Http\HttpClientInterface;

class AmplitudeConfigBuilder
{
Expand All @@ -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<string, mixed>
*/
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Flag/FlagConfigFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
28 changes: 14 additions & 14 deletions src/Http/GuzzleFetchClient.php → src/Http/GuzzleHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/**
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;

interface FetchClientInterface
interface HttpClientInterface
{
/**
* Return the underlying PSR HTTP Client
Expand Down
4 changes: 2 additions & 2 deletions src/Local/LocalEvaluationClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/Local/LocalEvaluationConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<string, mixed>
* 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;

Expand All @@ -60,7 +60,7 @@ class LocalEvaluationConfig
* @param array<string, mixed> $guzzleClientConfig
* @param array<string, mixed> $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;
Expand Down
6 changes: 3 additions & 3 deletions src/Local/LocalEvaluationConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<string, mixed>
*/
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/Remote/RemoteEvaluationClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +22,7 @@ class RemoteEvaluationClient
{
private string $apiKey;
private RemoteEvaluationConfig $config;
private FetchClientInterface $httpClient;
private HttpClientInterface $httpClient;
private LoggerInterface $logger;

/**
Expand All @@ -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);
}

Expand Down
18 changes: 9 additions & 9 deletions src/Remote/RemoteEvaluationConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, mixed>
* 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;

Expand All @@ -53,11 +53,11 @@ class RemoteEvaluationConfig
* @param array<string, mixed> $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;
Expand Down
6 changes: 3 additions & 3 deletions src/Remote/RemoteEvaluationConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace AmplitudeExperiment\Remote;

use AmplitudeExperiment\Http\FetchClientInterface;
use AmplitudeExperiment\Http\HttpClientInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

Expand All @@ -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<string, mixed>
*/
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit b416164

Please sign in to comment.