Skip to content

Commit

Permalink
Swap http client out
Browse files Browse the repository at this point in the history
  • Loading branch information
lancepioch committed Mar 23, 2024
1 parent 1ffc658 commit 358ac96
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 38 deletions.
7 changes: 3 additions & 4 deletions app/Repositories/Daemon/DaemonBackupRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Webmozart\Assert\Assert;
use App\Models\Backup;
use App\Models\Server;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\TransferException;
use App\Exceptions\Http\Connection\DaemonConnectionException;

Expand All @@ -28,7 +27,7 @@ public function setBackupAdapter(string $adapter): self
*
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/
public function backup(Backup $backup): ResponseInterface
public function backup(Backup $backup)
{
Assert::isInstanceOf($this->server, Server::class);

Expand All @@ -53,7 +52,7 @@ public function backup(Backup $backup): ResponseInterface
*
* @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);

Expand All @@ -78,7 +77,7 @@ public function restore(Backup $backup, string $url = null, bool $truncate = fal
*
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/
public function delete(Backup $backup): ResponseInterface
public function delete(Backup $backup)
{
Assert::isInstanceOf($this->server, Server::class);

Expand Down
5 changes: 2 additions & 3 deletions app/Repositories/Daemon/DaemonConfigurationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Repositories\Daemon;

use App\Models\Node;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\TransferException;
use App\Exceptions\Http\Connection\DaemonConnectionException;

Expand All @@ -22,7 +21,7 @@ public function getSystemInformation(?int $version = null): array
throw new DaemonConnectionException($exception);
}

return json_decode($response->getBody()->__toString(), true);
return $response->json();
}

/**
Expand All @@ -32,7 +31,7 @@ public function getSystemInformation(?int $version = null): array
*
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/
public function update(Node $node): ResponseInterface
public function update(Node $node)
{
try {
return $this->getHttpClient()->post(
Expand Down
29 changes: 14 additions & 15 deletions app/Repositories/Daemon/DaemonFileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace App\Repositories\Daemon;

use Illuminate\Support\Arr;
use Carbon\CarbonInterval;
use Webmozart\Assert\Assert;
use App\Models\Server;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\TransferException;
use App\Exceptions\Http\Server\FileSizeTooLargeException;
Expand Down Expand Up @@ -37,12 +36,12 @@ public function getContent(string $path, int $notLargerThan = null): string
throw new DaemonConnectionException($exception);
}

$length = (int) Arr::get($response->getHeader('Content-Length'), 0, 0);
$length = $response->header('Content-Length');
if ($notLargerThan && $length > $notLargerThan) {
throw new FileSizeTooLargeException();
}

return $response->getBody()->__toString();
return $response;
}

/**
Expand All @@ -51,7 +50,7 @@ public function getContent(string $path, int $notLargerThan = null): string
*
* @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);

Expand Down Expand Up @@ -88,15 +87,15 @@ public function getDirectory(string $path): array
throw new DaemonConnectionException($exception);
}

return json_decode($response->getBody(), true);
return $response->json();
}

/**
* Creates a new directory for the server in the given $path.
*
* @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);

Expand All @@ -120,7 +119,7 @@ public function createDirectory(string $name, string $path): ResponseInterface
*
* @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);

Expand All @@ -144,7 +143,7 @@ public function renameFiles(?string $root, array $files): ResponseInterface
*
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/
public function copyFile(string $location): ResponseInterface
public function copyFile(string $location)
{
Assert::isInstanceOf($this->server, Server::class);

Expand All @@ -167,7 +166,7 @@ public function copyFile(string $location): ResponseInterface
*
* @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);

Expand Down Expand Up @@ -212,15 +211,15 @@ public function compressFiles(?string $root, array $files): array
throw new DaemonConnectionException($exception);
}

return json_decode($response->getBody(), true);
return $response->json();
}

/**
* Decompresses a given archive file.
*
* @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);

Expand All @@ -234,7 +233,7 @@ public function decompressFile(?string $root, string $file): ResponseInterface
],
// 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.
'timeout' => 60 * 15,
'timeout' => (int) CarbonInterval::minutes(15)->totalSeconds,
]
);
} catch (TransferException $exception) {
Expand All @@ -247,7 +246,7 @@ public function decompressFile(?string $root, string $file): ResponseInterface
*
* @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);

Expand All @@ -271,7 +270,7 @@ public function chmodFiles(?string $root, array $files): ResponseInterface
*
* @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);

Expand Down
3 changes: 1 addition & 2 deletions app/Repositories/Daemon/DaemonPowerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Webmozart\Assert\Assert;
use App\Models\Server;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\TransferException;
use App\Exceptions\Http\Connection\DaemonConnectionException;

Expand All @@ -15,7 +14,7 @@ class DaemonPowerRepository extends DaemonRepository
*
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException
*/
public function send(string $action): ResponseInterface
public function send(string $action)
{
Assert::isInstanceOf($this->server, Server::class);

Expand Down
16 changes: 4 additions & 12 deletions app/Repositories/Daemon/DaemonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use GuzzleHttp\Client;
use App\Models\Node;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
use Webmozart\Assert\Assert;
use App\Models\Server;
use Illuminate\Contracts\Foundation\Application;
Expand Down Expand Up @@ -48,20 +50,10 @@ public function setNode(Node $node): static
/**
* 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);

return new Client([
'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',
]),
]);
return Http::daemon($this->node, $headers);
}
}
3 changes: 2 additions & 1 deletion app/Repositories/Daemon/DaemonServerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public function getDetails(): array
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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Subuser;
use App\Models\Permission;
use App\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
use Illuminate\Support\Facades\Http;

class UpdateSubuserTest extends ClientApiIntegrationTestCase
{
Expand All @@ -17,6 +18,8 @@ public function testCorrectPermissionsAreRequiredForUpdating(): void
{
[$user, $server] = $this->generateTestAccount(['user.read']);

Http::fake();

$subuser = Subuser::factory()
->for(User::factory()->create())
->for($server)
Expand Down Expand Up @@ -65,6 +68,8 @@ public function testPermissionsAreSavedToAccount(): void
'permissions' => ['control.restart', 'websocket.connect', 'foo.bar'],
]);

Http::fake();

$this->actingAs($user)
->postJson("/api/client/servers/$server->uuid/users/{$subuser->user->uuid}", [
'permissions' => [
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Jobs/Schedule/RunTaskJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function testJobIsExecuted(bool $isManualRun): void
$mock->expects('setServer')->with(\Mockery::on(function ($value) use ($server) {
return $value instanceof Server && $value->id === $server->id;
}))->andReturnSelf();
$mock->expects('send')->with('start')->andReturn(new Response());
$mock->expects('send')->with('start');

Bus::dispatchSync(new RunTaskJob($task, $isManualRun));

Expand Down

0 comments on commit 358ac96

Please sign in to comment.