Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Field definitions endpoint #17

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions examples/example-fetch-definitions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

require __DIR__ . "/../vendor/autoload.php";

use Symfony\Component\Dotenv\Dotenv;
use Silktide\ProspectClient\ProspectClient;

$envFile = __DIR__ . "/../.env";
if (file_exists($envFile)) {
(new Dotenv())->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;
}
6 changes: 6 additions & 0 deletions src/Api/ReportApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
73 changes: 73 additions & 0 deletions src/Entity/DefinitionField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Silktide\ProspectClient\Entity;

class DefinitionField
{
private string $id;
private string $label;
private string $description;
private string $type;
private array $raw = [];

/**
* @var string[]
*/
private array $scenarios = [];

/**
* @param string $id
* @param string $label
* @param string $description
* @param string $type
* @param string[] $scenarios
* @param array $data
* @return static
*/
public static function create(string $id, string $label, string $description, string $type, array $scenarios, array $data): self
{
$definitions = new self();

$definitions->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;
}
}
7 changes: 7 additions & 0 deletions src/Exception/Api/DefinitionsNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Silktide\ProspectClient\Exception\Api;

use Silktide\ProspectClient\Exception\ProspectClientException;

class DefinitionsNotFoundException extends ProspectClientException {}
29 changes: 29 additions & 0 deletions src/Request/FetchDefinitionsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Silktide\ProspectClient\Request;

use Silktide\ProspectClient\Exception\Api\DefinitionsNotFoundException;
use Silktide\ProspectClient\Response\FetchDefinitionsResponse;

class FetchDefinitionsRequest extends AbstractRequest
{
protected string $path = 'fetch-definitions';
protected array $queryParams = [];

public function getPath(): string
{
return $this->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);
}
}
3 changes: 2 additions & 1 deletion src/Request/FetchReportRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
37 changes: 37 additions & 0 deletions src/Response/FetchDefinitionsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Silktide\ProspectClient\Response;

use Silktide\ProspectClient\Entity\DefinitionField;

class FetchDefinitionsResponse extends AbstractResponse
{
/**
* @return DefinitionField[]
*/
public function getDefinitions(): array
{
$definitions = [];
foreach ($this->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'];
}
}