Skip to content

Commit 01f0fcb

Browse files
Release 0.0.1
1 parent ef4bb2f commit 01f0fcb

File tree

1,374 files changed

+223557
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,374 files changed

+223557
-1
lines changed

.fernignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Specify files that shouldn't be modified by Fern

.github/workflows/ci.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: ci
2+
3+
on: [push]
4+
5+
jobs:
6+
compile:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- name: Checkout repo
11+
uses: actions/checkout@v4
12+
13+
- name: Setup PHP
14+
uses: shivammathur/setup-php@v2
15+
with:
16+
php-version: "8.1"
17+
18+
- name: Install tools
19+
run: |
20+
composer install
21+
22+
- name: Build
23+
run: |
24+
composer build
25+
26+
- name: Analyze
27+
run: |
28+
composer analyze
29+
30+
unit-tests:
31+
runs-on: ubuntu-latest
32+
33+
steps:
34+
- name: Checkout repo
35+
uses: actions/checkout@v4
36+
37+
- name: Setup PHP
38+
uses: shivammathur/setup-php@v2
39+
with:
40+
php-version: "8.1"
41+
42+
- name: Install tools
43+
run: |
44+
composer install
45+
46+
- name: Run Tests
47+
run: |
48+
composer test

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.idea
2+
.php-cs-fixer.cache
3+
.phpunit.result.cache
4+
composer.lock
5+
vendor/

README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "vapi/vapi",
3+
"version": "0.0.1",
4+
"description": "Vapi PHP Library",
5+
"keywords": [
6+
"vapi",
7+
"api",
8+
"sdk"
9+
],
10+
"license": [],
11+
"require": {
12+
"php": "^8.1",
13+
"ext-json": "*",
14+
"guzzlehttp/guzzle": "^7.4"
15+
},
16+
"require-dev": {
17+
"phpunit/phpunit": "^9.0",
18+
"friendsofphp/php-cs-fixer": "3.5.0",
19+
"phpstan/phpstan": "^1.12"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Vapi\\": "src/"
24+
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"Vapi\\Tests\\": "tests/"
29+
}
30+
},
31+
"scripts": {
32+
"build": [
33+
"@php -l src",
34+
"@php -l tests"
35+
],
36+
"test": "phpunit",
37+
"analyze": "phpstan analyze src tests --memory-limit=1G"
38+
}
39+
}

phpstan.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
level: max
3+
reportUnmatchedIgnoredErrors: false
4+
paths:
5+
- src
6+
- tests

phpunit.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<phpunit bootstrap="vendor/autoload.php">
2+
<testsuites>
3+
<testsuite name="Test Suite">
4+
<directory suffix="Test.php">tests</directory>
5+
</testsuite>
6+
</testsuites>
7+
</phpunit>

src/Analytics/AnalyticsClient.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Vapi\Analytics\Requests;
4+
5+
use Vapi\Core\Json\JsonSerializableType;
6+
use Vapi\Types\AnalyticsQuery;
7+
use Vapi\Core\Json\JsonProperty;
8+
use Vapi\Core\Types\ArrayType;
9+
10+
class AnalyticsQueryDto extends JsonSerializableType
11+
{
12+
/**
13+
* @var array<AnalyticsQuery> $queries This is the list of metric queries you want to perform.
14+
*/
15+
#[JsonProperty('queries'), ArrayType([AnalyticsQuery::class])]
16+
public array $queries;
17+
18+
/**
19+
* @param array{
20+
* queries: array<AnalyticsQuery>,
21+
* } $values
22+
*/
23+
public function __construct(
24+
array $values,
25+
) {
26+
$this->queries = $values['queries'];
27+
}
28+
}

0 commit comments

Comments
 (0)