33namespace App \Services ;
44
55use App \Enum \HttpMethod ;
6+ use App \Enum \WebserviceEndpoint ;
67use GuzzleHttp \Client ;
78use GuzzleHttp \Exception \RequestException ;
89use 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