33/**
44 * HTTP Client library
55 *
6- * PHP version 5.4
7- *
86 * @author Matt Bernier <dx@sendgrid.com>
97 * @author Elmer Thomas <dx@sendgrid.com>
108 * @copyright 2016 SendGrid
1614namespace SendGrid ;
1715
1816/**
19- * Quickly and easily access any REST or REST-like API.
20- */
17+ * Quickly and easily access any REST or REST-like API.
18+ *
19+ * @method Response get($body = null, $query = null, $headers = null)
20+ * @method Response post($body = null, $query = null, $headers = null)
21+ * @method Response patch($body = null, $query = null, $headers = null)
22+ * @method Response put($body = null, $query = null, $headers = null)
23+ * @method Response delete($body = null, $query = null, $headers = null)
24+ *
25+ * @method Client version($value)
26+ */
2127class Client
2228{
2329 /** @var string */
@@ -30,32 +36,33 @@ class Client
3036 protected $ path ;
3137 /** @var array */
3238 protected $ curlOptions ;
33- /** @var array */
34- private $ methods ;
3539 /** @var bool */
36- private $ retryOnLimit ;
40+ protected $ retryOnLimit ;
41+
42+ /**
43+ * These are the supported HTTP verbs
44+ *
45+ * @var array
46+ */
47+ private $ methods = ['get ' , 'post ' , 'patch ' , 'put ' , 'delete ' ];
3748
3849 /**
3950 * Initialize the client
4051 *
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
52+ * @param string $host the base url (e.g. https://api.sendgrid.com)
53+ * @param array $headers global request headers
54+ * @param string $version api version (configurable)
55+ * @param array $path holds the segments of the url path
4756 */
48- public function __construct ($ host , $ headers = null , $ version = null , $ path = null , $ curlOptions = null , $ retryOnLimit = false )
57+ public function __construct ($ host , $ headers = [] , $ version = ' /v3 ' , $ path = [] )
4958 {
5059 $ this ->host = $ host ;
51- $ this ->headers = $ headers ?: [] ;
60+ $ this ->headers = $ headers ;
5261 $ this ->version = $ version ;
53- $ this ->path = $ path ?: [];
54- $ this ->curlOptions = $ curlOptions ?: [];
55- // These are the supported HTTP verbs
56- $ this ->methods = ['delete ' , 'get ' , 'patch ' , 'post ' , 'put ' ];
62+ $ this ->path = $ path ;
5763
58- $ this ->retryOnLimit = $ retryOnLimit ;
64+ $ this ->curlOptions = [];
65+ $ this ->retryOnLimit = false ;
5966 }
6067
6168 /**
@@ -91,28 +98,39 @@ public function getPath()
9198 }
9299
93100 /**
94- * @return array
101+ * Set extra options to set during curl initialization
102+ *
103+ * @param array $options
104+ *
105+ * @return Client
95106 */
96- public function getCurlOptions ( )
107+ public function setCurlOptions ( array $ options )
97108 {
98- return $ this ->curlOptions ;
109+ $ this ->curlOptions = $ options ;
110+
111+ return $ this ;
99112 }
100113
101114 /**
102- * Make a new Client object
103- *
104- * @param string $name name of the url segment
105- *
106- * @return Client object
107- */
108- private function buildClient ( $ name = null )
115+ * Set default retry on limit flag
116+ *
117+ * @param bool $retry
118+ *
119+ * @return Client
120+ */
121+ public function setRetryOnLimit ( $ retry )
109122 {
110- if (isset ($ name )) {
111- $ this ->path [] = $ name ;
112- }
113- $ client = new Client ($ this ->host , $ this ->headers , $ this ->version , $ this ->path , $ this ->curlOptions );
114- $ this ->path = [];
115- return $ client ;
123+ $ this ->retryOnLimit = $ retry ;
124+
125+ return $ this ;
126+ }
127+
128+ /**
129+ * @return array
130+ */
131+ public function getCurlOptions ()
132+ {
133+ return $ this ->curlOptions ;
116134 }
117135
118136 /**
@@ -135,10 +153,10 @@ private function buildUrl($queryParams = null)
135153 * Make the API call and return the response. This is separated into
136154 * it's own function, so we can mock it easily for testing.
137155 *
138- * @param string $method the HTTP verb
139- * @param string $url the final url to call
140- * @param array $body request body
141- * @param array $headers any additional request headers
156+ * @param string $method the HTTP verb
157+ * @param string $url the final url to call
158+ * @param array $body request body
159+ * @param array $headers any additional request headers
142160 * @param bool $retryOnLimit should retry if rate limit is reach?
143161 *
144162 * @return Response object
@@ -147,13 +165,18 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
147165 {
148166 $ curl = curl_init ($ url );
149167
150- curl_setopt_array ($ curl , [
151- CURLOPT_RETURNTRANSFER => true ,
152- CURLOPT_HEADER => 1 ,
153- CURLOPT_CUSTOMREQUEST => strtoupper ($ method ),
154- CURLOPT_SSL_VERIFYPEER => false ,
155- CURLOPT_FAILONERROR => false ,
156- ] + $ this ->curlOptions );
168+ $ options = array_merge (
169+ [
170+ CURLOPT_RETURNTRANSFER => true ,
171+ CURLOPT_HEADER => 1 ,
172+ CURLOPT_CUSTOMREQUEST => strtoupper ($ method ),
173+ CURLOPT_SSL_VERIFYPEER => false ,
174+ CURLOPT_FAILONERROR => false ,
175+ ],
176+ $ this ->curlOptions
177+ );
178+
179+ curl_setopt_array ($ curl , $ options );
157180
158181 if (isset ($ headers )) {
159182 $ this ->headers = array_merge ($ this ->headers , $ headers );
@@ -179,7 +202,7 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
179202
180203 $ response = new Response ($ statusCode , $ responseBody , $ responseHeaders );
181204
182- if ($ statusCode == 429 && $ retryOnLimit ) {
205+ if ($ statusCode === 429 && $ retryOnLimit ) {
183206 $ headers = $ response ->headers (true );
184207 $ sleepDurations = $ headers ['X-Ratelimit-Reset ' ] - time ();
185208 sleep ($ sleepDurations > 0 ? $ sleepDurations : 0 );
@@ -201,7 +224,15 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
201224 */
202225 public function _ ($ name = null )
203226 {
204- return $ this ->buildClient ($ name );
227+ if (isset ($ name )) {
228+ $ this ->path [] = $ name ;
229+ }
230+ $ client = new static ($ this ->host , $ this ->headers , $ this ->version , $ this ->path );
231+ $ client ->setCurlOptions ($ this ->curlOptions );
232+ $ client ->setRetryOnLimit ($ this ->retryOnLimit );
233+ $ this ->path = [];
234+
235+ return $ client ;
205236 }
206237
207238 /**
0 commit comments