diff options
author | Petr Pchelko <ppchelko@wikimedia.org> | 2021-10-22 10:04:11 -0700 |
---|---|---|
committer | Ppchelko <ppchelko@wikimedia.org> | 2021-11-05 16:41:22 +0000 |
commit | d8b92b761c26af37a9447dd6287b7c07c51de8cd (patch) | |
tree | 2d56b7f549201fb10cfc100bc5cc39d565edeb11 /includes/shell/ShellboxClientFactory.php | |
parent | db5cb74486fdc9d5e9827791097a301bb444b096 (diff) | |
download | mediawikicore-d8b92b761c26af37a9447dd6287b7c07c51de8cd.tar.gz mediawikicore-d8b92b761c26af37a9447dd6287b7c07c51de8cd.zip |
ShellboxClientFactory: add RPCClient getters
Bug: T263437
Change-Id: I0802afa1ebabbfaca2244c599293556ce32673ae
Diffstat (limited to 'includes/shell/ShellboxClientFactory.php')
-rw-r--r-- | includes/shell/ShellboxClientFactory.php | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/includes/shell/ShellboxClientFactory.php b/includes/shell/ShellboxClientFactory.php index 1ce19c9285e9..348408ab732c 100644 --- a/includes/shell/ShellboxClientFactory.php +++ b/includes/shell/ShellboxClientFactory.php @@ -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; |