@@ -30,38 +30,33 @@ class Client
30
30
protected $ path ;
31
31
/** @var array */
32
32
protected $ curlOptions ;
33
- /** @var array */
34
- private $ methods ;
35
33
/** @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 ' ];
37
42
38
43
/**
39
44
* Initialize the client
40
45
*
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
47
50
*/
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
+ {
56
53
$ this ->host = $ host ;
57
- $ this ->headers = $ headers ?: [] ;
54
+ $ this ->headers = $ headers ;
58
55
$ 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 ;
63
57
64
- $ this ->retryOnLimit = $ retryOnLimit ;
58
+ $ this ->curlOptions = [];
59
+ $ this ->retryOnLimit = false ;
65
60
}
66
61
67
62
/**
@@ -96,6 +91,34 @@ public function getPath()
96
91
return $ this ->path ;
97
92
}
98
93
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
+
99
122
/**
100
123
* @return array
101
124
*/
@@ -116,11 +139,25 @@ private function buildClient($name = null)
116
139
if (isset ($ name )) {
117
140
$ this ->path [] = $ name ;
118
141
}
119
- $ client = new Client ( $ this ->host , $ this -> headers , $ this -> version , $ this -> path , $ this -> curlOptions );
142
+ $ client = $ this ->cloneClient ( );
120
143
$ this ->path = [];
121
144
return $ client ;
122
145
}
123
146
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
+
124
161
/**
125
162
* Build the final URL to be passed
126
163
*
@@ -153,13 +190,18 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
153
190
{
154
191
$ curl = curl_init ($ url );
155
192
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 );
163
205
164
206
if (isset ($ headers )) {
165
207
$ this ->headers = array_merge ($ this ->headers , $ headers );
@@ -185,7 +227,7 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
185
227
186
228
$ response = new Response ($ statusCode , $ responseBody , $ responseHeaders );
187
229
188
- if ($ statusCode == 429 && $ retryOnLimit ) {
230
+ if ($ statusCode === 429 && $ retryOnLimit ) {
189
231
$ headers = $ response ->headers (true );
190
232
$ sleepDurations = $ headers ['X-Ratelimit-Reset ' ] - time ();
191
233
sleep ($ sleepDurations > 0 ? $ sleepDurations : 0 );
0 commit comments