update 2023-07-12 09:14:53

This commit is contained in:
Robot 2023-07-12 09:14:57 +02:00
parent 2244f466d4
commit a15c785c23
Signed by untrusted user: Robot
GPG Key ID: 14DECD44E7E1BB95
5 changed files with 247 additions and 31 deletions

View File

@ -83,9 +83,30 @@ class Utilities implements ServiceProviderInterface
*/
public function getHttp(Container $container): Http
{
$openai_token = null;
$openai_org_token = null;
if (Helper::getParams()->get('enable_open_ai') == 1)
{
$openai_token = Helper::getParams()->get('openai_token');
if (Helper::getParams()->get('enable_open_ai_org') == 1)
{
$openai_org_token = Helper::getParams()->get('openai_org_token');
}
if ($openai_token === 'secret')
{
$openai_token = null;
}
if ($openai_org_token === 'secret')
{
$openai_org_token = null;
}
}
return new Http(
Helper::getParams()->get('openai_token'),
Helper::getParams()->get('openai_org_token')
$openai_token,
$openai_org_token
);
}
}

View File

@ -54,8 +54,29 @@
*/
public function getHttp(Container $container): Http
{
$openai_token = null;
$openai_org_token = null;
if (Helper::getParams()->get('enable_open_ai') == 1)
{
$openai_token = Helper::getParams()->get('openai_token');
if (Helper::getParams()->get('enable_open_ai_org') == 1)
{
$openai_org_token = Helper::getParams()->get('openai_org_token');
}
if ($openai_token === 'secret')
{
$openai_token = null;
}
if ($openai_org_token === 'secret')
{
$openai_org_token = null;
}
}
return new Http(
Helper::getParams()->get('openai_token'),
Helper::getParams()->get('openai_org_token')
$openai_token,
$openai_org_token
);
}

View File

@ -11,7 +11,11 @@
```uml
@startuml
class Http << (F,LightGreen) >> #Green {
+ __construct(?string $token, ?string $org = null)
# array $defaultHeaders
+ __construct(?string $token, ?string $orgToken = null)
+ setTokens(?string $token = null, ?string $orgToken = null)
+ setToken(string $token)
+ setOrgToken(string $token)
}
note right of Http::__construct
@ -19,6 +23,24 @@ note right of Http::__construct
since: 3.2.0
end note
note right of Http::setTokens
Change the Tokens.
since: 3.2.0
end note
note right of Http::setToken
Change the User Token.
since: 3.2.0
end note
note right of Http::setOrgToken
Change the Organization Token.
since: 3.2.0
end note
@enduml
```

View File

@ -23,41 +23,117 @@ use Joomla\Registry\Registry;
*/
final class Http extends JoomlaHttp
{
/**
* The default Header
*
* @var array
* @since 3.2.0
*/
protected array $defaultHeaders = ['Content-Type' => 'application/json'];
/**
* Constructor.
*
* @param string|null $token The Openai API token.
* @param string|null $org The Openai API Organization token.
* @param string|null $orgToken The Openai API Organization token.
*
* @since 3.2.0
* @throws \InvalidArgumentException
**/
public function __construct(?string $token, ?string $org = null)
public function __construct(?string $token, ?string $orgToken = null)
{
// setup config
$config = [
'userAgent' => 'JoomlaOpenai/3.0',
'headers' => [
'Content-Type' => 'application/json'
]
];
// add the token if given
if (is_string($token))
{
$config['headers']['Authorization'] = 'Bearer ' . $token;
$this->defaultHeaders['Authorization'] = 'Bearer ' . $token;
}
// add the organization token if given
if (is_string($org))
if (is_string($orgToken))
{
$config['headers']['OpenAI-Organization'] = $org;
$this->defaultHeaders['OpenAI-Organization'] = $orgToken;
}
// setup config
$config = [
'userAgent' => 'JoomlaOpenai/3.0',
'headers' => $this->defaultHeaders
];
$options = new Registry($config);
// run parent constructor
parent::__construct($options);
}
/**
* Change the Tokens.
*
* @param string|null $token The Openai API token.
* @param string|null $orgToken The Openai API Organization token.
*
* @since 3.2.0
**/
public function setTokens(?string $token = null, ?string $orgToken = null)
{
// get the current headers
$this->defaultHeaders = (array) $this->getOption('headers',
$this->defaultHeaders
);
// add the token if given
if (is_string($token))
{
$this->defaultHeaders['Authorization'] = 'Bearer ' . $token;
}
// add the organization token if given
if (is_string($orgToken))
{
$this->defaultHeaders['OpenAI-Organization'] = $orgToken;
}
$this->setOption('headers', $this->defaultHeaders);
}
/**
* Change the User Token.
*
* @param string $token The API token.
*
* @since 3.2.0
**/
public function setToken(string $token)
{
// get the current headers
$this->defaultHeaders = (array) $this->getOption('headers',
$this->defaultHeaders
);
// add the token
$this->defaultHeaders['Authorization'] = 'Bearer ' . $token;
$this->setOption('headers', $this->defaultHeaders);
}
/**
* Change the Organization Token.
*
* @param string $token The Organization API token.
*
* @since 3.2.0
**/
public function setOrgToken(string $token)
{
// get the current headers
$this->defaultHeaders = (array) $this->getOption('headers',
$this->defaultHeader
);
// add the token
$this->defaultHeaders['OpenAI-Organization'] = $token;
$this->setOption('headers', $this->defaultHeaders);
}
}

View File

@ -1,36 +1,112 @@
/**
* The default Header
*
* @var array
* @since 3.2.0
*/
protected array $defaultHeaders = ['Content-Type' => 'application/json'];
/**
* Constructor.
*
* @param string|null $token The Openai API token.
* @param string|null $org The Openai API Organization token.
* @param string|null $orgToken The Openai API Organization token.
*
* @since 3.2.0
* @throws \InvalidArgumentException
**/
public function __construct(?string $token, ?string $org = null)
public function __construct(?string $token, ?string $orgToken = null)
{
// setup config
$config = [
'userAgent' => 'JoomlaOpenai/3.0',
'headers' => [
'Content-Type' => 'application/json'
]
];
// add the token if given
if (is_string($token))
{
$config['headers']['Authorization'] = 'Bearer ' . $token;
$this->defaultHeaders['Authorization'] = 'Bearer ' . $token;
}
// add the organization token if given
if (is_string($org))
if (is_string($orgToken))
{
$config['headers']['OpenAI-Organization'] = $org;
$this->defaultHeaders['OpenAI-Organization'] = $orgToken;
}
// setup config
$config = [
'userAgent' => 'JoomlaOpenai/3.0',
'headers' => $this->defaultHeaders
];
$options = new Registry($config);
// run parent constructor
parent::__construct($options);
}
/**
* Change the Tokens.
*
* @param string|null $token The Openai API token.
* @param string|null $orgToken The Openai API Organization token.
*
* @since 3.2.0
**/
public function setTokens(?string $token = null, ?string $orgToken = null)
{
// get the current headers
$this->defaultHeaders = (array) $this->getOption('headers',
$this->defaultHeaders
);
// add the token if given
if (is_string($token))
{
$this->defaultHeaders['Authorization'] = 'Bearer ' . $token;
}
// add the organization token if given
if (is_string($orgToken))
{
$this->defaultHeaders['OpenAI-Organization'] = $orgToken;
}
$this->setOption('headers', $this->defaultHeaders);
}
/**
* Change the User Token.
*
* @param string $token The API token.
*
* @since 3.2.0
**/
public function setToken(string $token)
{
// get the current headers
$this->defaultHeaders = (array) $this->getOption('headers',
$this->defaultHeaders
);
// add the token
$this->defaultHeaders['Authorization'] = 'Bearer ' . $token;
$this->setOption('headers', $this->defaultHeaders);
}
/**
* Change the Organization Token.
*
* @param string $token The Organization API token.
*
* @since 3.2.0
**/
public function setOrgToken(string $token)
{
// get the current headers
$this->defaultHeaders = (array) $this->getOption('headers',
$this->defaultHeader
);
// add the token
$this->defaultHeaders['OpenAI-Organization'] = $token;
$this->setOption('headers', $this->defaultHeaders);
}