Skip to content
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
93 changes: 93 additions & 0 deletions src/CrowdinApiClient/Api/AiGatewayApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace CrowdinApiClient\Api;

use CrowdinApiClient\Http\ResponseDecorator\ResponseModelDecorator;
use CrowdinApiClient\Model\AiGatewayResponse;

class AiGatewayApi extends AbstractApi
{
/**
* AI Gateway GET
* @link https://developer.crowdin.com/api/v2/#operation/api.ai.providers.gateway.crowdin.get API Documentation
*
* @param int $userId
* @param int $aiProviderId
* @param string $path Raw provider API path after `/gateway/` (e.g. `chat/completions`)
* @return AiGatewayResponse|null
*/
public function gatewayGet(int $userId, int $aiProviderId, string $path): ?AiGatewayResponse
{
$url = sprintf('users/%d/ai/providers/%d/gateway/%s', $userId, $aiProviderId, $path);
return $this->_get($url, AiGatewayResponse::class);
}

/**
* AI Gateway POST
* @link https://developer.crowdin.com/api/v2/#operation/api.ai.providers.gateway.crowdin.post API Documentation
*
* @param int $userId
* @param int $aiProviderId
* @param string $path Raw provider API path after `/gateway/` (e.g. `chat/completions`)
* @param array $data Request body forwarded to the AI provider
* @return AiGatewayResponse|null
*/
public function gatewayPost(int $userId, int $aiProviderId, string $path, array $data): ?AiGatewayResponse
{
$url = sprintf('users/%d/ai/providers/%d/gateway/%s', $userId, $aiProviderId, $path);
return $this->_post($url, AiGatewayResponse::class, $data);
}

/**
* AI Gateway PUT
* @link https://developer.crowdin.com/api/v2/#operation/api.ai.providers.gateway.crowdin.put API Documentation
*
* @param int $userId
* @param int $aiProviderId
* @param string $path Raw provider API path after `/gateway/` (e.g. `chat/completions`)
* @param array $data Request body forwarded to the AI provider
* @return AiGatewayResponse|null
*/
public function gatewayPut(int $userId, int $aiProviderId, string $path, array $data): ?AiGatewayResponse
{
$url = sprintf('users/%d/ai/providers/%d/gateway/%s', $userId, $aiProviderId, $path);
return $this->_put($url, AiGatewayResponse::class, $data);
}

/**
* AI Gateway PATCH
* @link https://developer.crowdin.com/api/v2/#operation/api.ai.providers.gateway.crowdin.patch API Documentation
*
* @param int $userId
* @param int $aiProviderId
* @param string $path Raw provider API path after `/gateway/` (e.g. `chat/completions`)
* @param array $data Request body forwarded to the AI provider
* @return AiGatewayResponse|null
*/
public function gatewayPatch(int $userId, int $aiProviderId, string $path, array $data): ?AiGatewayResponse
{
$url = sprintf('users/%d/ai/providers/%d/gateway/%s', $userId, $aiProviderId, $path);
return $this->_patch($url, AiGatewayResponse::class, $data);
}

/**
* AI Gateway DELETE
* @link https://developer.crowdin.com/api/v2/#operation/api.ai.providers.gateway.crowdin.delete API Documentation
*
* @param int $userId
* @param int $aiProviderId
* @param string $path Raw provider API path after `/gateway/` (e.g. `chat/completions`)
* @return AiGatewayResponse|null
*/
public function gatewayDelete(int $userId, int $aiProviderId, string $path): ?AiGatewayResponse
{
$url = sprintf('users/%d/ai/providers/%d/gateway/%s', $userId, $aiProviderId, $path);
return $this->client->apiRequest(
'delete',
$url,
new ResponseModelDecorator(AiGatewayResponse::class)
);
}
}
89 changes: 89 additions & 0 deletions src/CrowdinApiClient/Api/Enterprise/AiGatewayApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace CrowdinApiClient\Api\Enterprise;

use CrowdinApiClient\Api\AbstractApi;
use CrowdinApiClient\Http\ResponseDecorator\ResponseModelDecorator;
use CrowdinApiClient\Model\AiGatewayResponse;

class AiGatewayApi extends AbstractApi
{
/**
* AI Gateway GET
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.ai.providers.gateway.enterprise.get API Documentation
*
* @param int $aiProviderId
* @param string $path Raw provider API path after `/gateway/` (e.g. `chat/completions`)
* @return AiGatewayResponse|null
*/
public function gatewayGet(int $aiProviderId, string $path): ?AiGatewayResponse
{
$url = sprintf('ai/providers/%d/gateway/%s', $aiProviderId, $path);
return $this->_get($url, AiGatewayResponse::class);
}

/**
* AI Gateway POST
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.ai.providers.gateway.enterprise.post API Documentation
*
* @param int $aiProviderId
* @param string $path Raw provider API path after `/gateway/` (e.g. `chat/completions`)
* @param array $data Request body forwarded to the AI provider
* @return AiGatewayResponse|null
*/
public function gatewayPost(int $aiProviderId, string $path, array $data): ?AiGatewayResponse
{
$url = sprintf('ai/providers/%d/gateway/%s', $aiProviderId, $path);
return $this->_post($url, AiGatewayResponse::class, $data);
}

/**
* AI Gateway PUT
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.ai.providers.gateway.enterprise.put API Documentation
*
* @param int $aiProviderId
* @param string $path Raw provider API path after `/gateway/` (e.g. `chat/completions`)
* @param array $data Request body forwarded to the AI provider
* @return AiGatewayResponse|null
*/
public function gatewayPut(int $aiProviderId, string $path, array $data): ?AiGatewayResponse
{
$url = sprintf('ai/providers/%d/gateway/%s', $aiProviderId, $path);
return $this->_put($url, AiGatewayResponse::class, $data);
}

/**
* AI Gateway PATCH
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.ai.providers.gateway.enterprise.patch API Documentation
*
* @param int $aiProviderId
* @param string $path Raw provider API path after `/gateway/` (e.g. `chat/completions`)
* @param array $data Request body forwarded to the AI provider
* @return AiGatewayResponse|null
*/
public function gatewayPatch(int $aiProviderId, string $path, array $data): ?AiGatewayResponse
{
$url = sprintf('ai/providers/%d/gateway/%s', $aiProviderId, $path);
return $this->_patch($url, AiGatewayResponse::class, $data);
}

/**
* AI Gateway DELETE
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.ai.providers.gateway.enterprise.delete API Documentation
*
* @param int $aiProviderId
* @param string $path Raw provider API path after `/gateway/` (e.g. `chat/completions`)
* @return AiGatewayResponse|null
*/
public function gatewayDelete(int $aiProviderId, string $path): ?AiGatewayResponse
{
$url = sprintf('ai/providers/%d/gateway/%s', $aiProviderId, $path);
return $this->client->apiRequest(
'delete',
$url,
new ResponseModelDecorator(AiGatewayResponse::class)
);
}
}
3 changes: 3 additions & 0 deletions src/CrowdinApiClient/Crowdin.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
* @property \CrowdinApiClient\Api\AiApi|\CrowdinApiClient\Api\Enterprise\AiApi $ai
* @property \CrowdinApiClient\Api\Enterprise\FieldApi $field
* @property \CrowdinApiClient\Api\StyleGuideApi $styleGuide
* @property \CrowdinApiClient\Api\AiGatewayApi|\CrowdinApiClient\Api\Enterprise\AiGatewayApi $aiGateway
*/
class Crowdin
{
Expand Down Expand Up @@ -126,6 +127,7 @@ class Crowdin
'securityLog',
'ai',
'styleGuide',
'aiGateway',
];

protected $servicesEnterprise = [
Expand Down Expand Up @@ -170,6 +172,7 @@ class Crowdin
'ai',
'field',
'styleGuide',
'aiGateway',
];

public function __construct(array $config)
Expand Down
10 changes: 10 additions & 0 deletions src/CrowdinApiClient/Model/AiGatewayResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace CrowdinApiClient\Model;

/**
* @package Crowdin\Model
*/
class AiGatewayResponse extends BaseModel
{
}
137 changes: 137 additions & 0 deletions tests/CrowdinApiClient/Api/AiGatewayApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace CrowdinApiClient\Tests\Api;

use CrowdinApiClient\Model\AiGatewayResponse;

class AiGatewayApiTest extends AbstractTestApi
{
public function testGatewayGet(): void
{
$this->mockRequest([
'path' => '/users/1/ai/providers/2/gateway/chat/completions',
'method' => 'get',
'response' => '{
"data": {
"id": "chatcmpl-abc123",
"object": "chat.completion",
"choices": [{"message": {"role": "assistant", "content": "Hello!"}}]
}
}',
]);

$response = $this->crowdin->aiGateway->gatewayGet(1, 2, 'chat/completions');
$this->assertInstanceOf(AiGatewayResponse::class, $response);
$this->assertEquals('chatcmpl-abc123', $response->getData()['id']);
$this->assertEquals('chat.completion', $response->getData()['object']);
}

public function testGatewayPost(): void
{
$data = [
'model' => 'gpt-4o',
'messages' => [
[
'role' => 'user',
'content' => 'Hi',
],
],
];

$this->mockRequest([
'path' => '/users/1/ai/providers/2/gateway/chat/completions',
'method' => 'post',
'body' => json_encode($data),
'response' => '{
"data": {
"id": "chatcmpl-abc123",
"object": "chat.completion",
"choices": [{"message": {"role": "assistant", "content": "Hello!"}}]
}
}',
]);

$response = $this->crowdin->aiGateway->gatewayPost(1, 2, 'chat/completions', $data);
$this->assertInstanceOf(AiGatewayResponse::class, $response);
$this->assertEquals('chatcmpl-abc123', $response->getData()['id']);
}

public function testGatewayPut(): void
{
$data = [
'model' => 'gpt-4o',
'messages' => [
[
'role' => 'user',
'content' => 'Hi',
],
],
];

$this->mockRequest([
'path' => '/users/1/ai/providers/2/gateway/chat/completions',
'method' => 'put',
'body' => json_encode($data),
'response' => '{
"data": {
"id": "chatcmpl-abc123",
"object": "chat.completion",
"choices": [{"message": {"role": "assistant", "content": "Hello!"}}]
}
}',
]);

$response = $this->crowdin->aiGateway->gatewayPut(1, 2, 'chat/completions', $data);
$this->assertInstanceOf(AiGatewayResponse::class, $response);
$this->assertEquals('chatcmpl-abc123', $response->getData()['id']);
}

public function testGatewayPatch(): void
{
$data = [
'model' => 'gpt-4o',
'messages' => [
[
'role' => 'user',
'content' => 'Hi',
],
],
];

$this->mockRequest([
'path' => '/users/1/ai/providers/2/gateway/chat/completions',
'method' => 'patch',
'body' => json_encode($data),
'response' => '{
"data": {
"id": "chatcmpl-abc123",
"object": "chat.completion",
"choices": [{"message": {"role": "assistant", "content": "Hello!"}}]
}
}',
]);

$response = $this->crowdin->aiGateway->gatewayPatch(1, 2, 'chat/completions', $data);
$this->assertInstanceOf(AiGatewayResponse::class, $response);
$this->assertEquals('chatcmpl-abc123', $response->getData()['id']);
}

public function testGatewayDelete(): void
{
$this->mockRequest([
'path' => '/users/1/ai/providers/2/gateway/chat/completions',
'method' => 'delete',
'response' => '{
"data": {
"id": "chatcmpl-abc123",
"object": "chat.completion",
"choices": [{"message": {"role": "assistant", "content": "Hello!"}}]
}
}',
]);

$response = $this->crowdin->aiGateway->gatewayDelete(1, 2, 'chat/completions');
$this->assertInstanceOf(AiGatewayResponse::class, $response);
$this->assertEquals('chatcmpl-abc123', $response->getData()['id']);
}
}
Loading
Loading