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/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 @@ +path; + } + + 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($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); } } 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']; + } +}