22
33namespace Coderflex \LaravelSendy ;
44
5- use Coderflex \LaravelSendy \Resources \Resources \Brands ;
6- use Coderflex \LaravelSendy \Resources \Resources \Campaigns ;
7- use Coderflex \LaravelSendy \Resources \Resources \Lists ;
8- use Coderflex \LaravelSendy \Resources \Resources \Subscribers ;
95use Exception ;
10- use GuzzleHttp \Client ;
11- use GuzzleHttp \Exception \ClientException ;
6+ use Illuminate \Support \Facades \Http ;
127
138class LaravelSendy
149{
@@ -30,77 +25,75 @@ public function __construct()
3025 $ this ->apiUrl = config ('laravel-sendy.api_url ' );
3126 }
3227
33- public function subscribers (): Subscribers
28+ public function subscribers (): Resources \ Subscribers
3429 {
35- return new Subscribers ;
30+ return new Resources \ Subscribers ;
3631 }
3732
38- public function lists (): Lists
33+ public function lists (): Resources \ Lists
3934 {
40- return new Lists ;
35+ return new Resources \ Lists ;
4136 }
4237
43- public function brands (): Brands
38+ public function brands (): Resources \ Brands
4439 {
45- return new Brands ;
40+ return new Resources \ Brands ;
4641 }
4742
48- public function campaigns (): Campaigns
43+ public function campaigns (): Resources \ Campaigns
4944 {
50- return new Campaigns ;
45+ return new Resources \ Campaigns ;
5146 }
5247
53- public function __call (string $ function , array $ args )
48+ public function __call (string $ function , array $ args ): mixed
5449 {
5550 $ options = ['get ' , 'post ' , 'put ' , 'delete ' , 'patch ' ];
56- $ path = (isset ($ args [0 ])) ? $ args [0 ] : null ;
57- $ data = (isset ($ args [1 ])) ? $ args [1 ] : [];
58- $ headers = (isset ($ args [2 ])) ? $ args [2 ] : [];
51+ $ path = $ args [0 ] ?? null ;
52+ $ data = $ args [1 ] ?? [];
53+ $ async = $ args [2 ] ?? false ;
54+ $ headers = $ args [3 ] ?? [];
5955
6056 if (! in_array ($ function , $ options )) {
6157 throw new Exception ("Method {$ function } not found. " );
6258 }
6359
64- return self ::guzzle (
60+ return self ::sendRequest (
6561 type: $ function ,
6662 request: $ path ,
6763 data: $ data ,
68- headers: $ headers
64+ headers: $ headers ,
65+ async: $ async
6966 );
7067 }
7168
7269 /**
7370 * @throws \Exception
7471 */
75- protected function guzzle (string $ type , string $ request , array $ data = [], array $ headers = []): mixed
76- {
72+ protected function sendRequest (
73+ string $ type ,
74+ string $ request ,
75+ array $ data = [],
76+ array $ headers = [],
77+ bool $ async = false
78+ ): mixed {
7779 try {
78- $ client = new Client ;
79-
80- $ mainHeaders = [
80+ $ mainHeaders = array_merge ([
8181 'Content-Type ' => 'application/json ' ,
8282 'Accept ' => 'application/json ' ,
83- ];
84-
85- $ headers = is_array ($ headers ) && count ($ headers ) > 0
86- ? array_merge ($ mainHeaders , $ headers )
87- : $ mainHeaders ;
83+ ], $ headers ?? []);
8884
89- $ response = $ client ->{$ type }($ this ->apiUrl .$ request , [
90- 'headers ' => $ headers ,
91- 'body ' => json_encode (array_merge ($ data , [
92- 'api_key ' => $ this ->apiKey ,
93- ])),
85+ $ payload = array_merge ($ data , [
86+ 'api_key ' => $ this ->apiKey ,
9487 ]);
9588
96- $ responseObject = $ response -> getBody ()-> getContents ( );
89+ $ url = str_replace ( ' // ' , ' / ' , " $ this -> apiUrl / $ request " );
9790
98- return $ this ->isJson ($ responseObject )
99- ? json_decode ($ responseObject , true )
100- : $ responseObject ;
91+ $ client = Http::withHeaders ($ headers );
92+
93+ return $ async
94+ ? $ client ->async ()->{$ type }($ url , $ payload )
95+ : $ client ->{$ type }($ url , $ payload );
10196
102- } catch (ClientException $ th ) {
103- throw new Exception ('Error: ' .$ th ->getMessage ());
10497 } catch (Exception $ th ) {
10598 throw new Exception ('Error: ' .$ th ->getMessage ());
10699 }
0 commit comments