From a15c785c23d4381fa13ec5dbdfe2e7201baab597 Mon Sep 17 00:00:00 2001 From: aB0t Date: Wed, 12 Jul 2023 09:14:57 +0200 Subject: [PATCH] update 2023-07-12 09:14:53 --- .../code.php | 25 ++++- .../code.power | 25 ++++- .../README.md | 24 ++++- .../code.php | 102 +++++++++++++++--- .../code.power | 102 +++++++++++++++--- 5 files changed, 247 insertions(+), 31 deletions(-) diff --git a/src/07858e1f-5698-4227-8941-03dc93557938/code.php b/src/07858e1f-5698-4227-8941-03dc93557938/code.php index bd75d71..b7f6a4c 100644 --- a/src/07858e1f-5698-4227-8941-03dc93557938/code.php +++ b/src/07858e1f-5698-4227-8941-03dc93557938/code.php @@ -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 ); } } diff --git a/src/07858e1f-5698-4227-8941-03dc93557938/code.power b/src/07858e1f-5698-4227-8941-03dc93557938/code.power index a0851b0..9726b0c 100644 --- a/src/07858e1f-5698-4227-8941-03dc93557938/code.power +++ b/src/07858e1f-5698-4227-8941-03dc93557938/code.power @@ -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 ); } \ No newline at end of file diff --git a/src/4c58b254-aea5-40aa-b54d-9586a1690451/README.md b/src/4c58b254-aea5-40aa-b54d-9586a1690451/README.md index 0658582..d9a4674 100644 --- a/src/4c58b254-aea5-40aa-b54d-9586a1690451/README.md +++ b/src/4c58b254-aea5-40aa-b54d-9586a1690451/README.md @@ -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 ``` diff --git a/src/4c58b254-aea5-40aa-b54d-9586a1690451/code.php b/src/4c58b254-aea5-40aa-b54d-9586a1690451/code.php index 7ebb977..6d0b705 100644 --- a/src/4c58b254-aea5-40aa-b54d-9586a1690451/code.php +++ b/src/4c58b254-aea5-40aa-b54d-9586a1690451/code.php @@ -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); } } diff --git a/src/4c58b254-aea5-40aa-b54d-9586a1690451/code.power b/src/4c58b254-aea5-40aa-b54d-9586a1690451/code.power index 6519ef6..6eecc76 100644 --- a/src/4c58b254-aea5-40aa-b54d-9586a1690451/code.power +++ b/src/4c58b254-aea5-40aa-b54d-9586a1690451/code.power @@ -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); } \ No newline at end of file