3
3
/**
4
4
* HTTP Client library
5
5
*
6
- * PHP version 5.4
7
- *
8
6
* @author Matt Bernier <[email protected] >
9
7
* @author Elmer Thomas <[email protected] >
10
8
* @copyright 2016 SendGrid
16
14
namespace SendGrid ;
17
15
18
16
/**
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
+ */
21
27
class Client
22
28
{
23
29
/** @var string */
@@ -30,32 +36,33 @@ class Client
30
36
protected $ path ;
31
37
/** @var array */
32
38
protected $ curlOptions ;
33
- /** @var array */
34
- private $ methods ;
35
39
/** @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 ' ];
37
48
38
49
/**
39
50
* Initialize the client
40
51
*
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
47
56
*/
48
- public function __construct ($ host , $ headers = null , $ version = null , $ path = null , $ curlOptions = null , $ retryOnLimit = false )
57
+ public function __construct ($ host , $ headers = [] , $ version = ' /v3 ' , $ path = [] )
49
58
{
50
59
$ this ->host = $ host ;
51
- $ this ->headers = $ headers ?: [] ;
60
+ $ this ->headers = $ headers ;
52
61
$ 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 ;
57
63
58
- $ this ->retryOnLimit = $ retryOnLimit ;
64
+ $ this ->curlOptions = [];
65
+ $ this ->retryOnLimit = false ;
59
66
}
60
67
61
68
/**
@@ -91,28 +98,39 @@ public function getPath()
91
98
}
92
99
93
100
/**
94
- * @return array
101
+ * Set extra options to set during curl initialization
102
+ *
103
+ * @param array $options
104
+ *
105
+ * @return Client
95
106
*/
96
- public function getCurlOptions ( )
107
+ public function setCurlOptions ( array $ options )
97
108
{
98
- return $ this ->curlOptions ;
109
+ $ this ->curlOptions = $ options ;
110
+
111
+ return $ this ;
99
112
}
100
113
101
114
/**
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 )
109
122
{
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 ;
116
134
}
117
135
118
136
/**
@@ -135,10 +153,10 @@ private function buildUrl($queryParams = null)
135
153
* Make the API call and return the response. This is separated into
136
154
* it's own function, so we can mock it easily for testing.
137
155
*
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
142
160
* @param bool $retryOnLimit should retry if rate limit is reach?
143
161
*
144
162
* @return Response object
@@ -147,13 +165,18 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
147
165
{
148
166
$ curl = curl_init ($ url );
149
167
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 );
157
180
158
181
if (isset ($ headers )) {
159
182
$ this ->headers = array_merge ($ this ->headers , $ headers );
@@ -179,7 +202,7 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
179
202
180
203
$ response = new Response ($ statusCode , $ responseBody , $ responseHeaders );
181
204
182
- if ($ statusCode == 429 && $ retryOnLimit ) {
205
+ if ($ statusCode === 429 && $ retryOnLimit ) {
183
206
$ headers = $ response ->headers (true );
184
207
$ sleepDurations = $ headers ['X-Ratelimit-Reset ' ] - time ();
185
208
sleep ($ sleepDurations > 0 ? $ sleepDurations : 0 );
@@ -201,7 +224,15 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
201
224
*/
202
225
public function _ ($ name = null )
203
226
{
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 ;
205
236
}
206
237
207
238
/**
0 commit comments