Skip to content

Commit

Permalink
Merge branch 'release/1.8.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
Echron committed Jan 9, 2023
2 parents 93625f9 + c408ef7 commit da021a0
Show file tree
Hide file tree
Showing 12 changed files with 348 additions and 413 deletions.
397 changes: 58 additions & 339 deletions composer.lock

Large diffs are not rendered by default.

95 changes: 81 additions & 14 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class Client
private LogEndpoint $logEndpoint;
private ConnectionEndpoint $connectionEndpoint;

private bool $profileRequests = false;

private array $profiles = [];

public function __construct(string $clientId, string $clientSecret, bool $storeToken = false)
{
if (empty($clientId)) {
Expand All @@ -61,7 +65,7 @@ public function setEndPoint(string $endPoint): void
$this->endPoint = rtrim($endPoint, "/");
}

private function authenticate()
private function authenticate(): void
{


Expand Down Expand Up @@ -110,12 +114,12 @@ public function getAccessToken(): ?AccessToken
return $this->accessToken;
}

public function setAccessToken(AccessToken $accessToken)
public function setAccessToken(AccessToken $accessToken): void
{
$this->accessToken = $accessToken;
}

public function setTimeout(int $timeout)
public function setTimeout(int $timeout): void
{
$this->timeout = $timeout;
}
Expand All @@ -139,17 +143,34 @@ public function createRequest(string $method, string $uri, $body = null): Reques
return $this->provider->getAuthenticatedRequest($method, $url, $this->accessToken, $options);
}


public function sendRequest(RequestInterface $request): array
{
$response = null;
try {

$startTime = \microtime(true);

$response = $this->provider->getHttpClient()
->send($request, ['debug' => $this->debug]);


$jsonResponse = \json_decode($response->getBody()
->getContents(), true);

} catch (\Throwable $ex) {
throw new RequestException($ex->getMessage());
} finally {
if ($this->profileRequests) {
$seconds = \microtime(true) - $startTime;

$this->profiles[] = [
'Uri' => $request->getUri(),
'Method' => $request->getMethod(),
'Response code' => $response === null ? '' : $response->getStatusCode(),
'Duration' => $seconds
];
}
}

return $jsonResponse;
Expand Down Expand Up @@ -247,7 +268,9 @@ public function getTasks(string $projectId): array
$request = $this->createRequest('GET', $uri);

$rawTasks = $this->sendRequest($request);

if (isset($rawTasks['data'])) {
$rawTasks = $rawTasks['data'];
}
$tasks = [];
if (!\is_null($rawTasks)) {
foreach ($rawTasks as $rawTask) {
Expand All @@ -256,9 +279,15 @@ public function getTasks(string $projectId): array
$task->key = $rawTask['key'];
$task->name = $rawTask['name'];
$task->description = $rawTask['description'];
$task->project = $rawTask['project'];
$task->project = $this->getProjectIdFromRawValue($rawTask);

if (isset($rawTask['isDirect'])) {
$task->direct = $rawTask['isDirect'];
} else {
$task->direct = $rawTask['direct'];
}
$task->state = $rawTask['state'];
$task->direct = $rawTask['direct'];


$tasks[] = $task;
}
Expand Down Expand Up @@ -290,9 +319,7 @@ public function getTaskExecution(string $taskExecutionId): ?array
//TODO: handle when no execution is found
$request = $this->createRequest('GET', $uri);

$response = $this->sendRequest($request);

return $response;
return $this->sendRequest($request);
}

public function updateTaskExecution(string $taskExecutionId, string $status, int $time = null): void
Expand All @@ -302,7 +329,7 @@ public function updateTaskExecution(string $taskExecutionId, string $status, int
'time' => $time,
];

$uri = '/taskexecutions/' . $taskExecutionId . '';
$uri = '/taskexecutions/' . $taskExecutionId;

$request = $this->createRequest('POST', $uri, $body);

Expand Down Expand Up @@ -335,8 +362,10 @@ public function getConfigByProject(string $projectId, string $projectEnvironment
$configValue->inheritable = $rawConfigValue['inheritable'];
$configValue->sensitive = $rawConfigValue['sensitive'];
$configValue->state = $rawConfigValue['state'];
$configValue->project = $rawConfigValue['project'];
$configValue->projectEnvironment = (string)$rawConfigValue['projectEnvironment'];
$configValue->project = $this->getProjectIdFromRawValue($rawConfigValue);
$configValue->projectEnvironment = $this->getProjectEnvironmentIdFromRawValue($rawConfigValue);


$configValue->key = $rawConfigValue['key'];
$configValue->value = $rawConfigValue['value'];

Expand All @@ -347,6 +376,29 @@ public function getConfigByProject(string $projectId, string $projectEnvironment
return $result;
}

private function getProjectIdFromRawValue(array $rawValue): string
{
$tries = ['project', 'project_id', 'projectId'];
foreach ($tries as $try) {
if (isset($rawValue[$try])) {
return $rawValue[$try];
}
}
throw new \Error('Project not found in raw value');
}

private function getProjectEnvironmentIdFromRawValue(array $rawValue): string
{
$tries = ['project_environment', 'project_environment_id', 'projectEnvironment', 'projectEnvironmentId'];
foreach ($tries as $try) {
if (isset($rawValue[$try])) {
return $rawValue[$try];
}
}
throw new \Error('Project environment not found in raw value');
}


public function getProjectById(string $projectId): Project
{
$projects = $this->getProjects();
Expand Down Expand Up @@ -488,16 +540,31 @@ public function getApiVersion(): ?string
return null;
}

public function enableDebug()
public function enableDebug(): void
{
$this->debug = true;
}

public function disableDebug()
public function disableDebug(): void
{
$this->debug = false;
}

public function enableRequestProfiling(): void
{
$this->profileRequests = true;
}

public function disableRequestProfiling(): void
{
$this->profileRequests = false;
}

public function getProfiles(): array
{
return $this->profiles;
}

public function getStorageEndpoint(): StorageEndpoint
{
return $this->storageEndpoint;
Expand Down
4 changes: 2 additions & 2 deletions src/Endpoint/ConnectionEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public function getConnections(string $projectId): array
*/
public function getConnection(string $connectionKey): ?AdapterConnection
{
$uri = '/connections/' . $connectionKey . '';
$uri = '/connections/' . $connectionKey;

$request = $this->client->createRequest('GET', $uri);

$response = $this->client->sendRequest($request);

if (isset($response['errors']) && count($response['errors']) > 0) {
return null;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Endpoint/StorageEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Attlaz\Client;
use Attlaz\Model\StorageItem;
use DateTimeInterface;

class StorageEndpoint
{
Expand Down Expand Up @@ -47,7 +48,7 @@ public function getItem(string $projectEnvironmentId, string $storageType, strin
$item->value = $this->thawValue($rawItem['value']);

if ($rawItem['expiration'] !== null) {
$rawItem['expiration'] = \DateTime::createFromFormat(\DateTime::RFC3339_EXTENDED, $rawItem['expiration']);
$rawItem['expiration'] = \DateTime::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $rawItem['expiration']);
}
return $item;
}
Expand Down Expand Up @@ -91,11 +92,10 @@ public function setItem(string $projectEnvironmentId, string $storageType, Stora
$uri = '/projectenvironments/' . $projectEnvironmentId . '/storage/' . $storageType . '/items/' . $storageItem->key;
}

$data = clone $storageItem;
$data->value = $this->freezeValue($data->value);

$copy = clone $storageItem;
$copy->value = $this->freezeValue($copy->value);

$request = $this->client->createRequest('POST', $uri, $copy);
$request = $this->client->createRequest('POST', $uri, $data);

$rawResult = $this->client->sendRequest($request);

Expand Down
12 changes: 5 additions & 7 deletions src/Helper/TokenStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ public static function loadAccessToken(string $clientId, string $clientSecret):

if (!is_null($accessToken) && is_a($accessToken, AccessToken::class) && !$accessToken->hasExpired()) {
return $accessToken;
} else {

try {
\unlink($file);
} catch (\Throwable $ex) {
}
try {
\unlink($file);
} catch (\Throwable $ex) {

}
}
}

Expand All @@ -45,7 +43,7 @@ private static function getData(string $clientId, string $clientSecret): array
];
}

public static function saveAccessToken(AccessToken $accessToken, string $clientId, string $clientSecret)
public static function saveAccessToken(AccessToken $accessToken, string $clientId, string $clientSecret): void
{
$data = self::getData($clientId, $clientSecret);

Expand Down
4 changes: 3 additions & 1 deletion src/Model/Log/LogEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Attlaz\Model\Log;

use DateTimeInterface;

class LogEntry implements \JsonSerializable
{
public ?string $id;
Expand Down Expand Up @@ -35,7 +37,7 @@ public function jsonSerialize(): array
{
return [
'logStream' => ['id' => $this->logStreamId->__toString()],
'date' => $this->date->format(\DateTime::RFC3339_EXTENDED),
'date' => $this->date->format(DateTimeInterface::RFC3339_EXTENDED),
'level' => $this->level,
'message' => $this->message,
'context' => $this->context,
Expand Down
6 changes: 4 additions & 2 deletions src/Model/StorageItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace Attlaz\Model;

class StorageItem
use DateTimeInterface;

class StorageItem implements \JsonSerializable
{
public string $key;
/** @var string|int|float|array|object|null|bool */
Expand All @@ -15,7 +17,7 @@ public function jsonSerialize(): array
return [
'key' => $this->key,
'value' => $this->value,
'expiration' => $this->expiration->format(\DateTime::RFC3339_EXTENDED),
'expiration' => $this->expiration === null ? null : $this->expiration->format(DateTimeInterface::RFC3339_EXTENDED),
];
}
}
2 changes: 2 additions & 0 deletions tests/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function testWriteItem()
//}
$accessToken = $client->getAccessToken();
$secondsToExpireToken = ($accessToken->getExpires() - time());

$this->assertGreaterThanOrEqual(5, $secondsToExpireToken);
echo 'Token expires in ' . $secondsToExpireToken . ' seconds' . PHP_EOL;
$secondsToSleep = $secondsToExpireToken + 5;
echo 'Sleep ' . $secondsToSleep . ' seconds so the token expires' . PHP_EOL;
Expand Down
Loading

0 comments on commit da021a0

Please sign in to comment.