update 2023-10-24 10:36:32

This commit is contained in:
Robot 2023-10-24 10:36:35 +02:00
parent 71bd39cb1e
commit b36508a0bd
Signed by: Robot
GPG Key ID: 14DECD44E7E1BB95
13 changed files with 250 additions and 19 deletions

View File

@ -14,8 +14,12 @@ abstract Api #Orange {
# Http $http
# Uri $uri
# Response $response
# ?string $url
# ?string $token
+ __construct(Http $http, Uri $uri, ...)
+ load_(?string $url = null, ?string $token = null) : void
+ load_(?string $url = null, ?string $token = null, ...) : void
+ reset_() : void
+ api() : string
}
note right of Api::__construct
@ -34,6 +38,25 @@ note right of Api::load_
since: 3.2.0
return: void
arguments:
?string $url = null
?string $token = null
bool $backup = true
end note
note right of Api::reset_
Reset to previous toke, url it set
since: 3.2.0
return: void
end note
note right of Api::api
Get the API url
since: 3.2.0
return: string
end note
@enduml

View File

@ -48,6 +48,22 @@ abstract class Api
*/
protected Response $response;
/**
* The Url string
*
* @var string|null
* @since 3.2.0
*/
protected ?string $url = null;
/**
* The token string
*
* @var string|null
* @since 3.2.0
*/
protected ?string $token = null;
/**
* Constructor.
*
@ -67,14 +83,31 @@ abstract class Api
/**
* Load/Reload API.
*
* @param string|null $url The url.
* @param token|null $token The token.
* @param string|null $url The url.
* @param token|null $token The token.
* @param bool $backup The backup swapping switch.
*
* @return void
* @since 3.2.0
**/
public function load_(?string $url = null, ?string $token = null)
public function load_(?string $url = null, ?string $token = null, bool $backup = true): void
{
// we keep the old values
// so we can reset after our call
// for the rest of the container
if ($backup)
{
if ($url !== null)
{
$this->url = $this->uri->getUrl();
}
if ($token !== null)
{
$this->token = $this->http->getToken();
}
}
if ($url !== null)
{
$this->uri->setUrl($url);
@ -85,6 +118,37 @@ abstract class Api
$this->http->setToken($token);
}
}
/**
* Reset to previous toke, url it set
*
* @return void
* @since 3.2.0
**/
public function reset_(): void
{
if ($this->url !== null)
{
$this->uri->setUrl($this->url);
$this->url = null;
}
if ($this->token !== null)
{
$this->http->setToken($this->token);
$this->token = null;
}
}
/**
* Get the API url
*
* @return string
* @since 3.2.0
**/
public function api()
{
return $this->uri->api();
}
}

View File

@ -22,6 +22,22 @@
*/
protected Response $response;
/**
* The Url string
*
* @var string|null
* @since 3.2.0
*/
protected ?string $url = null;
/**
* The token string
*
* @var string|null
* @since 3.2.0
*/
protected ?string $token = null;
/**
* Constructor.
*
@ -41,14 +57,31 @@
/**
* Load/Reload API.
*
* @param string|null $url The url.
* @param token|null $token The token.
* @param string|null $url The url.
* @param token|null $token The token.
* @param bool $backup The backup swapping switch.
*
* @return void
* @since 3.2.0
**/
public function load_(?string $url = null, ?string $token = null)
public function load_(?string $url = null, ?string $token = null, bool $backup = true): void
{
// we keep the old values
// so we can reset after our call
// for the rest of the container
if ($backup)
{
if ($url !== null)
{
$this->url = $this->uri->getUrl();
}
if ($token !== null)
{
$this->token = $this->http->getToken();
}
}
if ($url !== null)
{
$this->uri->setUrl($url);
@ -59,3 +92,35 @@
$this->http->setToken($token);
}
}
/**
* Reset to previous toke, url it set
*
* @return void
* @since 3.2.0
**/
public function reset_(): void
{
if ($this->url !== null)
{
$this->uri->setUrl($this->url);
$this->url = null;
}
if ($this->token !== null)
{
$this->http->setToken($this->token);
$this->token = null;
}
}
/**
* Get the API url
*
* @return string
* @since 3.2.0
**/
public function api()
{
return $this->uri->api();
}

View File

@ -18,6 +18,7 @@ class Uri << (F,LightGreen) >> #RoyalBlue {
+ get(string $path) : JoomlaUri
+ api() : string
+ setUrl(string $url) : void
+ getUrl() : ?string
- setEndpoint(string $endpoint) : void
- setVersion(string $version) : void
}
@ -58,6 +59,13 @@ example: https://git.vdm.dev
return: void
end note
note right of Uri::getUrl
Get the URL of the API
since: 3.2.0
return: ?string
end note
note right of Uri::setEndpoint
Set the endpoint of the API

View File

@ -110,6 +110,17 @@ final class Uri
$this->url = $url;
}
/**
* Get the URL of the API
*
* @return string|null
* @since 3.2.0
**/
public function getUrl(): ?string
{
return $this->url ?? null;
}
/**
* Set the endpoint of the API
*
@ -134,7 +145,6 @@ final class Uri
private function setVersion($version)
{
$this->version = $version;
}
}
}

View File

@ -86,6 +86,17 @@
$this->url = $url;
}
/**
* Get the URL of the API
*
* @return string|null
* @since 3.2.0
**/
public function getUrl(): ?string
{
return $this->url ?? null;
}
/**
* Set the endpoint of the API
*
@ -110,4 +121,4 @@
private function setVersion($version)
{
$this->version = $version;
}
}

View File

@ -57,7 +57,7 @@ class Gitea implements ServiceProviderInterface
$gitea_url = $container->get('Config')->get('custom_gitea_url');
// only load this if we have a custom URL set
if ($add_gitea_url == 2 && is_string($gitea_url) && strpos($gitea_url, 'http') !== false)
if ($add_gitea_url == 2 && !empty($gitea_url) && strpos($gitea_url, 'http') !== false)
{
return new Uri($gitea_url);
}

View File

@ -30,7 +30,7 @@
$gitea_url = $container->get('Config')->get('custom_gitea_url');
// only load this if we have a custom URL set
if ($add_gitea_url == 2 && is_string($gitea_url) && strpos($gitea_url, 'http') !== false)
if ($add_gitea_url == 2 && !empty($gitea_url) && strpos($gitea_url, 'http') !== false)
{
return new Uri($gitea_url);
}

View File

@ -13,7 +13,8 @@
@startuml
class Http << (F,LightGreen) >> #RoyalBlue {
+ __construct(?string $token)
+ setToken(string $token)
+ setToken(string $token) : void
+ getToken() : ?string
}
note right of Http::__construct
@ -26,6 +27,14 @@ note right of Http::setToken
Change the Token.
since: 3.2.0
return: void
end note
note right of Http::getToken
Get the Token.
since: 3.2.0
return: ?string
end note
@enduml

View File

@ -23,6 +23,14 @@ use Joomla\Registry\Registry;
*/
final class Http extends JoomlaHttp
{
/**
* The token
*
* @var string
* @since 3.2.0
*/
protected string $_token_; // to avoid collusions (but allow swapping)
/**
* Constructor.
*
@ -45,6 +53,7 @@ final class Http extends JoomlaHttp
if (is_string($token))
{
$config['headers']['Authorization'] = 'token ' . $token;
$this->_token_ = $token;
}
$options = new Registry($config);
@ -60,7 +69,7 @@ final class Http extends JoomlaHttp
*
* @since 3.2.0
**/
public function setToken(string $token)
public function setToken(string $token): void
{
// get the current headers
$headers = (array) $this->getOption('headers', [
@ -70,9 +79,20 @@ final class Http extends JoomlaHttp
// add the token
$headers['Authorization'] = 'token ' . $token;
$this->_token_ = $token;
$this->setOption('headers', $headers);
}
/**
* Get the Token.
*
* @return string|null
* @since 3.2.0
**/
public function getToken(): ?string
{
return $this->_token_ ?? null;
}
}

View File

@ -1,3 +1,11 @@
/**
* The token
*
* @var string
* @since 3.2.0
*/
protected string $_token_; // to avoid collusions (but allow swapping)
/**
* Constructor.
*
@ -20,6 +28,7 @@
if (is_string($token))
{
$config['headers']['Authorization'] = 'token ' . $token;
$this->_token_ = $token;
}
$options = new Registry($config);
@ -35,7 +44,7 @@
*
* @since 3.2.0
**/
public function setToken(string $token)
public function setToken(string $token): void
{
// get the current headers
$headers = (array) $this->getOption('headers', [
@ -45,6 +54,18 @@
// add the token
$headers['Authorization'] = 'token ' . $token;
$this->_token_ = $token;
$this->setOption('headers', $headers);
}
/**
* Get the Token.
*
* @return string|null
* @since 3.2.0
**/
public function getToken(): ?string
{
return $this->_token_ ?? null;
}

View File

@ -58,7 +58,7 @@ class Jcb implements ServiceProviderInterface
$gitea_url = Helper::getParams('com_componentbuilder')->get('custom_gitea_url');
// only load this if we have a custom URL set
if ($add_gitea_url == 2 && is_string($gitea_url) && strpos($gitea_url, 'http') !== false)
if ($add_gitea_url == 2 && !empty($gitea_url) && strpos($gitea_url, 'http') !== false)
{
return new Uri($gitea_url);
}

View File

@ -30,7 +30,7 @@
$gitea_url = Helper::getParams('com_componentbuilder')->get('custom_gitea_url');
// only load this if we have a custom URL set
if ($add_gitea_url == 2 && is_string($gitea_url) && strpos($gitea_url, 'http') !== false)
if ($add_gitea_url == 2 && !empty($gitea_url) && strpos($gitea_url, 'http') !== false)
{
return new Uri($gitea_url);
}