ShellboxClientFactory: add RPCClient getters

Bug: T263437
Change-Id: I0802afa1ebabbfaca2244c599293556ce32673ae
This commit is contained in:
Petr Pchelko 2021-10-22 10:04:11 -07:00 committed by Ppchelko
parent db5cb74486
commit d8b92b761c
2 changed files with 54 additions and 2 deletions

View file

@ -5,7 +5,10 @@ namespace MediaWiki\Shell;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\RequestOptions;
use MediaWiki\Http\HttpRequestFactory;
use RuntimeException;
use Shellbox\Client;
use Shellbox\RPC\LocalRpcClient;
use Shellbox\RPC\RpcClient;
use WebRequest;
/**
@ -56,12 +59,12 @@ class ShellboxClientFactory {
* - timeout: The request timeout in seconds
* - service: the shellbox backend name to get the URL from the mapping
* @return Client
* @throws \RuntimeException
* @throws RuntimeException
*/
public function getClient( array $options = [] ) {
$url = $this->getUrl( $options['service'] ?? null );
if ( $url === null ) {
throw new \RuntimeException( 'To use a remote shellbox to run shell commands, ' .
throw new RuntimeException( 'To use a remote shellbox to run shell commands, ' .
'$wgShellboxUrls and $wgShellboxSecretKey must be configured.' );
}
@ -78,6 +81,35 @@ class ShellboxClientFactory {
);
}
/**
* Get a Shellbox RPC client with the specified options. If remote Shellbox is
* not configured (isEnabled() returns false), an exception will be thrown.
*
* @param array $options Associative array of options:
* - timeout: The request timeout in seconds
* - service: the shellbox backend name to get the URL from the mapping
* @return RpcClient
* @throws RuntimeException
*/
public function getRemoteRpcClient( array $options = [] ): RpcClient {
return $this->getClient( $options );
}
/**
* Get a Shellbox RPC client with specified options. If remote Shellbox is
* not configured (isEnabled() returns false), a local fallback is returned.
*
* @param array $options
* @return RpcClient
*/
public function getRpcClient( array $options = [] ): RpcClient {
$url = $this->getUrl( $options['service'] ?? null );
if ( $url === null ) {
return new LocalRpcClient();
}
return $this->getRemoteRpcClient( $options );
}
private function getUrl( ?string $service ): ?string {
if ( $this->urls === null || !strlen( $this->key ) ) {
return null;

View file

@ -2,6 +2,7 @@
use MediaWiki\Http\HttpRequestFactory;
use MediaWiki\Shell\ShellboxClientFactory;
use Shellbox\RPC\RpcClient;
/**
* @group Shell
@ -81,4 +82,23 @@ class ShellboxClientFactoryTest extends MediaWikiUnitTestCase {
$this->assertFalse( $shellboxClientFactory->isEnabled() );
}
public function testGetRemoteRpcClientNotEnabled() {
$shellboxClientFactory = new ShellboxClientFactory(
$this->createMock( HttpRequestFactory::class ),
null,
'key'
);
$this->expectException( RuntimeException::class );
$shellboxClientFactory->getRemoteRpcClient();
}
public function testGetRpcClientNotEnabled() {
$shellboxClientFactory = new ShellboxClientFactory(
$this->createMock( HttpRequestFactory::class ),
null,
'key'
);
$this->assertInstanceOf( RpcClient::class, $shellboxClientFactory->getRpcClient() );
}
}