From d4799cc061fbf4a3bb97a00a751c72bade8f2968 Mon Sep 17 00:00:00 2001 From: Echron Date: Sun, 13 Mar 2022 07:47:10 +0000 Subject: [PATCH 1/3] Connections: update API request + move to separate endpoint --- src/Client.php | 32 ++++++---------------- src/Endpoint/ConnectionEndpoint.php | 41 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 24 deletions(-) create mode 100644 src/Endpoint/ConnectionEndpoint.php diff --git a/src/Client.php b/src/Client.php index 97d8ec4..d535514 100644 --- a/src/Client.php +++ b/src/Client.php @@ -3,12 +3,12 @@ namespace Attlaz; +use Attlaz\Endpoint\ConnectionEndpoint; use Attlaz\Endpoint\LogEndpoint; use Attlaz\Endpoint\StorageEndpoint; use Attlaz\Helper\TokenStorage; use Attlaz\Model\Config; use Attlaz\Model\Exception\RequestException; -use Attlaz\Model\LogEntry; use Attlaz\Model\Project; use Attlaz\Model\ProjectEnvironment; use Attlaz\Model\Task; @@ -33,6 +33,7 @@ class Client private StorageEndpoint $storageEndpoint; private LogEndpoint $logEndpoint; + private ConnectionEndpoint $connectionEndpoint; public function __construct(string $clientId, string $clientSecret, bool $storeToken = false) { @@ -463,29 +464,6 @@ public function getProjectEnvironments(string $projectId): array return $projectEnvironments; } - /** - * @param string $projectId - * @return array[] - * @throws RequestException - */ - public function getConnections(string $projectId): array - { - $uri = '/connections?projectId=' . $projectId; - - $request = $this->createRequest('GET', $uri); - -// $connections = []; - - $response = $this->sendRequest($request); - $rawConnections = $response['data']; -// foreach ($rawEnvironments as $rawEnvironment) { -// $projectEnvironments[] = $this->parseProjectEnvironment($rawEnvironment); -// } -// -// return $projectEnvironments; - - return $rawConnections; - } public function enableDebug() { @@ -501,8 +479,14 @@ public function getStorageEndpoint(): StorageEndpoint { return $this->storageEndpoint; } + public function getLogEndpoint(): LogEndpoint { return $this->logEndpoint; } + + public function getConnectionEndpoint(): ConnectionEndpoint + { + return $this->connectionEndpoint; + } } diff --git a/src/Endpoint/ConnectionEndpoint.php b/src/Endpoint/ConnectionEndpoint.php new file mode 100644 index 0000000..617e000 --- /dev/null +++ b/src/Endpoint/ConnectionEndpoint.php @@ -0,0 +1,41 @@ +client = $client; + } + + /** + * @param string $projectId + * @return array[] + * @throws RequestException + */ + public function getConnections(string $projectId): array + { + $uri = '/project/' . $projectId . '/connections'; + + $request = $this->client->createRequest('GET', $uri); + +// $connections = []; + + $response = $this->client->sendRequest($request); + $rawConnections = $response['data']; +// foreach ($rawEnvironments as $rawEnvironment) { +// $projectEnvironments[] = $this->parseProjectEnvironment($rawEnvironment); +// } +// +// return $projectEnvironments; + + return $rawConnections; + } +} From 84f697de7de0c10f3067a58cad61b987f6a4a689 Mon Sep 17 00:00:00 2001 From: Echron Date: Sun, 27 Mar 2022 19:42:34 +0100 Subject: [PATCH 2/3] Log: add method to fetch log streams + refactoring --- src/Endpoint/LogEndpoint.php | 38 +++++++++++++++++++++++++++-- src/Model/{ => Log}/LogEntry.php | 2 +- src/Model/Log/LogStream.php | 27 ++++++++++++++++++++ src/Model/{ => Log}/LogStreamId.php | 2 +- 4 files changed, 65 insertions(+), 4 deletions(-) rename src/Model/{ => Log}/LogEntry.php (97%) create mode 100644 src/Model/Log/LogStream.php rename src/Model/{ => Log}/LogStreamId.php (89%) diff --git a/src/Endpoint/LogEndpoint.php b/src/Endpoint/LogEndpoint.php index 451870f..49b081a 100644 --- a/src/Endpoint/LogEndpoint.php +++ b/src/Endpoint/LogEndpoint.php @@ -4,7 +4,9 @@ namespace Attlaz\Endpoint; use Attlaz\Client; -use Attlaz\Model\LogEntry; +use Attlaz\Model\Log\LogEntry; +use Attlaz\Model\Log\LogStream; +use Attlaz\Model\Log\LogStreamId; class LogEndpoint { @@ -19,7 +21,7 @@ public function saveLog(LogEntry $logEntry): LogEntry { $body = $logEntry; - $uri = '/logstreams/' . \base64_encode($logEntry->getLogStreamId()->getId()) . '/logs'; + $uri = '/logstreams/' . \base64_encode($logEntry->getLogStreamId()->__toString()) . '/logs'; $request = $this->client->createRequest('POST', $uri, $body); @@ -34,4 +36,36 @@ public function saveLog(LogEntry $logEntry): LogEntry } throw new \Exception('Unable to save log entry: invalid response'); } + + /** + * @param string $projectId + * @return LogStream[] + * @throws \Attlaz\Model\Exception\RequestException + */ + public function getLogStreams(string $projectId): array + { + + $uri = '/project/' . $projectId . '/logstreams'; + + $request = $this->client->createRequest('GET', $uri); + + $response = $this->client->sendRequest($request); + + + if (isset($response['data'])) { + + $result = []; + foreach ($response['data'] as $logStream) { + + $id = $logStream['id']; + if (\is_array($id)) { + $id = $id['id']; + } + $result[] = new LogStream(new LogStreamId($id), $logStream['name']); + } + + return $result; + } + throw new \Exception('Unable to get log streams: invalid response'); + } } diff --git a/src/Model/LogEntry.php b/src/Model/Log/LogEntry.php similarity index 97% rename from src/Model/LogEntry.php rename to src/Model/Log/LogEntry.php index bd8d178..75d3854 100644 --- a/src/Model/LogEntry.php +++ b/src/Model/Log/LogEntry.php @@ -1,7 +1,7 @@ id = $id; + $this->name = $name; + } + + public function getId(): LogStreamId + { + return $this->id; + } + + public function getName(): string + { + return $this->name; + } + +} diff --git a/src/Model/LogStreamId.php b/src/Model/Log/LogStreamId.php similarity index 89% rename from src/Model/LogStreamId.php rename to src/Model/Log/LogStreamId.php index 3bb1177..8f1f097 100644 --- a/src/Model/LogStreamId.php +++ b/src/Model/Log/LogStreamId.php @@ -1,7 +1,7 @@ Date: Sun, 27 Mar 2022 20:15:10 +0100 Subject: [PATCH 3/3] Refactor "getId" to "__toString()" for logStreamId --- src/Model/Log/LogEntry.php | 2 +- src/Model/Log/LogStreamId.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Model/Log/LogEntry.php b/src/Model/Log/LogEntry.php index 75d3854..c7cea20 100644 --- a/src/Model/Log/LogEntry.php +++ b/src/Model/Log/LogEntry.php @@ -34,7 +34,7 @@ public function getDate(): \DateTimeInterface public function jsonSerialize(): array { return [ - 'logStream' => ['id' => $this->logStreamId->getId()], + 'logStream' => ['id' => $this->logStreamId->__toString()], 'date' => $this->date->format(\DateTime::RFC3339_EXTENDED), 'level' => $this->level, 'message' => $this->message, diff --git a/src/Model/Log/LogStreamId.php b/src/Model/Log/LogStreamId.php index 8f1f097..6ca51ad 100644 --- a/src/Model/Log/LogStreamId.php +++ b/src/Model/Log/LogStreamId.php @@ -13,7 +13,7 @@ public function __construct(string $id) $this->id = $id; } - public function getId(): string + public function __toString(): string { return $this->id; }