This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Manager.php
69 lines (60 loc) · 1.54 KB
/
Manager.php
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
<?php
/**
* Copyright (c) 2022 Tectalic (https://tectalic.com)
*
* For copyright and license information, please view the LICENSE file that was distributed with this source code.
*
* Please see the README.md file for usage instructions.
*/
declare(strict_types=1);
namespace Tectalic\OpenAi;
use Http\Message\Authentication;
use LogicException;
use Psr\Http\Client\ClientInterface;
/**
* Holds and interacts with the API Client.
*/
final class Manager
{
public const BASE_URI = 'https://api.openai.com/v1';
/** @var Client|null */
protected static $client = null;
/**
* Build the Client.
*
* @param ClientInterface $client
* @param Authentication $auth
*
* @return Client
* @throws LogicException
*/
public static function build(ClientInterface $client, Authentication $auth): Client
{
if (!\is_null(self::$client)) {
throw new LogicException('Client already built.');
}
return self::$client = new Client($client, $auth, self::BASE_URI);
}
/**
* Check if a client globally accessible.
*
* @return boolean
*/
public static function isGlobal(): bool
{
return !\is_null(self::$client);
}
/**
* Access the client.
*
* @return Client
* @throws LogicException
*/
public static function access(): Client
{
if (\is_null(self::$client)) {
throw new LogicException('Client not configured. Use Client::build() first.');
}
return self::$client;
}
}