@@ -30,7 +30,7 @@ public function checkHealth(): HealthCheckResponse
3030 return HealthCheckResponse::from ($ healthData ['data ' ]['attributes ' ]);
3131 }
3232
33- public function performExtractionRequest (array $ data ): array
33+ public function performExtractionRequest (array $ requestData ): array
3434 {
3535 $ request = new Request (
3636 'POST ' ,
@@ -39,19 +39,24 @@ public function performExtractionRequest(array $data): array
3939
4040 $ data ['password ' ] = $ this ->key ;
4141
42- return $ this ->performHttpRequest (
42+ // Get result
43+ $ response = $ this ->performHttpRequest (
4344 $ request ,
4445 [
45- 'form_params ' => $ data ,
46+ 'form_params ' => $ requestData ,
4647 'timeout ' => 300.0
4748 ]
4849 );
50+
51+ $ responseData = $ this ->decodeResponse ($ response , $ request );
52+
53+ return $ responseData ;
4954 }
5055
5156 protected function performWebserviceRequest (
5257 HttpMethod $ method ,
5358 WebserviceEndpoint $ endpoint ,
54- array $ data = []
59+ array $ requestData = []
5560 ): array {
5661 $ request = new Request (
5762 $ method ->name ,
@@ -61,18 +66,32 @@ protected function performWebserviceRequest(
6166 ]
6267 );
6368
64- return $ this ->performHttpRequest (
69+ // Get result
70+ $ response = $ this ->performHttpRequest (
6571 $ request ,
6672 [
67- "json " => $ data
73+ "json " => $ requestData
6874 ]
6975 );
76+
77+ $ responseData = $ this ->decodeResponse ($ response , $ request );
78+
79+ // Make sure it matches the Joomla webservice response format
80+ if (empty ($ responseData ['data ' ]['attributes ' ])) {
81+ throw new RequestException (
82+ "Invalid JSON format " ,
83+ $ request ,
84+ $ response
85+ );
86+ }
87+
88+ return $ responseData ;
7089 }
7190
7291 protected function performHttpRequest (
7392 RequestInterface $ request ,
7493 array $ options = []
75- ): array {
94+ ): Response {
7695 /** @var Client $httpClient */
7796 $ httpClient = App::make (Client::class);
7897
@@ -91,23 +110,28 @@ protected function performHttpRequest(
91110 );
92111 }
93112
94- // Decode body
95- $ return = json_decode (
113+ return $ response ;
114+ }
115+
116+ protected function decodeResponse (Response $ response , Request $ request ): array
117+ {
118+ // Decode
119+ $ data = json_decode (
96120 (string ) $ response ->getBody (),
97121 true ,
98122 512 ,
99123 JSON_THROW_ON_ERROR
100124 );
101125
102126 // Make sure it's an array
103- if (!is_array ($ return )) {
127+ if (!is_array ($ data )) {
104128 throw new RequestException (
105129 "Invalid JSON body " ,
106130 $ request ,
107131 $ response
108132 );
109133 }
110134
111- return $ return ;
135+ return $ data ;
112136 }
113137}
0 commit comments