Skip to content

Commit 12b4e14

Browse files
authored
Version 1.1.1 (#4)
* Use tap * Laravel 7 & guzzle 7 * Immutable client (#1) * Replace/mutate client * Bug fixes and test * Update changelog * Defer service provider * Revert "Defer service provider" This reverts commit 8bce8a6. * Fix version * Merges config recursively (#3) * Temporary fix for merging config * Merge recursive distinct * Correct json driver merging * Rename helper method to avoid conflict * Update changelog * Update changelog * Update changelog
1 parent c664d2e commit 12b4e14

File tree

5 files changed

+109
-16
lines changed

5 files changed

+109
-16
lines changed

CHANGELOG.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
1616
1717
### Fixed -->
1818

19-
## [1.0.2](https://github.com/jenky/hermes/compare/1.0.2...1.1.0) - 2020-03-17
19+
## [1.1.1](https://github.com/jenky/hermes/compare/1.1.0...1.1.1) - 2020-02-25
20+
21+
### Fixed
22+
- Fixed merges config recursively
23+
24+
### Changed
25+
- `ResponseHandler` interceptor constructor now accepts class name instead of `ResponseHandlerInterface` instance
26+
27+
## [1.1.0](https://github.com/jenky/hermes/compare/1.0.2...1.1.0) - 2020-02-17
2028

2129
### Added
2230
- Prepare for Laravel 7 and Guzzle 7
2331

2432
### Changed
2533
- Mutable client by passing `$options` argument
2634

27-
## [1.0.2](https://github.com/jenky/hermes/compare/1.0.1...1.0.2) - 2020-03-13
35+
## [1.0.2](https://github.com/jenky/hermes/compare/1.0.1...1.0.2) - 2020-02-13
2836

2937
### Fixed
3038
- Some bugs

src/GuzzleManager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ protected function configurationFor($name)
112112
*/
113113
protected function resolve($name, array $options = [])
114114
{
115-
$config = array_merge_recursive(
115+
$config = array_merge_recursive_unique(
116116
$this->configurationFor($name), $options
117117
);
118118

@@ -199,14 +199,14 @@ protected function createGuzzleDriver(array $config)
199199
protected function createJsonDriver(array $config)
200200
{
201201
return new Client($this->makeClientOptions(
202-
array_merge_recursive($config, [
202+
array_merge_recursive_unique([
203203
'options' => [
204204
'response_handler' => JsonResponse::class,
205205
],
206206
'interceptors' => [
207207
Interceptors\ResponseHandler::class,
208208
],
209-
])
209+
], $config)
210210
));
211211
}
212212

src/Interceptors/ResponseHandler.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ class ResponseHandler
1212
/**
1313
* The default response handler class name.
1414
*
15-
* @var \Jenky\Hermes\Contracts\ResponseHandler
15+
* @var string
1616
*/
1717
protected $response;
1818

1919
/**
2020
* Create a new handler instance.
2121
*
22-
* @param \Jenky\Hermes\Contracts\ResponseHandler|null $response
22+
* @param string|null $response
2323
* @return void
2424
*/
25-
public function __construct(ResponseHandlerInterface $response = null)
25+
public function __construct($response = null)
2626
{
2727
$this->response = $response;
2828
}
@@ -51,21 +51,21 @@ public function __invoke(callable $handler)
5151
*/
5252
protected function createResponseHandler(ResponseInterface $response, array $options): ResponseInterface
5353
{
54-
$handler = $this->getResponseHandler($options) ?: $this->response;
54+
$handler = $this->getResponseHandler(
55+
$options['response_handler'] ?? $this->response
56+
);
5557

5658
return $handler ? $handler::create($response) : $response;
5759
}
5860

5961
/**
6062
* Get the response handler class name.
6163
*
62-
* @param array $options
64+
* @param string $handler
6365
* @return string|null
6466
*/
65-
protected function getResponseHandler(array $options)
67+
protected function getResponseHandler($handler)
6668
{
67-
$handler = $options['response_handler'] ?? null;
68-
6969
if ($handler && ! is_a($handler, ResponseHandlerInterface::class, true)) {
7070
throw new InvalidArgumentException(
7171
$handler.' must be an instance of '.ResponseHandlerInterface::class

src/helpers.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,44 @@ function guzzle($channel = null, array $options = [])
1515
return $channel ? app(Hermes::class)->channel($channel, $options) : app(Hermes::class);
1616
}
1717
}
18+
19+
if (! function_exists('array_merge_recursive_unique')) {
20+
/**
21+
* Merges any number of arrays / parameters recursively, using the left array as base, giving priority to the right array. Replacing entries with string keys with values from latter arrays.
22+
*
23+
* @return array
24+
*/
25+
function array_merge_recursive_unique()
26+
{
27+
$arrays = func_get_args();
28+
29+
if (count($arrays) < 2) {
30+
if ($arrays === []) {
31+
return [];
32+
} else {
33+
return $arrays[0];
34+
}
35+
}
36+
37+
$merged = array_shift($arrays);
38+
39+
foreach ($arrays as $array) {
40+
foreach ($array as $key => $value) {
41+
if (is_array($value) && (isset($merged[$key]) && is_array($merged[$key]))) {
42+
$merged[$key] = array_merge_recursive_unique($merged[$key], $value);
43+
} else {
44+
if (is_numeric($key)) {
45+
if (! in_array($value, $merged)) {
46+
$merged[] = $value;
47+
}
48+
} else {
49+
$merged[$key] = $value;
50+
}
51+
}
52+
}
53+
unset($key, $value);
54+
}
55+
56+
return $merged;
57+
}
58+
}

tests/FeatureTest.php

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
use Illuminate\Support\Facades\Event;
99
use Illuminate\Support\Str;
1010
use Jenky\Hermes\Contracts\Hermes;
11-
use Jenky\Hermes\Contracts\HttpResponseHandler;
1211
use Jenky\Hermes\Events\RequestHandled;
1312
use Jenky\Hermes\Interceptors\ResponseHandler;
13+
use Jenky\Hermes\JsonResponse;
1414
use Jenky\Hermes\Response;
1515
use Psr\Http\Message\RequestInterface;
1616
use SimpleXMLElement;
@@ -28,7 +28,7 @@ protected function setUp(): void
2828

2929
$this->app[Hermes::class]->extend('rss', function ($app, array $config) {
3030
return new Client($this->makeClientOptions(
31-
array_merge_recursive($config, [
31+
array_merge_recursive_unique($config, [
3232
'options' => [
3333
'response_handler' => XmlResponse::class,
3434
],
@@ -58,6 +58,14 @@ protected function getEnvironmentSetUp($app)
5858
],
5959
]);
6060

61+
$app['config']->set('hermes.channels.reqres', [
62+
'driver' => 'json',
63+
'options' => [
64+
'base_uri' => 'https://reqres.in',
65+
'response_handler' => ReqresResponse::class,
66+
],
67+
]);
68+
6169
$app['config']->set('hermes.channels.googlenews', [
6270
'driver' => 'rss',
6371
'options' => [
@@ -120,6 +128,14 @@ public function test_driver()
120128
$this->assertInstanceOf(SimpleXMLElement::class, $response->toXml());
121129
}
122130

131+
public function test_json_driver()
132+
{
133+
$response = guzzle('reqres')->get('api/users');
134+
135+
$this->assertInstanceOf(ReqresResponse::class, $response);
136+
$this->assertTrue($response->isSuccessful());
137+
}
138+
123139
public function test_custom_driver()
124140
{
125141
$response = guzzle('custom')->get('https://example.com');
@@ -142,19 +158,39 @@ public function test_mutable_client()
142158
$this->assertEquals(401, $response->getStatusCode());
143159

144160
// Mutate the client by creating new client instance
161+
$apiKey = (string) Str::uuid();
162+
145163
$this->httpClient([
146164
'options' => [
147165
'headers' => [
148166
'Authorization' => 'Bearer '.$token = Str::random(),
149167
],
150168
],
169+
'interceptors' => [
170+
function (callable $handler) use ($apiKey) {
171+
return function (RequestInterface $request, array $options) use ($handler, $apiKey) {
172+
$request = $request->withHeader('X-Api-Key', $apiKey);
173+
174+
return $handler($request, $options);
175+
};
176+
},
177+
Middleware::mapRequest(function (RequestInterface $request) {
178+
return $request->withHeader('Foo', 'Bar');
179+
}),
180+
],
151181
]);
152182

153183
$response = $this->httpClient()->get('bearer');
154184

155185
$this->assertTrue($response->isSuccessful());
156186
$this->assertTrue($response->authenticated);
157187
$this->assertEquals($token, $response->token);
188+
189+
$response = $this->httpClient()->get('anything');
190+
191+
$this->assertTrue($response->isSuccessful());
192+
$this->assertEquals($response->get('headers.X-Api-Key'), $apiKey);
193+
$this->assertEquals($response->get('headers.Foo'), 'Bar');
158194
}
159195

160196
public function test_default_channel()
@@ -191,10 +227,18 @@ class InvalidResponseHandler
191227
{
192228
}
193229

194-
class XmlResponse extends Response implements HttpResponseHandler
230+
class XmlResponse extends Response
195231
{
196232
public function toXml()
197233
{
198234
return new SimpleXMLElement((string) $this->getBody());
199235
}
200236
}
237+
238+
class ReqresResponse extends JsonResponse
239+
{
240+
public function isSuccessful(): bool
241+
{
242+
return parent::isSuccessful() && ! empty($this->get('data'));
243+
}
244+
}

0 commit comments

Comments
 (0)