From 8207bde20950a69fd10cdbf9064d18933b501daf Mon Sep 17 00:00:00 2001 From: B3none Date: Thu, 28 Jul 2022 12:30:04 +0100 Subject: [PATCH 1/4] Added fetch-definitions endpoint and relevant entities --- examples/example-fetch-definitions.php | 29 +++++++++ src/Api/ReportApi.php | 6 ++ src/Entity/DefinitionField.php | 73 +++++++++++++++++++++++ src/Request/FetchDefinitionsRequest.php | 28 +++++++++ src/Response/FetchDefinitionsResponse.php | 37 ++++++++++++ 5 files changed, 173 insertions(+) create mode 100644 examples/example-fetch-definitions.php create mode 100644 src/Entity/DefinitionField.php create mode 100644 src/Request/FetchDefinitionsRequest.php create mode 100644 src/Response/FetchDefinitionsResponse.php diff --git a/examples/example-fetch-definitions.php b/examples/example-fetch-definitions.php new file mode 100644 index 0000000..5778505 --- /dev/null +++ b/examples/example-fetch-definitions.php @@ -0,0 +1,29 @@ +load($envFile); +} +$apiKey = $_ENV["PROSPECT_API_KEY"] ?? null; + +if (!is_string($apiKey)) { + throw new \Exception("An API key should be specified in the ./env file to run the examples"); +} + +$prospectClient = ProspectClient::createFromApiKey($apiKey); +$reportApi = $prospectClient->getReportApi(); + +$response = $reportApi->fetchDefinitions()->execute(); + +$definitions = $response->getDefinitions(); + +foreach ($definitions as $definition) { + echo "Definition ID: " . $definition->getId() . PHP_EOL; + var_dump($definition->getRawData()); + echo PHP_EOL; +} diff --git a/src/Api/ReportApi.php b/src/Api/ReportApi.php index b9bcc8d..a131b11 100644 --- a/src/Api/ReportApi.php +++ b/src/Api/ReportApi.php @@ -3,6 +3,7 @@ namespace Silktide\ProspectClient\Api; use Silktide\ProspectClient\Request\CreateReportRequest; +use Silktide\ProspectClient\Request\FetchDefinitionsRequest; use Silktide\ProspectClient\Request\FetchReportRequest; use Silktide\ProspectClient\Request\ReanalyzeReportRequest; use Silktide\ProspectClient\Request\SearchReportRequest; @@ -27,6 +28,11 @@ public function fetch(string $reportId): FetchReportRequest return new FetchReportRequest($this->httpWrapper, $reportId); } + public function fetchDefinitions(): FetchDefinitionsRequest + { + return new FetchDefinitionsRequest($this->httpWrapper); + } + public function reanalyze(string $reportId): ReanalyzeReportRequest { return new ReanalyzeReportRequest($this->httpWrapper, $reportId); diff --git a/src/Entity/DefinitionField.php b/src/Entity/DefinitionField.php new file mode 100644 index 0000000..a358517 --- /dev/null +++ b/src/Entity/DefinitionField.php @@ -0,0 +1,73 @@ +id = $id; + $definitions->label = $label; + $definitions->description = $description; + $definitions->type = $type; + $definitions->scenarios = $scenarios; + $definitions->raw = $data; + + return $definitions; + } + + public function getId(): string + { + return $this->id; + } + + public function getLabel(): string + { + return $this->label; + } + + public function getDescription(): string + { + return $this->description; + } + + public function getType(): string + { + return $this->type; + } + + /** + * @return string[] + */ + public function getScenarios(): array + { + return $this->scenarios; + } + + public function getRawData(): array + { + return $this->raw; + } +} diff --git a/src/Request/FetchDefinitionsRequest.php b/src/Request/FetchDefinitionsRequest.php new file mode 100644 index 0000000..c41a841 --- /dev/null +++ b/src/Request/FetchDefinitionsRequest.php @@ -0,0 +1,28 @@ +path; + } + + public function execute(): FetchDefinitionsResponse + { + $httpResponse = $this->httpWrapper->execute($this); + + if ($httpResponse->getStatusCode() === 404) { + throw new ReportNotFoundException($response['error_message'] ?? 'Endpoint not found'); + } + + return new FetchDefinitionsResponse($httpResponse->getResponse()); + } +} diff --git a/src/Response/FetchDefinitionsResponse.php b/src/Response/FetchDefinitionsResponse.php new file mode 100644 index 0000000..3ae1c8c --- /dev/null +++ b/src/Response/FetchDefinitionsResponse.php @@ -0,0 +1,37 @@ +response['fields'] as $field) { + /** + * @var $field array + */ + + $definitions[$field['id']] = DefinitionField::create( + $field['id'], + $field['label'], + $field['description'], + $field['type'], + $field['scenarios'], + $field + ); + } + + return $definitions; + } + + public function getRequestStatus() : string + { + return $this->response['request_status']; + } +} From c83f7de3d75e247e93fbbced66de4bcc9c9dfd8d Mon Sep 17 00:00:00 2001 From: B3none Date: Thu, 28 Jul 2022 13:51:08 +0100 Subject: [PATCH 2/4] Update exception --- src/Exception/Api/DefinitionsNotFoundException.php | 7 +++++++ src/Request/FetchDefinitionsRequest.php | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 src/Exception/Api/DefinitionsNotFoundException.php diff --git a/src/Exception/Api/DefinitionsNotFoundException.php b/src/Exception/Api/DefinitionsNotFoundException.php new file mode 100644 index 0000000..d90368e --- /dev/null +++ b/src/Exception/Api/DefinitionsNotFoundException.php @@ -0,0 +1,7 @@ +httpWrapper->execute($this); if ($httpResponse->getStatusCode() === 404) { - throw new ReportNotFoundException($response['error_message'] ?? 'Endpoint not found'); + throw new DefinitionsNotFoundException($response['error_message'] ?? 'Endpoint not found'); } return new FetchDefinitionsResponse($httpResponse->getResponse()); From d304967f1b893f4f76fd5616626543153b394a59 Mon Sep 17 00:00:00 2001 From: B3none Date: Thu, 28 Jul 2022 13:55:05 +0100 Subject: [PATCH 3/4] update wording --- src/Request/FetchDefinitionsRequest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Request/FetchDefinitionsRequest.php b/src/Request/FetchDefinitionsRequest.php index c10de6f..4ec6e0d 100644 --- a/src/Request/FetchDefinitionsRequest.php +++ b/src/Request/FetchDefinitionsRequest.php @@ -20,7 +20,7 @@ public function execute(): FetchDefinitionsResponse $httpResponse = $this->httpWrapper->execute($this); if ($httpResponse->getStatusCode() === 404) { - throw new DefinitionsNotFoundException($response['error_message'] ?? 'Endpoint not found'); + throw new DefinitionsNotFoundException($response['error_message'] ?? 'Definitions not found'); } return new FetchDefinitionsResponse($httpResponse->getResponse()); From 91014688525221ec97a6cf9c756c242de26b0f42 Mon Sep 17 00:00:00 2001 From: B3none Date: Thu, 28 Jul 2022 14:01:54 +0100 Subject: [PATCH 4/4] Fix fetch responses --- src/Request/FetchDefinitionsRequest.php | 3 ++- src/Request/FetchReportRequest.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Request/FetchDefinitionsRequest.php b/src/Request/FetchDefinitionsRequest.php index 4ec6e0d..f5a7714 100644 --- a/src/Request/FetchDefinitionsRequest.php +++ b/src/Request/FetchDefinitionsRequest.php @@ -18,11 +18,12 @@ public function getPath(): string public function execute(): FetchDefinitionsResponse { $httpResponse = $this->httpWrapper->execute($this); + $response = $httpResponse->getResponse(); if ($httpResponse->getStatusCode() === 404) { throw new DefinitionsNotFoundException($response['error_message'] ?? 'Definitions not found'); } - return new FetchDefinitionsResponse($httpResponse->getResponse()); + return new FetchDefinitionsResponse($response); } } diff --git a/src/Request/FetchReportRequest.php b/src/Request/FetchReportRequest.php index fce862b..256dda1 100644 --- a/src/Request/FetchReportRequest.php +++ b/src/Request/FetchReportRequest.php @@ -57,11 +57,12 @@ public function includeAvailableVersions(): self public function execute(): FetchReportResponse { $httpResponse = $this->httpWrapper->execute($this); + $response = $httpResponse->getResponse(); if ($httpResponse->getStatusCode() === 404) { throw new ReportNotFoundException($response['error_message'] ?? 'Report not found'); } - return new FetchReportResponse($httpResponse->getResponse()); + return new FetchReportResponse($response); } }