Skip to content

Commit 2dec37a

Browse files
committed
expose endpoint-specific connectoinhelper method
1 parent c73aea7 commit 2dec37a

File tree

3 files changed

+51
-40
lines changed

3 files changed

+51
-40
lines changed

app/Enum/WebserviceEndpoints.php renamed to app/Enum/WebserviceEndpoint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Enum;
44

5-
enum WebserviceEndpoints: string
5+
enum WebserviceEndpoint: string
66
{
77
case HEALTH_CHECK = "health.json";
88
}

app/Jobs/CheckSiteHealth.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,10 @@ public function handle(): void
2929
/** @var SiteConnectionService $connection */
3030
$connection = $this->site->connection;
3131

32-
$response = $connection->performWebserviceRequest(
33-
HttpMethod::GET,
34-
WebserviceEndpoints::HEALTH_CHECK->value
35-
);
32+
$response = $connection->checkHealth();
3633

3734
$healthData = collect($response);
3835

39-
// Perform a sanity check
40-
if (!$healthData->has('cms_version')) {
41-
throw new \Exception("Invalid health response content");
42-
}
43-
4436
// Write updated data to DB
4537
$this->site->update(
4638
$healthData->only([

app/Services/SiteConnectionService.php

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Services;
44

55
use App\Enum\HttpMethod;
6+
use App\Enum\WebserviceEndpoint;
67
use GuzzleHttp\Client;
78
use GuzzleHttp\Exception\RequestException;
89
use GuzzleHttp\Psr7\Request;
@@ -16,35 +17,19 @@ public function __construct(protected readonly string $baseUrl, protected readon
1617
{
1718
}
1819

19-
protected function performRequest(
20-
RequestInterface $request,
21-
array $options = []
22-
): array {
23-
/** @var Client $httpClient */
24-
$httpClient = App::make(Client::class);
25-
26-
/** @var Response $response */
27-
$response = $httpClient->send(
28-
$request,
29-
$options
20+
public function checkHealth(): array
21+
{
22+
$healthData = $this->performWebserviceRequest(
23+
HttpMethod::GET,
24+
WebserviceEndpoint::HEALTH_CHECK
3025
);
3126

32-
// Validate response
33-
if (!json_validate((string) $response->getBody())) {
34-
throw new RequestException(
35-
"Invalid JSON body",
36-
$request,
37-
$response
38-
);
27+
// Perform a sanity check
28+
if (empty($healthData['cms_version'])) {
29+
throw new \Exception("Invalid health response content");
3930
}
4031

41-
// Return decoded body
42-
return json_decode(
43-
(string) $response->getBody(),
44-
true,
45-
512,
46-
JSON_THROW_ON_ERROR
47-
);
32+
return $healthData;
4833
}
4934

5035
public function performExtractionRequest(array $data): array
@@ -56,7 +41,7 @@ public function performExtractionRequest(array $data): array
5641

5742
$data['password'] = $this->key;
5843

59-
return $this->performRequest(
44+
return $this->performHttpRequest(
6045
$request,
6146
[
6247
'form_params' => $data,
@@ -65,21 +50,55 @@ public function performExtractionRequest(array $data): array
6550
);
6651
}
6752

68-
public function performWebserviceRequest(HttpMethod $method, string $endpoint, array $data = []): array
69-
{
53+
protected function performWebserviceRequest(
54+
HttpMethod $method,
55+
WebserviceEndpoint $endpoint,
56+
array $data = []
57+
): array {
7058
$request = new Request(
7159
$method->name,
72-
$this->baseUrl . $endpoint,
60+
$this->baseUrl . $endpoint->value,
7361
[
7462
'Authorization' => 'JUpdate-Token ' . $this->key
7563
]
7664
);
7765

78-
return $this->performRequest(
66+
return $this->performHttpRequest(
7967
$request,
8068
[
8169
"json" => $data
8270
]
8371
);
8472
}
73+
74+
protected function performHttpRequest(
75+
RequestInterface $request,
76+
array $options = []
77+
): array {
78+
/** @var Client $httpClient */
79+
$httpClient = App::make(Client::class);
80+
81+
/** @var Response $response */
82+
$response = $httpClient->send(
83+
$request,
84+
$options
85+
);
86+
87+
// Validate response
88+
if (!json_validate((string) $response->getBody())) {
89+
throw new RequestException(
90+
"Invalid JSON body",
91+
$request,
92+
$response
93+
);
94+
}
95+
96+
// Return decoded body
97+
return json_decode(
98+
(string) $response->getBody(),
99+
true,
100+
512,
101+
JSON_THROW_ON_ERROR
102+
);
103+
}
85104
}

0 commit comments

Comments
 (0)