2
2
3
3
namespace Coderflex \LaravelSendy ;
4
4
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 ;
9
5
use Exception ;
10
- use GuzzleHttp \Client ;
11
- use GuzzleHttp \Exception \ClientException ;
6
+ use Illuminate \Support \Facades \Http ;
12
7
13
8
class LaravelSendy
14
9
{
@@ -30,77 +25,75 @@ public function __construct()
30
25
$ this ->apiUrl = config ('laravel-sendy.api_url ' );
31
26
}
32
27
33
- public function subscribers (): Subscribers
28
+ public function subscribers (): Resources \ Subscribers
34
29
{
35
- return new Subscribers ;
30
+ return new Resources \ Subscribers ;
36
31
}
37
32
38
- public function lists (): Lists
33
+ public function lists (): Resources \ Lists
39
34
{
40
- return new Lists ;
35
+ return new Resources \ Lists ;
41
36
}
42
37
43
- public function brands (): Brands
38
+ public function brands (): Resources \ Brands
44
39
{
45
- return new Brands ;
40
+ return new Resources \ Brands ;
46
41
}
47
42
48
- public function campaigns (): Campaigns
43
+ public function campaigns (): Resources \ Campaigns
49
44
{
50
- return new Campaigns ;
45
+ return new Resources \ Campaigns ;
51
46
}
52
47
53
- public function __call (string $ function , array $ args )
48
+ public function __call (string $ function , array $ args ): mixed
54
49
{
55
50
$ 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 ] ?? [];
59
55
60
56
if (! in_array ($ function , $ options )) {
61
57
throw new Exception ("Method {$ function } not found. " );
62
58
}
63
59
64
- return self ::guzzle (
60
+ return self ::sendRequest (
65
61
type: $ function ,
66
62
request: $ path ,
67
63
data: $ data ,
68
- headers: $ headers
64
+ headers: $ headers ,
65
+ async: $ async
69
66
);
70
67
}
71
68
72
69
/**
73
70
* @throws \Exception
74
71
*/
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 {
77
79
try {
78
- $ client = new Client ;
79
-
80
- $ mainHeaders = [
80
+ $ mainHeaders = array_merge ([
81
81
'Content-Type ' => 'application/json ' ,
82
82
'Accept ' => 'application/json ' ,
83
- ];
84
-
85
- $ headers = is_array ($ headers ) && count ($ headers ) > 0
86
- ? array_merge ($ mainHeaders , $ headers )
87
- : $ mainHeaders ;
83
+ ], $ headers ?? []);
88
84
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 ,
94
87
]);
95
88
96
- $ responseObject = $ response -> getBody ()-> getContents ( );
89
+ $ url = str_replace ( ' // ' , ' / ' , " $ this -> apiUrl / $ request " );
97
90
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 );
101
96
102
- } catch (ClientException $ th ) {
103
- throw new Exception ('Error: ' .$ th ->getMessage ());
104
97
} catch (Exception $ th ) {
105
98
throw new Exception ('Error: ' .$ th ->getMessage ());
106
99
}
0 commit comments