Skip to content
This repository was archived by the owner on Sep 6, 2019. It is now read-only.

Commit 0237d90

Browse files
committed
added async option
1 parent 2b370fa commit 0237d90

File tree

4 files changed

+102
-26
lines changed

4 files changed

+102
-26
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,19 @@ $requester->url('example.com/upload')->addFile('/tmp/image.jpg')->post([
101101
]
102102
]);
103103
```
104+
105+
Guzzle 5 uses RingPHP and has the added functionality of performing request asynchronously.
106+
107+
Performing asynchronous requests
108+
```php
109+
// Create a post request
110+
$response = $requester->url('example.com')->async(true)->get();
111+
112+
// Use the response asynchronously
113+
$this->response = $response->then(function ($response) {
114+
return $response->getBody();
115+
});
116+
117+
// Use the response synchronously
118+
$this->response = $response->getBody();
119+
```

src/Requester.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ class Requester
3939
*/
4040
protected $secure = true;
4141

42+
/**
43+
* Verify ssl connection
44+
*
45+
* @var boolean|string
46+
*/
47+
protected $verify = true;
48+
49+
/**
50+
* Make request asynchronously
51+
*
52+
* @var boolean
53+
*/
54+
protected $async = false;
55+
4256
/**
4357
* Retry request on which types of errors
4458
*
@@ -123,7 +137,20 @@ public function secure($secure)
123137
*/
124138
public function verify($verify)
125139
{
126-
$this->options = array_merge($this->options, ['verify' => $verify]);
140+
$this->verify = $verify;
141+
142+
return $this;
143+
}
144+
145+
/**
146+
* Make request asynchronously
147+
*
148+
* @param boolean $async
149+
* @return \PulkitJalan\Requester\Requester
150+
*/
151+
public function async($async)
152+
{
153+
$this->async = $async;
127154

128155
return $this;
129156
}
@@ -277,6 +304,7 @@ public function options(array $options = [])
277304

278305
/**
279306
* Getter for the url will append protocol if one does not exist
307+
*
280308
* @return string
281309
*/
282310
public function getUrl()
@@ -294,6 +322,24 @@ public function getUrl()
294322
return $url;
295323
}
296324

325+
/**
326+
* Getter for options
327+
*
328+
* @return array
329+
*/
330+
public function getOptions($options)
331+
{
332+
// Add verify to options
333+
$options = array_merge(['verify' => $this->verify], $options);
334+
335+
if ($this->async) {
336+
$options = array_merge(['future' => true], $options);
337+
}
338+
339+
// merge and return
340+
return array_merge_recursive($this->options, $options);
341+
}
342+
297343
/**
298344
* Send the request using guzzle
299345
*
@@ -308,7 +354,7 @@ protected function send($function, array $options = [])
308354
$url = $this->getUrl();
309355

310356
// merge options
311-
$options = array_merge_recursive($this->options, $options);
357+
$options = $this->getOptions($options);
312358

313359
// need to reset after every request
314360
$this->initialize();
@@ -363,6 +409,7 @@ protected function initialize()
363409
$this->retryOn = array_get($this->config, 'retry.on', [500, 502, 503, 504]);
364410
$this->retryDelay = array_get($this->config, 'retry.delay', 10);
365411
$this->retry = array_get($this->config, 'retry.times', 5);
366-
$this->verify(array_get($this->config, 'verify', true));
412+
$this->verify = array_get($this->config, 'verify', true);
413+
$this->async = array_get($this->config, 'async', false);
367414
}
368415
}

src/config/config.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
*/
2424
'verify' => true,
2525

26+
/*
27+
|--------------------------------------------------------------------------
28+
| Make request asynchronously (see README for more info)
29+
|--------------------------------------------------------------------------
30+
|
31+
| Advisable to leave it false and use the async method per request
32+
|
33+
*/
34+
'async' => false,
35+
2636
/*
2737
|--------------------------------------------------------------------------
2838
| Retry options

tests/RequesterTest.php

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@
44

55
use PHPUnit_Framework_TestCase;
66
use Mockery;
7-
use PulkitJalan\Requester\Requester;
87

98
class RequesterTest extends PHPUnit_Framework_TestCase
109
{
1110
public function setUp()
1211
{
1312
$this->guzzle = Mockery::mock('GuzzleHttp\Client')->makePartial();
14-
$this->requester = new Requester($this->guzzle, []);
13+
$this->requester = Mockery::mock('PulkitJalan\Requester\Requester', [$this->guzzle, []])->makePartial();
1514
}
1615

1716
public function tearDown()
1817
{
1918
Mockery::close();
2019
}
2120

22-
public function test_url_getter()
21+
public function testUrlGetter()
2322
{
2423
$this->requester->url('example.com');
2524

@@ -34,37 +33,42 @@ public function test_url_getter()
3433
$this->assertEquals('git://example.com', $this->requester->getUrl());
3534
}
3635

37-
public function test_invalid_url_exception()
36+
public function testInvalidUrlException()
3837
{
3938
$this->setExpectedException('PulkitJalan\Requester\Exceptions\InvalidUrlException');
4039

4140
$this->requester->getUrl();
4241
}
4342

44-
public function test_guzzle_getter()
43+
public function testGuzzleGetter()
4544
{
4645
$this->assertInstanceOf('GuzzleHttp\Client', $this->requester->getGuzzleClient());
4746
}
4847

49-
public function test_disabling_ssl_verification()
48+
public function testDisablingSslVerification()
5049
{
51-
$this->requester->verify(false);
50+
$this->guzzle->shouldReceive('get')->once()->with('https://example.com', [
51+
'verify' => false,
52+
]);
5253

53-
$this->assertEquals(['verify' => false], $this->readAttribute($this->requester, 'options'));
54+
$this->requester->url('example.com')->verify(false)->get();
55+
}
5456

55-
$this->guzzle->shouldReceive('get')->once()->with('http://example.com', [
56-
'verify' => false,
57+
public function testSettingAsync()
58+
{
59+
$this->guzzle->shouldReceive('get')->once()->with('https://example.com', [
60+
'verify' => true,
61+
'future' => true
5762
]);
5863

59-
$this->requester->url('example.com')->secure(false)->verify(false)->get();
64+
$this->requester->url('example.com')->async(true)->get();
6065
}
6166

62-
public function test_setting_and_adding_headers()
67+
public function testSettingAndAddingHeaders()
6368
{
6469
$this->requester->headers(['Authorization' => 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==']);
6570

6671
$this->assertEquals([
67-
'verify' => true,
6872
'headers' => [
6973
'Authorization' => 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==',
7074
],
@@ -73,15 +77,14 @@ public function test_setting_and_adding_headers()
7377
$this->requester->headers(['Cache-Control' => 'no-cache']);
7478

7579
$this->assertEquals([
76-
'verify' => true,
7780
'headers' => [
7881
'Authorization' => 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==',
7982
'Cache-Control' => 'no-cache',
8083
],
8184
], $this->readAttribute($this->requester, 'options'));
8285
}
8386

84-
public function test_adding_file_to_request()
87+
public function testAddingFileToRequest()
8588
{
8689
$this->requester->addFile(__FILE__);
8790

@@ -94,7 +97,7 @@ public function test_adding_file_to_request()
9497
$this->assertInternalType('resource', $this->readAttribute($this->requester, 'options')['body']['image']);
9598
}
9699

97-
public function test_changing_retry_options()
100+
public function testChangingRetryOptions()
98101
{
99102
$this->requester->retry(10);
100103

@@ -113,7 +116,7 @@ public function test_changing_retry_options()
113116
$this->assertEquals(false, $this->readAttribute($this->requester, 'retry'));
114117
}
115118

116-
public function test_sending_get_request()
119+
public function testSendingGetRequest()
117120
{
118121
$this->guzzle->shouldReceive('get')->once()->with('https://example.com', [
119122
'verify' => true,
@@ -122,7 +125,7 @@ public function test_sending_get_request()
122125
$this->requester->url('example.com')->get();
123126
}
124127

125-
public function test_sending_head_request()
128+
public function testSendingHeadRequest()
126129
{
127130
$this->guzzle->shouldReceive('head')->once()->with('https://example.com', [
128131
'verify' => true,
@@ -131,7 +134,7 @@ public function test_sending_head_request()
131134
$this->requester->url('example.com')->head();
132135
}
133136

134-
public function test_sending_options_request()
137+
public function testSendingOptionsRequest()
135138
{
136139
$this->guzzle->shouldReceive('options')->once()->with('https://example.com', [
137140
'verify' => true,
@@ -140,7 +143,7 @@ public function test_sending_options_request()
140143
$this->requester->url('example.com')->options();
141144
}
142145

143-
public function test_sending_post_request()
146+
public function testSendingPostRequest()
144147
{
145148
$this->guzzle->shouldReceive('post')->once()->with('https://example.com', [
146149
'verify' => true,
@@ -156,7 +159,7 @@ public function test_sending_post_request()
156159
]);
157160
}
158161

159-
public function test_sending_put_request()
162+
public function testSendingPutRequest()
160163
{
161164
$this->guzzle->shouldReceive('put')->once()->with('https://example.com', [
162165
'verify' => true,
@@ -172,7 +175,7 @@ public function test_sending_put_request()
172175
]);
173176
}
174177

175-
public function test_sending_patch_request()
178+
public function testSendingPatchRequest()
176179
{
177180
$this->guzzle->shouldReceive('patch')->once()->with('https://example.com', [
178181
'verify' => true,
@@ -188,7 +191,7 @@ public function test_sending_patch_request()
188191
]);
189192
}
190193

191-
public function test_sending_delete_request()
194+
public function testSendingDeleteRequest()
192195
{
193196
$this->guzzle->shouldReceive('delete')->once()->with('https://example.com', [
194197
'verify' => true,

0 commit comments

Comments
 (0)