Skip to content

Commit

Permalink
Update configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
nasrulhazim committed Nov 5, 2024
1 parent ec14d36 commit a476813
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
55 changes: 54 additions & 1 deletion src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,43 @@

namespace CleaniqueCoders\KongAdminApi;

/**
* Class Configuration
*
* This class holds configuration settings for connecting to the Kong Admin API.
*/
class Configuration
{
/**
* Base URL for Kong Admin.
*/
protected string $base;

/**
* URI for Kong Admin API.
*/
protected string $uri;

/**
* API key for authentication.
*/
protected string $apiKey;

/**
* API key name for authentication.
*/
protected string $keyName;

/**
* Headers for API requests.
*
* @var array<string, string>
*/
protected array $headers;

/**
* Whether to verify SSL certificates.
*/
protected bool $verify;

/**
Expand All @@ -23,7 +48,7 @@ class Configuration
* @param string $uri URI for Kong Admin API
* @param string $apiKey API key for authentication
* @param string $keyName API key name for authentication
* @param array $headers Headers for requests
* @param array<string, string> $headers Headers for requests
* @param bool $verify Whether to verify SSL certificates
*/
public function __construct(
Expand All @@ -45,33 +70,61 @@ public function __construct(
$this->verify = $verify;
}

/**
* Get the base URL for Kong Admin.
*/
public function getBase(): string
{
return $this->base;
}

/**
* Get the URI for Kong Admin API.
*/
public function getUri(): string
{
return $this->uri;
}

/**
* Get the API key for authentication.
*/
public function getApiKey(): string
{
return $this->apiKey;
}

/**
* Get the API key name used for authentication.
*/
public function getKeyName(): string
{
return $this->keyName;
}

/**
* Get the headers for API requests.
*
* @return array<string, string>
*/
public function getHeaders(): array
{
return $this->headers;
}

/**
* Determine if SSL certificates should be verified.
*/
public function shouldVerify(): bool
{
return $this->verify;
}

/**
* Get the full URL for the Kong Admin API, combining base and URI.
*/
public function getUrl(): string
{
return rtrim($this->getBase(), '/').'/'.trim($this->getUri(), '/');
}
}
9 changes: 8 additions & 1 deletion tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
expect($config->getKeyName())->toBe($keyName);
expect($config->getHeaders())->toBe($headers);
expect($config->shouldVerify())->toBeTrue();
expect($config->getUrl())->toBe('http://localhost/admin');
});

it('uses default keyName, headers, and verify if not provided', function () {
it('uses default values for keyName, headers, and verify if not provided', function () {
$base = 'http://localhost';
$uri = '/admin';
$apiKey = 'test-api-key';
Expand All @@ -39,6 +40,12 @@
'Accept' => 'application/json',
]); // Default headers
expect($config->shouldVerify())->toBeFalse(); // Default verify
expect($config->getUrl())->toBe('http://localhost/admin'); // URL combination
});

it('correctly combines base and uri in getUrl', function () {
$config = new Configuration('http://localhost/', '/admin', 'test-api-key');
expect($config->getUrl())->toBe('http://localhost/admin');
});

it('throws an error when required parameters are missing', function () {
Expand Down

0 comments on commit a476813

Please sign in to comment.