@@ -30,38 +30,33 @@ class Client
3030 protected $ path ;
3131 /** @var array */
3232 protected $ curlOptions ;
33- /** @var array */
34- private $ methods ;
3533 /** @var bool */
36- private $ retryOnLimit ;
34+ protected $ retryOnLimit ;
35+
36+ /**
37+ * These are the supported HTTP verbs
38+ *
39+ * @var array
40+ */
41+ private $ methods = ['delete ' , 'get ' , 'patch ' , 'post ' , 'put ' ];
3742
3843 /**
3944 * Initialize the client
4045 *
41- * @param string $host the base url (e.g. https://api.sendgrid.com)
42- * @param array $headers global request headers
43- * @param string $version api version (configurable)
44- * @param array $path holds the segments of the url path
45- * @param array $curlOptions extra options to set during curl initialization
46- * @param bool $retryOnLimit set default retry on limit flag
46+ * @param string $host the base url (e.g. https://api.sendgrid.com)
47+ * @param array $headers global request headers
48+ * @param string $version api version (configurable)
49+ * @param array $path holds the segments of the url path
4750 */
48- public function __construct (
49- $ host ,
50- $ headers = null ,
51- $ version = null ,
52- $ path = null ,
53- $ curlOptions = null ,
54- $ retryOnLimit = false
55- ) {
51+ public function __construct ($ host , $ headers = [], $ version = null , $ path = [])
52+ {
5653 $ this ->host = $ host ;
57- $ this ->headers = $ headers ?: [] ;
54+ $ this ->headers = $ headers ;
5855 $ this ->version = $ version ;
59- $ this ->path = $ path ?: [];
60- $ this ->curlOptions = $ curlOptions ?: [];
61- // These are the supported HTTP verbs
62- $ this ->methods = ['delete ' , 'get ' , 'patch ' , 'post ' , 'put ' ];
56+ $ this ->path = $ path ;
6357
64- $ this ->retryOnLimit = $ retryOnLimit ;
58+ $ this ->curlOptions = [];
59+ $ this ->retryOnLimit = false ;
6560 }
6661
6762 /**
@@ -96,6 +91,34 @@ public function getPath()
9691 return $ this ->path ;
9792 }
9893
94+ /**
95+ * Set extra options to set during curl initialization
96+ *
97+ * @param array $options
98+ *
99+ * @return Client
100+ */
101+ public function setCurlOptions (array $ options )
102+ {
103+ $ this ->curlOptions = $ options ;
104+
105+ return $ this ;
106+ }
107+
108+ /**
109+ * Set default retry on limit flag
110+ *
111+ * @param bool $retry
112+ *
113+ * @return Client
114+ */
115+ public function setRetryOnLimit ($ retry )
116+ {
117+ $ this ->retryOnLimit = $ retry ;
118+
119+ return $ this ;
120+ }
121+
99122 /**
100123 * @return array
101124 */
@@ -116,11 +139,25 @@ private function buildClient($name = null)
116139 if (isset ($ name )) {
117140 $ this ->path [] = $ name ;
118141 }
119- $ client = new Client ( $ this ->host , $ this -> headers , $ this -> version , $ this -> path , $ this -> curlOptions );
142+ $ client = $ this ->cloneClient ( );
120143 $ this ->path = [];
121144 return $ client ;
122145 }
123146
147+ /**
148+ * Clone existing Client object with all settings
149+ *
150+ * @return Client
151+ */
152+ private function cloneClient ()
153+ {
154+ $ client = new static ($ this ->host , $ this ->headers , $ this ->version , $ this ->path );
155+ $ client ->setCurlOptions ($ this ->curlOptions );
156+ $ client ->setRetryOnLimit ($ this ->retryOnLimit );
157+
158+ return $ client ;
159+ }
160+
124161 /**
125162 * Build the final URL to be passed
126163 *
@@ -153,13 +190,18 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
153190 {
154191 $ curl = curl_init ($ url );
155192
156- curl_setopt_array ($ curl , [
157- CURLOPT_RETURNTRANSFER => true ,
158- CURLOPT_HEADER => 1 ,
159- CURLOPT_CUSTOMREQUEST => strtoupper ($ method ),
160- CURLOPT_SSL_VERIFYPEER => false ,
161- CURLOPT_FAILONERROR => false ,
162- ] + $ this ->curlOptions );
193+ $ options = array_merge (
194+ [
195+ CURLOPT_RETURNTRANSFER => true ,
196+ CURLOPT_HEADER => 1 ,
197+ CURLOPT_CUSTOMREQUEST => strtoupper ($ method ),
198+ CURLOPT_SSL_VERIFYPEER => false ,
199+ CURLOPT_FAILONERROR => false ,
200+ ],
201+ $ this ->curlOptions
202+ );
203+
204+ curl_setopt_array ($ curl , $ options );
163205
164206 if (isset ($ headers )) {
165207 $ this ->headers = array_merge ($ this ->headers , $ headers );
@@ -185,7 +227,7 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
185227
186228 $ response = new Response ($ statusCode , $ responseBody , $ responseHeaders );
187229
188- if ($ statusCode == 429 && $ retryOnLimit ) {
230+ if ($ statusCode === 429 && $ retryOnLimit ) {
189231 $ headers = $ response ->headers (true );
190232 $ sleepDurations = $ headers ['X-Ratelimit-Reset ' ] - time ();
191233 sleep ($ sleepDurations > 0 ? $ sleepDurations : 0 );
0 commit comments