Skip to content

Commit ecf6173

Browse files
committed
feat: use symfony/http-client
1 parent 702d05d commit ecf6173

File tree

5 files changed

+49
-20
lines changed

5 files changed

+49
-20
lines changed

composer.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
],
1313
"require": {
1414
"php": "^8.1.0",
15-
"openai-php/client": "^0.3.5",
15+
"nyholm/psr7": "^1.5",
16+
"openai-php/client": "^0.4.0",
17+
"psr/http-client": "^1.0.1",
18+
"psr/http-factory": "^1.0.1",
1619
"symfony/config": "^5.4.21|^6.2.7",
1720
"symfony/dependency-injection": "^5.4.21|^6.2.7",
21+
"symfony/http-client": "^5.4.21|^6.2.7",
1822
"symfony/http-kernel": "^5.4.21|^6.2.7"
1923
},
2024
"require-dev": {
@@ -37,7 +41,10 @@
3741
"prefer-stable": true,
3842
"config": {
3943
"sort-packages": true,
40-
"preferred-install": "dist"
44+
"preferred-install": "dist",
45+
"allow-plugins": {
46+
"php-http/discovery": false
47+
}
4148
},
4249
"scripts": {
4350
"lint": "pint -v",

src/DependencyInjection/Configuration.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,8 @@ public function getConfigTreeBuilder(): TreeBuilder
2828

2929
assert($children instanceof NodeBuilder);
3030

31-
$children = $children->scalarNode('api_key')->defaultValue('%env(OPENAI_API_KEY)%')->end();
32-
33-
assert($children instanceof NodeBuilder);
34-
35-
$children = $children->scalarNode('organization')->defaultValue('%env(default::OPENAI_ORGANIZATION)%')->end();
36-
37-
assert($children instanceof NodeBuilder);
38-
39-
$children->end();
31+
$children->scalarNode('api_key')->defaultValue('%env(OPENAI_API_KEY)%')->end();
32+
$children->scalarNode('organization')->defaultValue('%env(default::OPENAI_ORGANIZATION)%')->end();
4033

4134
return $treeBuilder;
4235
}

src/DependencyInjection/OpenAIExtension.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace OpenAI\Symfony\DependencyInjection;
66

7-
use OpenAI\Client;
7+
use OpenAI\Factory;
88
use Symfony\Component\Config\Definition\ConfigurationInterface;
99
use Symfony\Component\Config\FileLocator;
1010
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -30,8 +30,10 @@ public function load(array $configs, ContainerBuilder $container): void
3030

3131
$config = $this->processConfiguration($configuration, $configs);
3232

33-
$definition = $container->getDefinition(Client::class);
34-
$definition->setArgument(0, $config['api_key']);
35-
$definition->setArgument(1, $config['organization']);
33+
$definition = $container->getDefinition(Factory::class);
34+
$definition->addMethodCall('withApiKey', [$config['api_key']]);
35+
if ($config['organization']) {
36+
$definition->addMethodCall('withOrganization', [$config['organization']]);
37+
}
3638
}
3739
}

src/Resources/config/services.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66

77
use OpenAI;
88
use OpenAI\Client;
9+
use OpenAI\Factory;
10+
use Symfony\Component\HttpClient\Psr18Client;
911

1012
return static function (ContainerConfigurator $container) {
1113
$container->services()
14+
->set('openai.http_client', Psr18Client::class)
15+
->arg(0, service('http_client'))
16+
17+
->set(Factory::class)
18+
->factory([OpenAI::class, 'factory'])
19+
->call('withHttpClient', [service('openai.http_client')])
20+
1221
->set(Client::class)
13-
->factory([OpenAI::class, 'client'])
14-
->args([
15-
abstract_arg('API Key'),
16-
abstract_arg('Organisation'),
17-
])
22+
->factory([service(Factory::class), 'make'])
23+
1824
->alias('openai', Client::class);
1925
};

tests/DependencyInjection/OpenAIExtensionTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,30 @@
88
use OpenAI\Symfony\DependencyInjection\OpenAIExtension;
99
use PHPUnit\Framework\TestCase;
1010
use Symfony\Component\DependencyInjection\ContainerBuilder;
11+
use Symfony\Component\HttpClient\MockHttpClient;
12+
use Symfony\Component\HttpClient\Response\MockResponse;
1113

1214
final class OpenAIExtensionTest extends TestCase
1315
{
1416
public function testService(): void
1517
{
18+
// Using a mock to test the service configuration
19+
$httpClient = new MockHttpClient(function (string $method, string $url, array $options = []) {
20+
self::assertSame('DELETE', $method);
21+
self::assertSame('https://api.openai.com/v1/files/file.txt', $url);
22+
self::assertContains('Authorization: Bearer pk-123456789', $options['headers']);
23+
24+
return new MockResponse('{"id":"file.txt","object":"file","deleted":true}', [
25+
'http_code' => 200,
26+
'response_headers' => [
27+
'content-type' => 'application/json',
28+
],
29+
]);
30+
});
31+
1632
$container = new ContainerBuilder();
33+
$container->set('http_client', $httpClient);
34+
1735
$extension = new OpenAIExtension();
1836
$extension->load([
1937
'openai' => [
@@ -23,5 +41,8 @@ public function testService(): void
2341

2442
$openai = $container->get('openai');
2543
self::assertInstanceOf(Client::class, $openai);
44+
45+
$response = $openai->files()->delete('file.txt');
46+
self::assertSame('file.txt', $response->id);
2647
}
2748
}

0 commit comments

Comments
 (0)