Skip to content

Commit

Permalink
fix: Update Remote fetch variants to use flag keys array (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc authored Nov 30, 2023
1 parent 97cfd38 commit b5337c6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
20 changes: 10 additions & 10 deletions src/Remote/RemoteEvaluationClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,24 @@ public function __construct(string $apiKey, ?RemoteEvaluationConfig $config = nu
* This method will automatically retry if configured (default).
*
* @param $user User The {@link User} context
* @param $options ?FetchOptions The {@link FetchOptions} for this specific fetch request.
* @param $flagKeys array The flags to evaluate for this specific fetch request.
* @return PromiseInterface A {@link Variant} array for the user on success, empty array on error.
* @throws Exception
*/
public function fetch(User $user, ?FetchOptions $options = null): PromiseInterface
public function fetch(User $user, array $flagKeys = []): PromiseInterface
{
if ($user->userId == null && $user->deviceId == null) {
$this->logger->warning('[Experiment] user id and device id are null; Amplitude may not resolve identity');
}
$this->logger->debug('[Experiment] Fetching variants for user: ' . json_encode($user->toArray()));

return $this->doFetch($user, $this->config->fetchTimeoutMillis, $options)
->otherwise(function (Throwable $e) use ($user, $options) {
return $this->doFetch($user, $this->config->fetchTimeoutMillis, $flagKeys)
->otherwise(function (Throwable $e) use ($user, $flagKeys) {
// Handle the exception
$this->logger->error('[Experiment] Fetch variant failed: ' . $e->getMessage());

// Retry the fetch
return $this->retryFetch($user, $options)
return $this->retryFetch($user, $flagKeys)
->then(function ($result) {
// Process the result if retry is successful
return $result;
Expand All @@ -79,7 +79,7 @@ public function fetch(User $user, ?FetchOptions $options = null): PromiseInterfa
});
}

public function doFetch(User $user, int $timeoutMillis, ?FetchOptions $options = null): PromiseInterface
public function doFetch(User $user, int $timeoutMillis, array $flagKeys = []): PromiseInterface
{
// Define the request data
$libraryUser = $user->copyToBuilder()->library('experiment-php-server/' . VERSION)->build();
Expand All @@ -95,8 +95,8 @@ public function doFetch(User $user, int $timeoutMillis, ?FetchOptions $options =
'X-Amp-Exp-User' => $serializedUser,
];

if ($options && $options->flagKeys) {
$headers['X-Amp-Exp-Flag-Keys'] = base64_encode(json_encode($options->flagKeys));
if (!empty($flagKeys)) {
$headers['X-Amp-Exp-Flag-Keys'] = base64_encode(json_encode($flagKeys));
}

$promise = $this->httpClient->requestAsync('GET', $endpoint, [
Expand Down Expand Up @@ -124,7 +124,7 @@ function (Exception $reason) {
/**
* @throws Exception
*/
private function retryFetch(User $user, ?FetchOptions $options = null): PromiseInterface
private function retryFetch(User $user, array $flagKeys = []): PromiseInterface
{
if ($this->config->fetchRetries == 0) {
return Create::promiseFor([]);
Expand All @@ -142,7 +142,7 @@ private function retryFetch(User $user, ?FetchOptions $options = null): PromiseI
return $this->doFetch(
$user,
$this->config->fetchRetryTimeoutMillis,
$options
$flagKeys
)->then(
function ($result) {
return $result;
Expand Down
3 changes: 1 addition & 2 deletions tests/Remote/RemoteEvaluationClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace AmplitudeExperiment\Test\Remote;

use AmplitudeExperiment\Experiment;
use AmplitudeExperiment\Remote\FetchOptions;
use AmplitudeExperiment\Remote\RemoteEvaluationClient;
use AmplitudeExperiment\Remote\RemoteEvaluationConfig;
use AmplitudeExperiment\User;
Expand Down Expand Up @@ -89,7 +88,7 @@ public function testFetchRetryOnceTimeoutFirstThenSucceedWithZeroBackoff()
public function testFetchWithFlagKeysOptionsSuccess()
{
$client = new RemoteEvaluationClient($this->apiKey);
$variants = $client->fetch($this->testUser, new FetchOptions(['sdk-ci-test']))->wait();
$variants = $client->fetch($this->testUser, ['sdk-ci-test'])->wait();
$variant = $variants['sdk-ci-test'];
$this->assertEquals(1, sizeof($variants));
$this->assertEquals("on", $variant->key);
Expand Down

0 comments on commit b5337c6

Please sign in to comment.