44
55use GuzzleHttp \Client ;
66use GuzzleHttp \Exception \GuzzleException ;
7+ use MusahMusah \LaravelMultipaymentGateways \Exceptions \HttpClientException ;
78use MusahMusah \LaravelMultipaymentGateways \Exceptions \HttpMethodFoundException ;
89
910trait ConsumesExternalServices
@@ -16,8 +17,7 @@ trait ConsumesExternalServices
1617 /**
1718 * Send a request to any service.
1819 *
19- *
20- * @throws GuzzleException|HttpMethodFoundException
20+ * @throws GuzzleException|HttpMethodFoundException|HttpClientException
2121 */
2222 public function makeRequest (string $ method , string $ requestUrl , array |string $ formParams = [], bool $ isJsonRequest = false , array $ queryParams = [], array $ headers = [], bool $ skipResolve = false ): mixed
2323 {
@@ -31,15 +31,19 @@ public function makeRequest(string $method, string $requestUrl, array|string $fo
3131 $ this ->resolveAuthorization ($ queryParams , $ formParams , $ headers );
3232 }
3333
34- $ response = $ client ->request ($ method , $ requestUrl , [
35- $ isJsonRequest ? 'json ' : 'form_params ' => $ formParams ,
36- 'headers ' => [
37- ...$ headers ,
38- 'Content-Type ' => $ isJsonRequest ? 'application/json ' : 'application/x-www-form-urlencoded ' ,
39- 'Accept ' => 'application/json ' ,
40- ],
41- 'query ' => $ queryParams ,
42- ]);
34+ try {
35+ $ response = $ client ->request ($ method , $ requestUrl , [
36+ $ isJsonRequest ? 'json ' : 'form_params ' => $ formParams ,
37+ 'headers ' => [
38+ ...$ headers ,
39+ 'Content-Type ' => $ isJsonRequest ? 'application/json ' : 'application/x-www-form-urlencoded ' ,
40+ 'Accept ' => 'application/json ' ,
41+ ],
42+ 'query ' => $ queryParams ,
43+ ]);
44+ } catch (GuzzleException $ e ) {
45+ throw new HttpClientException ($ e ->getMessage ());
46+ }
4347
4448 $ this ->response = $ response ->getBody ()->getContents ();
4549
@@ -55,7 +59,7 @@ public function makeRequest(string $method, string $requestUrl, array|string $fo
5559 */
5660 private function validateRequest (string $ method ): void
5761 {
58- if (! in_array ($ method , ['GET ' , 'POST ' , 'PUT ' , 'DELETE ' ])) {
62+ if (! in_array ($ method , ['GET ' , 'POST ' , 'PUT ' , 'PATCH ' , ' DELETE ' ])) {
5963 throw new HttpMethodFoundException ('Method not found ' );
6064 }
6165 }
0 commit comments