Skip to content

Commit

Permalink
enhancement: Metadata (headers) support
Browse files Browse the repository at this point in the history
Signed-off-by: Oğuzhan Durgun <[email protected]>
  • Loading branch information
oguzhand95 committed Dec 12, 2023
1 parent 1c86c21 commit 07bad5b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
13 changes: 12 additions & 1 deletion src/Sdk/Builder/CerbosClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CerbosClientBuilder
private ?string $caCertificate;
private ?string $tlsCertificate;
private ?string $tlsKey;
private ?array $metadata;

/**
* @param string $hostname
Expand All @@ -31,6 +32,7 @@ private function __construct(string $hostname) {
$this->caCertificate = null;
$this->tlsCertificate = null;
$this->tlsKey = null;
$this->metadata = null;
}

/**
Expand All @@ -41,6 +43,15 @@ public static function newInstance(string $hostname): CerbosClientBuilder {
return new CerbosClientBuilder($hostname);
}

/**
* @param array<string, array> $headers
* @return CerbosClientBuilder
*/
public function withMetadata(array $headers): CerbosClientBuilder {
$this->metadata = $headers;
return $this;
}

/**
* @param bool $plaintext
* @return CerbosClientBuilder
Expand Down Expand Up @@ -126,6 +137,6 @@ public function build(): CerbosClient {
]
);

return new CerbosClient($csc, $this->playgroundInstanceId);
return new CerbosClient($csc, $this->playgroundInstanceId, $this->metadata);
}
}
35 changes: 28 additions & 7 deletions src/Sdk/CerbosClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,43 @@
class CerbosClient
{
private CerbosServiceClient $csc;
private ?array $metadata;

private string $playgroundInstanceId;
private string $playgroundInstanceHeader = "playground-instance";

/**
* @param CerbosServiceClient $csc
* @param string $playgroundInstanceId
* @param array<string, array> $headers
*/
public function __construct(CerbosServiceClient $csc, string $playgroundInstanceId) {
public function __construct(CerbosServiceClient $csc, string $playgroundInstanceId, $headers = null) {
$this->csc = $csc;
$this->metadata = $headers;
$this->playgroundInstanceId = $playgroundInstanceId;
}

/**
* @param CheckResourcesRequest $request
* @param array<string, array> $headers
* @return CheckResourcesResponse
* @throws Exception
*/
public function checkResources(CheckResourcesRequest $request): CheckResourcesResponse {
list($checkResourcesResponse, $status) = $this->csc->CheckResources($request->toCheckResourcesRequest(), $this->getMetadata())->wait();
public function checkResources(CheckResourcesRequest $request, $headers = null): CheckResourcesResponse {
list($checkResourcesResponse, $status) = $this->csc->CheckResources($request->toCheckResourcesRequest(), $this->mergeMetadata($this->getMetadata(), $headers))->wait();
$this->handleError($status);

return new CheckResourcesResponse($checkResourcesResponse);
}

/**
* @param PlanResourcesRequest $request
* @param array<string, array> $headers
* @return PlanResourcesResponse
* @throws Exception
*/
public function planResources(PlanResourcesRequest $request): PlanResourcesResponse {
list($planResourcesResponse, $status) = $this->csc->PlanResources($request->toPlanResourcesRequest(), $this->getMetadata())->wait();
public function planResources(PlanResourcesRequest $request, $headers = null): PlanResourcesResponse {
list($planResourcesResponse, $status) = $this->csc->PlanResources($request->toPlanResourcesRequest(), $this->mergeMetadata($this->getMetadata(), $headers))->wait();
$this->handleError($status);

return new PlanResourcesResponse($planResourcesResponse);
Expand All @@ -58,11 +63,27 @@ public function planResources(PlanResourcesRequest $request): PlanResourcesRespo
* @return array<string, array>
*/
private function getMetadata(): array {
return array(
$this->playgroundInstanceHeader => [$this->playgroundInstanceId]
return $this->mergeMetadata(
array($this->playgroundInstanceHeader => [$this->playgroundInstanceId]),
$this->metadata
);
}

/**
* @param array<string, array> $first
* @param array<string, array> $second
* @return array<string, array>
*/
private function mergeMetadata(array $first, ?array $second): array {
if (!is_null($second)) {
foreach ($second as $k => $v) {
$first[$k] = $v;
}
}

return $first;
}

/**
* @param object $status
* @throws Exception
Expand Down

0 comments on commit 07bad5b

Please sign in to comment.