Skip to content

Commit 62dc5a1

Browse files
committed
chore: add result validation
1 parent a1cd0f4 commit 62dc5a1

File tree

4 files changed

+80
-24
lines changed

4 files changed

+80
-24
lines changed

src/Api.php

+64-22
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,117 @@
44

55
use Illuminate\Http\Client\ConnectionException;
66
use Illuminate\Http\Client\Response;
7+
use Illuminate\Support\Collection;
78
use Illuminate\Support\Facades\Http;
89
use JsonException;
910
use Sensson\DirectAdmin\Exceptions\ConnectionFailed;
11+
use Sensson\DirectAdmin\Exceptions\InvalidResponse;
1012

1113
class Api
1214
{
1315
public bool $debug = false;
1416

15-
public bool $json = true;
16-
1717
/**
18+
* Call the DirectAdmin API by giving it an API command and some
19+
* parameters. This will return an array with processed data.
20+
*
1821
* @throws ConnectionFailed
22+
* @throws InvalidResponse
1923
*/
20-
public function call(string $command, array $params = []): array
24+
public function call(string $command, array $params = []): Collection
2125
{
22-
$queryParams = $this->json
23-
? ['json' => true]
24-
: [];
25-
2626
try {
2727
$response = Http::acceptJson()
2828
->withBasicAuth(config('directadmin.username'), config('directadmin.password'))
29-
->withQueryParameters($queryParams)
29+
->withOptions($this->getHttpOptions())
30+
->withQueryParameters($this->getQueryParams())
3031
->post(config('directadmin.baseUrl').'/'.strtoupper($command), $params);
3132
} catch (ConnectionException $e) {
3233
throw new ConnectionFailed('Connection failed: '.$e->getMessage());
3334
}
3435

35-
return $this->process($response);
36+
return $this->processResponse($response);
3637
}
3738

38-
public function withoutJson(): static
39+
/**
40+
* Enable debug mode for HTTP requests. This can help identify
41+
* issues with the DirectAdmin server.
42+
*/
43+
public function debug(): static
3944
{
40-
$this->json = false;
45+
$this->debug = true;
4146

4247
return $this;
4348
}
4449

4550
/**
51+
* Process the response that's returned by the DirectAdmin API
52+
* and prepare it for further processing by third party code.
53+
*
4654
* @throws ConnectionFailed
55+
* @throws InvalidResponse
4756
*/
48-
protected function process(Response $response): array
57+
protected function processResponse(Response $response): Collection
4958
{
50-
if (! $response->successful()) {
59+
if (! $response->successful() && $response->status() !== 500) {
5160
match ($response->status()) {
5261
401 => throw new ConnectionFailed('Unauthorized. Please check the credentials.'),
5362
403 => throw new ConnectionFailed('Unauthorized. You do not have access to this resource.'),
5463
405 => throw new ConnectionFailed('Command does not exist.'),
55-
default => throw new ConnectionFailed('Something went wrong.'),
64+
default => throw new ConnectionFailed('Something went wrong: '.$response->body()),
5665
};
5766
}
5867

5968
try {
60-
$content = json_decode($response->body(), associative: true, flags: JSON_THROW_ON_ERROR);
69+
$result = json_decode($response->body(), associative: true, flags: JSON_THROW_ON_ERROR);
70+
$result = collect($result);
6171
} catch (JsonException) {
62-
$content = collect(explode('&', urldecode($response->body())))
63-
->map(function ($item) {
64-
[$value, $key] = explode('=', $item);
72+
throw new InvalidResponse('Invalid JSON returned by server: '.$response->body());
73+
}
74+
75+
$this->validate($result);
76+
77+
return $result;
78+
}
79+
80+
/**
81+
* Validate the content returned by DirectAdmin. Even though the
82+
* API can return a successfull response to our HTTP-request
83+
* the actual call may have failed.
84+
*
85+
* @throws InvalidResponse
86+
*/
87+
protected function validate(Collection $result): void
88+
{
89+
if ($result->has('error')) {
90+
$error = $result->get('error');
91+
$description = $result->get('result');
6592

66-
return [$key => $value];
67-
})
68-
->toArray();
93+
if (! empty($description)) {
94+
$description = ': '.$description;
95+
}
96+
97+
throw new InvalidResponse($error.$description);
6998
}
99+
}
70100

71-
return $content;
101+
protected function getHttpOptions(): array
102+
{
103+
return [
104+
'debug' => $this->debug,
105+
];
106+
}
107+
108+
protected function getQueryParams(): array
109+
{
110+
return [
111+
'json' => 'yes',
112+
];
72113
}
73114

74115
/**
75116
* @throws ConnectionFailed
117+
* @throws InvalidResponse
76118
*/
77119
public function __call(string $name, array $arguments)
78120
{

src/DirectAdmin.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Sensson\DirectAdmin;
44

5+
use Illuminate\Support\Collection;
56
use Illuminate\Support\Traits\ForwardsCalls;
67
use Illuminate\Support\Traits\Tappable;
78
use Sensson\DirectAdmin\Exceptions\ConnectionFailed;
9+
use Sensson\DirectAdmin\Exceptions\InvalidResponse;
810

911
class DirectAdmin
1012
{
@@ -18,8 +20,9 @@ public function __construct(public Api $api)
1820

1921
/**
2022
* @throws ConnectionFailed
23+
* @throws InvalidResponse
2124
*/
22-
public function call(string $command, array $params = []): array
25+
public function call(string $command, array $params = []): Collection
2326
{
2427
return $this->api->call($command, $params);
2528
}

src/Exceptions/InvalidResponse.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Sensson\DirectAdmin\Exceptions;
4+
5+
use Exception;
6+
7+
class InvalidResponse extends Exception
8+
{
9+
//
10+
}

src/Facades/DirectAdmin.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
/**
99
* @see BaseDirectAdmin
1010
*
11-
* @method static call(string $string)
11+
* @method static call(string $string, array $params = [])
12+
* @method static debug()
1213
*/
1314
class DirectAdmin extends Facade
1415
{

0 commit comments

Comments
 (0)