Swap http client out

This commit is contained in:
Lance Pioch 2024-03-23 16:10:47 -04:00
parent 1ffc65897b
commit 358ac96ac1
8 changed files with 32 additions and 38 deletions

View File

@ -5,7 +5,6 @@ namespace App\Repositories\Daemon;
use Webmozart\Assert\Assert; use Webmozart\Assert\Assert;
use App\Models\Backup; use App\Models\Backup;
use App\Models\Server; use App\Models\Server;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\TransferException; use GuzzleHttp\Exception\TransferException;
use App\Exceptions\Http\Connection\DaemonConnectionException; use App\Exceptions\Http\Connection\DaemonConnectionException;
@ -28,7 +27,7 @@ class DaemonBackupRepository extends DaemonRepository
* *
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException * @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function backup(Backup $backup): ResponseInterface public function backup(Backup $backup)
{ {
Assert::isInstanceOf($this->server, Server::class); Assert::isInstanceOf($this->server, Server::class);
@ -53,7 +52,7 @@ class DaemonBackupRepository extends DaemonRepository
* *
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException * @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function restore(Backup $backup, string $url = null, bool $truncate = false): ResponseInterface public function restore(Backup $backup, string $url = null, bool $truncate = false)
{ {
Assert::isInstanceOf($this->server, Server::class); Assert::isInstanceOf($this->server, Server::class);
@ -78,7 +77,7 @@ class DaemonBackupRepository extends DaemonRepository
* *
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException * @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function delete(Backup $backup): ResponseInterface public function delete(Backup $backup)
{ {
Assert::isInstanceOf($this->server, Server::class); Assert::isInstanceOf($this->server, Server::class);

View File

@ -3,7 +3,6 @@
namespace App\Repositories\Daemon; namespace App\Repositories\Daemon;
use App\Models\Node; use App\Models\Node;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\TransferException; use GuzzleHttp\Exception\TransferException;
use App\Exceptions\Http\Connection\DaemonConnectionException; use App\Exceptions\Http\Connection\DaemonConnectionException;
@ -22,7 +21,7 @@ class DaemonConfigurationRepository extends DaemonRepository
throw new DaemonConnectionException($exception); throw new DaemonConnectionException($exception);
} }
return json_decode($response->getBody()->__toString(), true); return $response->json();
} }
/** /**
@ -32,7 +31,7 @@ class DaemonConfigurationRepository extends DaemonRepository
* *
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException * @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function update(Node $node): ResponseInterface public function update(Node $node)
{ {
try { try {
return $this->getHttpClient()->post( return $this->getHttpClient()->post(

View File

@ -2,10 +2,9 @@
namespace App\Repositories\Daemon; namespace App\Repositories\Daemon;
use Illuminate\Support\Arr; use Carbon\CarbonInterval;
use Webmozart\Assert\Assert; use Webmozart\Assert\Assert;
use App\Models\Server; use App\Models\Server;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\TransferException; use GuzzleHttp\Exception\TransferException;
use App\Exceptions\Http\Server\FileSizeTooLargeException; use App\Exceptions\Http\Server\FileSizeTooLargeException;
@ -37,12 +36,12 @@ class DaemonFileRepository extends DaemonRepository
throw new DaemonConnectionException($exception); throw new DaemonConnectionException($exception);
} }
$length = (int) Arr::get($response->getHeader('Content-Length'), 0, 0); $length = $response->header('Content-Length');
if ($notLargerThan && $length > $notLargerThan) { if ($notLargerThan && $length > $notLargerThan) {
throw new FileSizeTooLargeException(); throw new FileSizeTooLargeException();
} }
return $response->getBody()->__toString(); return $response;
} }
/** /**
@ -51,7 +50,7 @@ class DaemonFileRepository extends DaemonRepository
* *
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException * @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function putContent(string $path, string $content): ResponseInterface public function putContent(string $path, string $content)
{ {
Assert::isInstanceOf($this->server, Server::class); Assert::isInstanceOf($this->server, Server::class);
@ -88,7 +87,7 @@ class DaemonFileRepository extends DaemonRepository
throw new DaemonConnectionException($exception); throw new DaemonConnectionException($exception);
} }
return json_decode($response->getBody(), true); return $response->json();
} }
/** /**
@ -96,7 +95,7 @@ class DaemonFileRepository extends DaemonRepository
* *
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException * @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function createDirectory(string $name, string $path): ResponseInterface public function createDirectory(string $name, string $path)
{ {
Assert::isInstanceOf($this->server, Server::class); Assert::isInstanceOf($this->server, Server::class);
@ -120,7 +119,7 @@ class DaemonFileRepository extends DaemonRepository
* *
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException * @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function renameFiles(?string $root, array $files): ResponseInterface public function renameFiles(?string $root, array $files)
{ {
Assert::isInstanceOf($this->server, Server::class); Assert::isInstanceOf($this->server, Server::class);
@ -144,7 +143,7 @@ class DaemonFileRepository extends DaemonRepository
* *
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException * @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function copyFile(string $location): ResponseInterface public function copyFile(string $location)
{ {
Assert::isInstanceOf($this->server, Server::class); Assert::isInstanceOf($this->server, Server::class);
@ -167,7 +166,7 @@ class DaemonFileRepository extends DaemonRepository
* *
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException * @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function deleteFiles(?string $root, array $files): ResponseInterface public function deleteFiles(?string $root, array $files)
{ {
Assert::isInstanceOf($this->server, Server::class); Assert::isInstanceOf($this->server, Server::class);
@ -212,7 +211,7 @@ class DaemonFileRepository extends DaemonRepository
throw new DaemonConnectionException($exception); throw new DaemonConnectionException($exception);
} }
return json_decode($response->getBody(), true); return $response->json();
} }
/** /**
@ -220,7 +219,7 @@ class DaemonFileRepository extends DaemonRepository
* *
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException * @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function decompressFile(?string $root, string $file): ResponseInterface public function decompressFile(?string $root, string $file)
{ {
Assert::isInstanceOf($this->server, Server::class); Assert::isInstanceOf($this->server, Server::class);
@ -234,7 +233,7 @@ class DaemonFileRepository extends DaemonRepository
], ],
// Wait for up to 15 minutes for the decompress to be completed when calling this endpoint // Wait for up to 15 minutes for the decompress to be completed when calling this endpoint
// since it will likely take quite awhile for large directories. // since it will likely take quite awhile for large directories.
'timeout' => 60 * 15, 'timeout' => (int) CarbonInterval::minutes(15)->totalSeconds,
] ]
); );
} catch (TransferException $exception) { } catch (TransferException $exception) {
@ -247,7 +246,7 @@ class DaemonFileRepository extends DaemonRepository
* *
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException * @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function chmodFiles(?string $root, array $files): ResponseInterface public function chmodFiles(?string $root, array $files)
{ {
Assert::isInstanceOf($this->server, Server::class); Assert::isInstanceOf($this->server, Server::class);
@ -271,7 +270,7 @@ class DaemonFileRepository extends DaemonRepository
* *
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException * @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function pull(string $url, ?string $directory, array $params = []): ResponseInterface public function pull(string $url, ?string $directory, array $params = [])
{ {
Assert::isInstanceOf($this->server, Server::class); Assert::isInstanceOf($this->server, Server::class);

View File

@ -4,7 +4,6 @@ namespace App\Repositories\Daemon;
use Webmozart\Assert\Assert; use Webmozart\Assert\Assert;
use App\Models\Server; use App\Models\Server;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\TransferException; use GuzzleHttp\Exception\TransferException;
use App\Exceptions\Http\Connection\DaemonConnectionException; use App\Exceptions\Http\Connection\DaemonConnectionException;
@ -15,7 +14,7 @@ class DaemonPowerRepository extends DaemonRepository
* *
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException * @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/ */
public function send(string $action): ResponseInterface public function send(string $action)
{ {
Assert::isInstanceOf($this->server, Server::class); Assert::isInstanceOf($this->server, Server::class);

View File

@ -4,6 +4,8 @@ namespace App\Repositories\Daemon;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use App\Models\Node; use App\Models\Node;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
use Webmozart\Assert\Assert; use Webmozart\Assert\Assert;
use App\Models\Server; use App\Models\Server;
use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Foundation\Application;
@ -48,20 +50,10 @@ abstract class DaemonRepository
/** /**
* Return an instance of the Guzzle HTTP Client to be used for requests. * Return an instance of the Guzzle HTTP Client to be used for requests.
*/ */
public function getHttpClient(array $headers = []): Client public function getHttpClient(array $headers = []): PendingRequest
{ {
Assert::isInstanceOf($this->node, Node::class); Assert::isInstanceOf($this->node, Node::class);
return new Client([ return Http::daemon($this->node, $headers);
'verify' => $this->app->environment('production'),
'base_uri' => $this->node->getConnectionAddress(),
'timeout' => config('panel.guzzle.timeout'),
'connect_timeout' => config('panel.guzzle.connect_timeout'),
'headers' => array_merge($headers, [
'Authorization' => 'Bearer ' . $this->node->getDecryptedKey(),
'Accept' => 'application/json',
'Content-Type' => 'application/json',
]),
]);
} }
} }

View File

@ -27,10 +27,11 @@ class DaemonServerRepository extends DaemonRepository
throw new DaemonConnectionException($exception, false); throw new DaemonConnectionException($exception, false);
} }
return json_decode($response->getBody()->__toString(), true); return $response->json();
} }
/** /**
* Creates a new server on the daemon.
* *
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException * @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/ */

View File

@ -6,6 +6,7 @@ use App\Models\User;
use App\Models\Subuser; use App\Models\Subuser;
use App\Models\Permission; use App\Models\Permission;
use App\Tests\Integration\Api\Client\ClientApiIntegrationTestCase; use App\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
use Illuminate\Support\Facades\Http;
class UpdateSubuserTest extends ClientApiIntegrationTestCase class UpdateSubuserTest extends ClientApiIntegrationTestCase
{ {
@ -17,6 +18,8 @@ class UpdateSubuserTest extends ClientApiIntegrationTestCase
{ {
[$user, $server] = $this->generateTestAccount(['user.read']); [$user, $server] = $this->generateTestAccount(['user.read']);
Http::fake();
$subuser = Subuser::factory() $subuser = Subuser::factory()
->for(User::factory()->create()) ->for(User::factory()->create())
->for($server) ->for($server)
@ -65,6 +68,8 @@ class UpdateSubuserTest extends ClientApiIntegrationTestCase
'permissions' => ['control.restart', 'websocket.connect', 'foo.bar'], 'permissions' => ['control.restart', 'websocket.connect', 'foo.bar'],
]); ]);
Http::fake();
$this->actingAs($user) $this->actingAs($user)
->postJson("/api/client/servers/$server->uuid/users/{$subuser->user->uuid}", [ ->postJson("/api/client/servers/$server->uuid/users/{$subuser->user->uuid}", [
'permissions' => [ 'permissions' => [

View File

@ -93,7 +93,7 @@ class RunTaskJobTest extends IntegrationTestCase
$mock->expects('setServer')->with(\Mockery::on(function ($value) use ($server) { $mock->expects('setServer')->with(\Mockery::on(function ($value) use ($server) {
return $value instanceof Server && $value->id === $server->id; return $value instanceof Server && $value->id === $server->id;
}))->andReturnSelf(); }))->andReturnSelf();
$mock->expects('send')->with('start')->andReturn(new Response()); $mock->expects('send')->with('start');
Bus::dispatchSync(new RunTaskJob($task, $isManualRun)); Bus::dispatchSync(new RunTaskJob($task, $isManualRun));