44namespace Espo \ApiClient ;
55
66use CurlHandle ;
7+ use Espo \ApiClient \Exception \Error ;
8+ use Espo \ApiClient \Exception \ResponseError ;
79use InvalidArgumentException ;
810use JsonException ;
911use RuntimeException ;
@@ -71,15 +73,16 @@ public function setSecretKey(?string $secretKey): self
7173 * @param string $path A relative URL path. E.g. `Account/00000000000id`.
7274 * @param array<int, mixed>|array<string, mixed>|stdClass|null $data Payload data.
7375 * @param Header[] $headers Headers.
74- * @return stdClass|array<int, mixed>|string
75- * @throws Exception
76+ * @return Response A response (on success).
77+ * @throws Error
78+ * @throws ResponseError On error occurred on request.
7679 */
7780 public function request (
7881 string $ method ,
7982 string $ path ,
8083 mixed $ data = null ,
8184 array $ headers = []
82- ): stdClass | array | string {
85+ ): Response {
8386
8487 $ method = strtoupper ($ method );
8588 $ this ->lastCh = null ;
@@ -121,7 +124,7 @@ public function request(
121124 curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , true );
122125 curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , true );
123126 curl_setopt ($ ch , CURLOPT_HEADER , true );
124-
127+
125128 if ($ this ->port !== null ) {
126129 curl_setopt ($ ch , CURLOPT_PORT , $ this ->port );
127130 }
@@ -194,7 +197,7 @@ public function request(
194197 $ lastResponse = curl_exec ($ ch );
195198
196199 if ($ lastResponse === false ) {
197- throw new Exception ('CURL exec failure. ' , 0 );
200+ throw new Error ('CURL exec failure. ' , 0 );
198201 }
199202
200203 $ this ->lastCh = $ ch ;
@@ -203,47 +206,49 @@ public function request(
203206 $ responseCode = $ this ->getResponseHttpCode ();
204207 $ responseContentType = $ this ->getResponseContentType ();
205208
209+ $ response = new Response (
210+ $ responseCode ?? 0 ,
211+ $ responseContentType ,
212+ $ parsedResponse ['header ' ],
213+ $ parsedResponse ['body ' ],
214+ );
215+
206216 curl_close ($ ch );
207217
208218 if (
209- ($ responseCode !== null && $ responseCode >= 200 && $ responseCode < 300 ) &&
210- !empty ($ parsedResponse ['body ' ])
219+ $ responseCode !== null &&
220+ $ responseCode >= 200 &&
221+ $ responseCode < 300
211222 ) {
212- if ($ responseContentType === 'application/json ' ) {
213- return json_decode ($ parsedResponse ['body ' ]);
214- }
215-
216- return $ parsedResponse ['body ' ];
223+ return $ response ;
217224 }
218225
219226 $ responseHeaders = $ this ->normalizeHeader ($ parsedResponse ['header ' ]);
227+ $ errorMessage = $ responseHeaders ['X-Status-Reason ' ] ?? '' ;
220228
221- $ errorMessage = $ responseHeaders ['X-Status-Reason ' ] ?? 'Unknown Error ' ;
222-
223- throw (new Exception ($ errorMessage , $ responseCode ?? 0 ))
224- ->withBody ($ parsedResponse ['body ' ]);
229+ throw new ResponseError ($ response , $ errorMessage , $ responseCode ?? 0 );
225230 }
226231
227232 /**
228233 * Get a response content type.
229234 */
230- public function getResponseContentType (): ?string
235+ private function getResponseContentType (): ?string
231236 {
232237 return $ this ->getInfo (CURLINFO_CONTENT_TYPE );
233238 }
234239
235240 /**
236241 * Get a response total time.
237242 */
238- public function getResponseTotalTime (): ?int
243+ private function getResponseTotalTime (): ?int
239244 {
240245 return $ this ->getInfo (CURLINFO_TOTAL_TIME );
241246 }
242247
243248 /**
244249 * Get a response code.
245250 */
246- public function getResponseHttpCode (): ?int
251+ private function getResponseHttpCode (): ?int
247252 {
248253 return $ this ->getInfo (CURLINFO_HTTP_CODE );
249254 }
0 commit comments