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