-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyticsClient.php
More file actions
100 lines (94 loc) · 3.07 KB
/
Copy pathAnalyticsClient.php
File metadata and controls
100 lines (94 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace Vapi\Analytics;
use Psr\Http\Client\ClientInterface;
use Vapi\Core\Client\RawClient;
use Vapi\Analytics\Requests\AnalyticsQueryDto;
use Vapi\Types\AnalyticsQueryResult;
use Vapi\Exceptions\VapiException;
use Vapi\Exceptions\VapiApiException;
use Vapi\Core\Json\JsonApiRequest;
use Vapi\Environments;
use Vapi\Core\Client\HttpMethod;
use Vapi\Core\Json\JsonDecoder;
use JsonException;
use Psr\Http\Client\ClientExceptionInterface;
class AnalyticsClient
{
/**
* @var array{
* baseUrl?: string,
* client?: ClientInterface,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* } $options @phpstan-ignore-next-line Property is used in endpoint methods via HttpEndpointGenerator
*/
private array $options;
/**
* @var RawClient $client
*/
private RawClient $client;
/**
* @param RawClient $client
* @param ?array{
* baseUrl?: string,
* client?: ClientInterface,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* } $options
*/
public function __construct(
RawClient $client,
?array $options = null,
) {
$this->client = $client;
$this->options = $options ?? [];
}
/**
* @param AnalyticsQueryDto $request
* @param ?array{
* baseUrl?: string,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* queryParameters?: array<string, mixed>,
* bodyProperties?: array<string, mixed>,
* } $options
* @return ?array<AnalyticsQueryResult>
* @throws VapiException
* @throws VapiApiException
*/
public function get(AnalyticsQueryDto $request, ?array $options = null): ?array
{
$options = array_merge($this->options, $options ?? []);
try {
$response = $this->client->sendRequest(
new JsonApiRequest(
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Default_->value,
path: "analytics",
method: HttpMethod::POST,
body: $request,
),
$options,
);
$statusCode = $response->getStatusCode();
if ($statusCode >= 200 && $statusCode < 400) {
$json = $response->getBody()->getContents();
if (empty($json)) {
return null;
}
return JsonDecoder::decodeArray($json, [AnalyticsQueryResult::class]); // @phpstan-ignore-line
}
} catch (JsonException $e) {
throw new VapiException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
} catch (ClientExceptionInterface $e) {
throw new VapiException(message: $e->getMessage(), previous: $e);
}
throw new VapiApiException(
message: 'API request failed',
statusCode: $statusCode,
body: $response->getBody()->getContents(),
);
}
}