Skip to content

Commit

Permalink
feat: adding php cac and experimentation client wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
namitgoel committed Jul 29, 2024
1 parent 710c69d commit 94198e0
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 0 deletions.
31 changes: 31 additions & 0 deletions clients/php/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## [<u> CAC Client </u>](./cacclient/client.php)

1. This exports a class that exposes functions that internally call rust functions.
2. For Different platform it read different rust files.
* <span style="color: #808080" >For Mac </span> -> libcac_client.dylib
* <span style="color: #357EC7" >For Windows </span> -> libcac_client.so
* <span style="color: orange" >For Linux </span> -> libcac_client.dll
3. This run CAC CLient in two thread one is main thread another is worker thread.
4. Polling updates for config are done on different thread. ([ref](./cacclient/client.php#L63)).


## [<u> Experimentation Client </u>](./expclient/client.php)

1. This exports a class that exposes functions that internally call rust functions.
2. For Different platform it read different rust files.
* <span style="color: #808080" >For Mac </span> -> libexperimentation_client.dylib
* <span style="color: #357EC7" >For Windows </span> -> libexperimentation_client.so
* <span style="color: orange" >For Linux </span> -> libexperimentation_client.dll
3. This run Experimentation CLient in two thread one is main thread another is worker thread.
4. Polling updates for experiments are done on different thread. ([ref](./expclient/client.php#L58)).


## [<u> Test </u>](./server.php)

1. To test this sample project follow below steps.
* Run superposition client.
* Run <u> **php server.php** </u> this will start a server that runs on port 8002.
2. By Default this sample code uses [dev](./server.php#L5) tenant.
3. By Default this sample code assumes superposition is running on [8080](./server.php#L7) port.
3. By Default this sample code polls superposition every [1 second](./server.php#L6) port.
4. This sample code creates both [CAC CLient](./server.php#L9) and [Experimentation Client](./server.php#L10) with above default values.
116 changes: 116 additions & 0 deletions clients/php/cacclient/client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
class CACClient
{
private $tenant = null;
private $polling_frequency = null;
private $cac_host_name = null;
public $rustFFI;

function __construct($tenant_name, $polling_frequency, $cac_host_name)
{
$this->tenant = $tenant_name;
$this->polling_frequency = $polling_frequency;
$this->cac_host_name = $cac_host_name;
if (!file_exists("/Users/namit.goel/Desktop/repos/namit_superposition/superposition/target/debug/libcac_client.dylib")) {
throw new InvalidArgumentException("Library file does not exist:");
}
$this->rustFFI = FFI::cdef("
int cac_last_error_length();
char* cac_last_error_message();
void cac_free_string(char* str);
int cac_new_client(char* tenant, int update_frequency, char* hostname);
void cac_start_polling_update(char* tenant);
void cac_free_client(char* tenantPtr);
char* cac_get_client(char* tenant);
char* cac_get_last_modified(char* tenantPtr);
char* cac_get_config(char* tenantPtr, char* filter_query, char* filter_prefix);
char* cac_get_resolved_config(char* tenantPtr, char* query, char* filter_keys, char* merge_strategy);
char* cac_get_default_config(char* tenantPtr, char* filter_keys);
", "/Users/namit.goel/Desktop/repos/namit_superposition/superposition/target/debug/libcac_client.dylib");
}

function get_cac_last_error_length()
{
return $this->rustFFI->cac_last_error_length();
}

function get_cac_last_error_message()
{
try {
$resp = $this->rustFFI->cac_last_error_message();
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}

function free_cac_string($str)
{
$this->rustFFI->cac_free_string($str);
}

function create_new_cac_client()
{
$resp = $this->rustFFI->cac_new_client($this->tenant, $this->polling_frequency, $this->cac_host_name);
if ($resp == 1) {
echo $this->get_cac_last_error_message();
}
return $resp;
}

function start_cac_polling_update()
{
$this->rustFFI->cac_start_polling_update($this->tenant);
}

function get_cac_client()
{
return $this->rustFFI->cac_get_client($this->tenant);
}

function free_cac_client()
{
$this->rustFFI->cac_free_client($this->get_cac_client());
}

function get_last_cac_modified()
{
try {
$resp = $this->rustFFI->cac_get_last_modified($this->get_cac_client());
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}

function get_cac_config($filter_query, $filter_prefix)
{
try {
$resp = $this->rustFFI->cac_get_config($this->get_cac_client(), $filter_query, $filter_prefix);
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}

function get_cac_resolved_config($tenantPtr, $query, $filter_keys, $merge_strategy)
{
try {
$resp = $this->rustFFI->cac_get_resolved_config($this->get_cac_client(), $query, $filter_keys, $merge_strategy);
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}

function get_cac_default_config($filter_keys)
{
try {
$resp = $this->rustFFI->cac_get_default_config($this->get_cac_client(), $filter_keys);
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}
}
?>
105 changes: 105 additions & 0 deletions clients/php/expclient/client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
class ExperimentationClient
{
private $tenant = null;
private $polling_frequency = null;
private $cac_host_name = null;
public $rustFFI;

function __construct($tenant_name, $polling_frequency, $cac_host_name)
{
$this->tenant = $tenant_name;
$this->polling_frequency = $polling_frequency;
$this->cac_host_name = $cac_host_name;
if (!file_exists("/Users/namit.goel/Desktop/repos/namit_superposition/superposition/target/debug/libexperimentation_client.dylib")) {
throw new InvalidArgumentException("Library file does not exist:");
}
$this->rustFFI = FFI::cdef("
int expt_last_error_length();
char* expt_last_error_message();
void expt_free_string(char* str);
int expt_new_client(char* tenant, int update_frequency, char* hostname);
void expt_start_polling_update(char* tenant);
void expt_free_client(char* exp_ptr);
char* expt_get_client(char* tenant);
char* expt_get_applicable_variant(char* exp_ptr, char* c_context, int toss);
char* expt_get_satisfied_experiments(char* exp_ptr, char* c_context, char* filter_prefix);
char* expt_get_filtered_satisfied_experiments(char* exp_ptr, char* c_context, char* filter_prefix);
char* expt_get_running_experiments(char* exp_ptr);
", "/Users/namit.goel/Desktop/repos/namit_superposition/superposition/target/debug/libexperimentation_client.dylib");
}

function get_expt_last_error_length() {
return $this->rustFFI->expt_last_error_length();
}

function get_expt_last_error_message() {
try {
$resp = $this->rustFFI->expt_last_error_message();
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}

function free_expt_string() {
$this->rustFFI->expt_free_string();
}

function create_expt_new_client() {
$resp = $this->rustFFI->expt_new_client($this->tenant, $this->polling_frequency, $this->cac_host_name);
if ($resp == 1) {
echo $this->get_expt_last_error_message();
}
return $resp;
}

function start_expt_polling_update() {
$this->rustFFI->expt_start_polling_update($this->tenant);
}

function get_expt_client() {
return $this->rustFFI->expt_get_client($this->tenant);
}

function free_exp_client() {
$this->rustFFI->expt_free_client($this->get_expt_client());
}

function get_expt_applicable_variant($context, $toss) {
try {
$resp = $this->rustFFI->expt_get_applicable_variant($this->get_expt_client(), $context, $toss);
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}

function expt_get_satisfied_experiments($context, $filter_prefix) {
try {
$resp = $this->rustFFI->expt_get_satisfied_experiments($this->get_expt_client(), $context, $filter_prefix);
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}

function get_expt_filtered_satisfied_experiments($context, $filter_prefix) {
try {
$resp = $this->rustFFI->expt_get_filtered_satisfied_experiments($this->get_expt_client(), $context, $filter_prefix);
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}

function get_expt_running_experiments() {
try {
$resp = $this->rustFFI->expt_get_running_experiments($this->get_expt_client());
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}
}
?>
22 changes: 22 additions & 0 deletions clients/php/server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
require_once "./cacclient/client.php";
require_once "./expclient/client.php";

$tenant_name = "dev";
$polling_frequency = 1;
$cac_host_name = "http://localhost:8080";

$cac_client = new CACClient($tenant_name, $polling_frequency, $cac_host_name);
$exp_client = new ExperimentationClient($tenant_name, $polling_frequency, $cac_host_name);

$cac_client->create_new_cac_client();
$exp_client->create_expt_new_client();

$cac_client-> start_cac_polling_update();
sleep(5);
$resp_cac = $cac_client->get_cac_config("{}", "");
$resp_exp = $exp_client->get_expt_running_experiments();
echo $resp_cac;
echo "\n";
echo $resp_exp;
?>

0 comments on commit 94198e0

Please sign in to comment.