Skip to content

Commit 2222ae3

Browse files
author
pulkit
committed
added an addFile method to add files to the request
1 parent e6b435f commit 2222ae3

File tree

2 files changed

+98
-42
lines changed

2 files changed

+98
-42
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"require": {
1313
"php": ">=5.4.0",
1414
"guzzlehttp/guzzle": "5.0.*",
15-
"guzzlehttp/retry-subscriber": "2.0.*"
15+
"guzzlehttp/retry-subscriber": "2.0.*",
16+
"illuminate/support": "~4|~5"
1617
},
1718
"autoload": {
1819
"psr-4": {

src/Client.php

Lines changed: 96 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ class Client
1010
*/
1111
protected $guzzleClient;
1212

13+
/**
14+
* @var array
15+
*/
16+
protected $config = [];
17+
1318
/**
1419
* Url
1520
*
@@ -54,20 +59,33 @@ class Client
5459
protected $retry = 5;
5560

5661
/**
57-
* @param \GuzzleHttp\Client $guzzleClient
62+
* @param \GuzzleHttp\Client $guzzleClient
63+
* @param array $config
5864
*/
59-
public function __construct(GuzzleClient $guzzleClient)
65+
public function __construct(GuzzleClient $guzzleClient, array $config = [])
6066
{
6167
$this->guzzleClient = $guzzleClient;
68+
$this->config = $config;
69+
70+
$this->initilize();
71+
}
72+
73+
/**
74+
* Getter for guzzle client
75+
*
76+
* @return \GuzzleHttp\Client
77+
*/
78+
public function getGuzzleClient()
79+
{
80+
return $this->guzzleClient;
6281
}
6382

6483
/**
6584
* Set the url
6685
* will automatically append the protocol
6786
*
68-
* @param string $base the base url, can be url or something from config
69-
* @param string $protocol custom protocol to add
70-
*
87+
* @param string $base the base url, can be url or something from config
88+
* @param string $protocol custom protocol to add
7189
* @return \PulkitJalan\Requester\Client
7290
*/
7391
public function url($url)
@@ -80,8 +98,7 @@ public function url($url)
8098
/**
8199
* Use secure endpoint or not
82100
*
83-
* @param boolean $secure
84-
*
101+
* @param boolean $secure
85102
* @return \PulkitJalan\Requester\Client
86103
*/
87104
public function secure($secure)
@@ -94,8 +111,7 @@ public function secure($secure)
94111
/**
95112
* Verify ssl or not
96113
*
97-
* @param boolean|string $verify boolean or path to certificate
98-
*
114+
* @param boolean|string $verify boolean or path to certificate
99115
* @return \PulkitJalan\Requester\Client
100116
*/
101117
public function verify($verify)
@@ -108,22 +124,20 @@ public function verify($verify)
108124
/**
109125
* Set headers for the request
110126
*
111-
* @param array $headers
112-
*
127+
* @param array $headers
113128
* @return \PulkitJalan\Requester\Client
114129
*/
115130
public function headers(array $headers)
116131
{
117-
$this->options = array_merge($this->options, ['headers' => $headers]);
132+
$this->options = array_merge_recursive($this->options, ['headers' => $headers]);
118133

119134
return $this;
120135
}
121136

122137
/**
123138
* Number if times to retry
124139
*
125-
* @param int $retry times to retry
126-
*
140+
* @param int $retry times to retry
127141
* @return \PulkitJalan\Requester\Client
128142
*/
129143
public function retry($retry)
@@ -136,8 +150,7 @@ public function retry($retry)
136150
/**
137151
* Delay between retrying
138152
*
139-
* @param int $retryDelay delay between retrying
140-
*
153+
* @param int $retryDelay delay between retrying
141154
* @return \PulkitJalan\Requester\Client
142155
*/
143156
public function every($retryDelay)
@@ -150,7 +163,7 @@ public function every($retryDelay)
150163
/**
151164
* Types of errors to retry on
152165
*
153-
* @param array $retryOn errors to retry on
166+
* @param array $retryOn errors to retry on
154167
* @return \PulkitJalan\Requester\Client
155168
*/
156169
public function on(array $retryOn)
@@ -160,11 +173,29 @@ public function on(array $retryOn)
160173
return $this;
161174
}
162175

176+
/**
177+
* Add a file to the request
178+
*
179+
* @param string $filepath path to file
180+
* @param string $key optional post key, default to file
181+
* @return \PulkitJalan\Requester\Client
182+
*/
183+
public function addFile($filepath, $key = 'file')
184+
{
185+
$this->options = array_merge_recursive($this->options, [
186+
'body' => [
187+
$key => fopen($filepath, 'r')
188+
]
189+
]);
190+
191+
return $this;
192+
}
193+
163194
/**
164195
* Send get request
165196
*
166-
* @param array $options
167-
* @return guzzle response
197+
* @param array $options
198+
* @return \GuzzleHttp\Message\ResponseInterface
168199
*/
169200
public function get(array $options = [])
170201
{
@@ -174,8 +205,8 @@ public function get(array $options = [])
174205
/**
175206
* Send head request
176207
*
177-
* @param array $options
178-
* @return guzzle response
208+
* @param array $options
209+
* @return \GuzzleHttp\Message\ResponseInterface
179210
*/
180211
public function head(array $options = [])
181212
{
@@ -185,8 +216,8 @@ public function head(array $options = [])
185216
/**
186217
* Send delete request
187218
*
188-
* @param array $options
189-
* @return guzzle response
219+
* @param array $options
220+
* @return \GuzzleHttp\Message\ResponseInterface
190221
*/
191222
public function delete(array $options = [])
192223
{
@@ -196,8 +227,8 @@ public function delete(array $options = [])
196227
/**
197228
* Send put request
198229
*
199-
* @param array $options
200-
* @return guzzle response
230+
* @param array $options
231+
* @return \GuzzleHttp\Message\ResponseInterface
201232
*/
202233
public function put(array $options = [])
203234
{
@@ -207,8 +238,8 @@ public function put(array $options = [])
207238
/**
208239
* Send patch request
209240
*
210-
* @param array $options
211-
* @return guzzle response
241+
* @param array $options
242+
* @return \GuzzleHttp\Message\ResponseInterface
212243
*/
213244
public function patch(array $options = [])
214245
{
@@ -218,8 +249,8 @@ public function patch(array $options = [])
218249
/**
219250
* Send post request
220251
*
221-
* @param array $options
222-
* @return guzzle response
252+
* @param array $options
253+
* @return \GuzzleHttp\Message\ResponseInterface
223254
*/
224255
public function post(array $options = [])
225256
{
@@ -229,8 +260,8 @@ public function post(array $options = [])
229260
/**
230261
* Send options request
231262
*
232-
* @param array $options
233-
* @return guzzle response
263+
* @param array $options
264+
* @return \GuzzleHttp\Message\ResponseInterface
234265
*/
235266
public function options(array $options = [])
236267
{
@@ -239,7 +270,6 @@ public function options(array $options = [])
239270

240271
/**
241272
* Getter for the url will append protocol if one does not exist
242-
*
243273
* @return string
244274
*/
245275
public function getUrl()
@@ -256,27 +286,33 @@ public function getUrl()
256286
/**
257287
* Send the request using guzzle
258288
*
259-
* @param string $function function to call on guzzle
260-
* @param array $options options to pass
261-
*
262-
* @return guzzle response
289+
* @param string $function function to call on guzzle
290+
* @param array $options options to pass
291+
* @return \GuzzleHttp\Message\ResponseInterface
263292
*/
264293
protected function send($function, array $options = [])
265294
{
295+
$guzzle = $this->getGuzzleClient();
296+
266297
if ($this->retry) {
267-
$this->addRetrySubscriber();
298+
$guzzle = $this->addRetrySubscriber($guzzle);
268299
}
269300

301+
$url = $this->getUrl();
302+
270303
// merge options
271-
$options = array_merge($this->options, $options);
304+
$options = array_merge_recursive($this->options, $options);
305+
306+
// need to reset after every request
307+
$this->initilize();
272308

273-
return $this->guzzleClient->$function($this->getUrl(), $options);
309+
return $guzzle->$function($url, $options);
274310
}
275311

276312
/**
277313
* Add the retry subscriber to the guzzle client
278314
*/
279-
protected function addRetrySubscriber()
315+
protected function addRetrySubscriber(GuzzleClient $guzzle)
280316
{
281317
// Build retry subscriber
282318
$retry = new RetrySubscriber([
@@ -288,7 +324,9 @@ protected function addRetrySubscriber()
288324
]);
289325

290326
// add the retry emitter
291-
$this->guzzleClient->getEmitter()->attach($retry);
327+
$guzzle->getEmitter()->attach($retry);
328+
329+
return $guzzle;
292330
}
293331

294332
/**
@@ -306,4 +344,21 @@ protected function getProtocol()
306344

307345
return $protocol . '://';
308346
}
347+
348+
/**
349+
* Resets all variables to default values
350+
* required if using the same instance for multiple requests
351+
*
352+
* @return void
353+
*/
354+
protected function initilize()
355+
{
356+
$this->url = '';
357+
$this->options = [];
358+
$this->secure = array_get($this->config, 'secure', true);
359+
$this->retryOn = array_get($this->config, 'retry.on', [500, 502, 503, 504]);
360+
$this->retryDelay = array_get($this->config, 'retry.delay', 10);
361+
$this->retry = array_get($this->config, 'retry.times', 5);
362+
$this->verify(array_get($this->config, 'verify', true));
363+
}
309364
}

0 commit comments

Comments
 (0)