66use  Coderflex \LaravelSendy \Resources \Resources \Campaigns ;
77use  Coderflex \LaravelSendy \Resources \Resources \Lists ;
88use  Coderflex \LaravelSendy \Resources \Resources \Subscribers ;
9+ use  Exception ;
10+ use  GuzzleHttp \Client ;
11+ use  GuzzleHttp \Exception \ClientException ;
912
1013class  LaravelSendy
1114{
15+     protected  string  $ apiKey
16+ 
17+     protected  string  $ apiUrl
18+ 
19+     public  function  __construct ()
20+     {
21+         if  (blank (config ('laravel-sendy.api_key ' ))) {
22+             throw  new  Exception ('API Key is not set in the config file. ' );
23+         }
24+ 
25+         if  (blank (config ('laravel-sendy.api_url ' ))) {
26+             throw  new  Exception ('API URL is not set in the config file. ' );
27+         }
28+ 
29+         $ this apiKey  = config ('laravel-sendy.api_key ' );
30+         $ this apiUrl  = config ('laravel-sendy.api_url ' );
31+     }
32+ 
1233    public  function  subscribers (): Subscribers 
1334    {
1435        return  new  Subscribers ;
@@ -28,4 +49,65 @@ public function campaigns(): Campaigns
2849    {
2950        return  new  Campaigns ;
3051    }
52+ 
53+     public  function  __call (string  $ functionarray  $ args
54+     {
55+         $ options'get ' , 'post ' , 'put ' , 'delete ' , 'patch ' ];
56+         $ pathisset ($ args0 ])) ? $ args0 ] : null ;
57+         $ dataisset ($ args1 ])) ? $ args1 ] : [];
58+         $ headersisset ($ args2 ])) ? $ args2 ] : [];
59+ 
60+         if  (! in_array ($ function$ options
61+             throw  new  Exception ("Method  {$ function not found. " );
62+         }
63+ 
64+         return  self ::guzzle (
65+             type: $ function
66+             request: $ path
67+             data: $ data
68+             headers: $ headers
69+         );
70+     }
71+ 
72+     /** 
73+      * @throws \Exception 
74+      */ 
75+     protected  function  guzzle (string  $ typestring  $ requestarray  $ dataarray  $ headersmixed 
76+     {
77+         try  {
78+             $ clientnew  Client ;
79+ 
80+             $ mainHeaders
81+                 'Content-Type '  => 'application/json ' ,
82+                 'Accept '  => 'application/json ' ,
83+                 'Authorization '  => 'Bearer  ' .$ this apiKey ,
84+             ];
85+ 
86+             $ headersis_array ($ headerscount ($ headers0 
87+                 ? array_merge ($ mainHeaders$ headers
88+                 : $ mainHeaders
89+ 
90+             $ response$ client$ type$ this apiUrl .$ request
91+                 'headers '  => $ headers
92+                 'body '  => json_encode ($ data
93+             ]);
94+ 
95+             $ responseObject$ responsegetBody ()->getContents ();
96+ 
97+             return  $ this isJson ($ responseObject
98+                 ? json_decode ($ responseObjecttrue )
99+                 : $ responseObject
100+ 
101+         } catch  (ClientException $ th
102+             throw  new  Exception ('Error:  ' .$ thgetMessage ());
103+         } catch  (Exception $ th
104+             throw  new  Exception ('Error:  ' .$ thgetMessage ());
105+         }
106+     }
107+ 
108+     protected  function  isJson (string  $ stringbool 
109+     {
110+         return  is_array (json_decode ($ string
111+             (json_last_error () === JSON_ERROR_NONE );
112+     }
31113}
0 commit comments