Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit d370d33

Browse files
authored
Make endpoint and request options configurable. (#28)
1 parent 5e54aaa commit d370d33

File tree

5 files changed

+65
-22
lines changed

5 files changed

+65
-22
lines changed

config/unleash.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55
// This should be the base URL, do not include /api or anything else.
66
'url' => env('UNLEASH_URL'),
77

8+
// Other default settings for requests to your Unleash server.
9+
'otherRequestDefaults' => [
10+
// Any other defaults for the request can be added here.
11+
// e.g. your Unleash server may require headers like these.
12+
//
13+
// 'headers' => [
14+
// 'UNLEASH-APPNAME' => env('UNLEASH_APPNAME', env('APP_ENV')),
15+
// 'UNLEASH-INSTANCEID' => env('UNLEASH_INSTANCEID'),
16+
// ],
17+
],
18+
19+
// Endpoint for accessing the feature flags, on your Unleash server.
20+
// The default is /api/client/features;
21+
// your Unleash server may use a different endpoint e.g. if it houses flags for multiple projects.
22+
'featuresEndpoint' => env('UNLEASH_FEATURES_ENDPOINT', '/api/client/features'),
23+
824
// Globally control whether Unleash is enabled or disabled.
925
// If not enabled, no API requests will be made and all "enabled" checks will return `false` and
1026
// "disabled" checks will return `true`.

src/Client.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ class Client extends GuzzleClient
1010
public function __construct(Config $config)
1111
{
1212
parent::__construct(
13-
[
14-
'base_uri' => $config->get('unleash.url'),
15-
]
13+
array_merge(
14+
['base_uri' => $config->get('unleash.url')],
15+
$config->get('unleash.otherRequestDefaults')
16+
)
1617
);
1718
}
1819
}

src/Unleash.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ function () {
127127

128128
protected function fetchFeatures(): array
129129
{
130-
$response = $this->client->get($this->getFeaturesApiUrl(), $this->getRequestOptions());
130+
$response = $this->client->get($this->config->get('unleash.featuresEndpoint'));
131131

132132
try {
133133
$data = json_decode((string)$response->getBody(), true, 512, \JSON_BIGINT_AS_STRING);
@@ -137,16 +137,6 @@ protected function fetchFeatures(): array
137137

138138
return $this->formatResponse($data);
139139
}
140-
141-
protected function getFeaturesApiUrl(): string
142-
{
143-
return '/api/client/features';
144-
}
145-
146-
protected function getRequestOptions(): array
147-
{
148-
return [];
149-
}
150140

151141
protected function formatResponse($data): array
152142
{

tests/Strategies/DynamicStrategyTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function testWithArgs()
8888
}
8989

9090
/**
91-
* @param \PHPUnit\Framework\MockObject\MockObject $strategy
91+
* @param \PHPUnit\Framework\MockObject\MockObject $strategy
9292
* @return Config|\PHPUnit\Framework\MockObject\MockObject
9393
*/
9494
protected function getMockConfig(\PHPUnit\Framework\MockObject\MockObject $strategy)
@@ -104,6 +104,10 @@ protected function getMockConfig(\PHPUnit\Framework\MockObject\MockObject $strat
104104
->with('unleash.cache.isEnabled')
105105
->willReturn(false);
106106
$config->expects($this->at(2))
107+
->method('get')
108+
->with('unleash.featuresEndpoint')
109+
->willReturn('/api/client/features');
110+
$config->expects($this->at(3))
107111
->method('get')
108112
->with('unleash.strategies')
109113
->willReturn(
@@ -113,15 +117,15 @@ protected function getMockConfig(\PHPUnit\Framework\MockObject\MockObject $strat
113117
},
114118
]
115119
);
116-
$config->expects($this->at(3))
120+
$config->expects($this->at(4))
117121
->method('get')
118122
->with('unleash.isEnabled')
119123
->willReturn(true);
120-
$config->expects($this->at(4))
124+
$config->expects($this->at(5))
121125
->method('get')
122126
->with('unleash.cache.isEnabled')
123127
->willReturn(false);
124-
$config->expects($this->at(5))
128+
$config->expects($this->at(6))
125129
->method('get')
126130
->with('unleash.strategies')
127131
->willReturn(

tests/UnleashTest.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ public function testFeaturesCacheFailoverEnabledIndependently()
356356
->method('get')
357357
->with('unleash.cache.isEnabled')
358358
->willReturn(false);
359+
$config->expects($this->at(2))
360+
->method('get')
361+
->with('unleash.featuresEndpoint')
362+
->willReturn('/api/client/features');
359363

360364
$cache->expects($this->at(0))
361365
->method('forever')
@@ -366,7 +370,7 @@ public function testFeaturesCacheFailoverEnabledIndependently()
366370
],
367371
]);
368372

369-
$config->expects($this->at(2))
373+
$config->expects($this->at(3))
370374
->method('get')
371375
->with('unleash.strategies')
372376
->willReturn(
@@ -377,15 +381,19 @@ public function testFeaturesCacheFailoverEnabledIndependently()
377381

378382

379383
// Request 2
380-
$config->expects($this->at(3))
384+
$config->expects($this->at(4))
381385
->method('get')
382386
->with('unleash.isEnabled')
383387
->willReturn(true);
384-
$config->expects($this->at(4))
388+
$config->expects($this->at(5))
385389
->method('get')
386390
->with('unleash.cache.isEnabled')
387391
->willReturn(false);
388-
$config->expects($this->at(5))
392+
$config->expects($this->at(6))
393+
->method('get')
394+
->with('unleash.featuresEndpoint')
395+
->willReturn('/api/client/features');
396+
$config->expects($this->at(7))
389397
->method('get')
390398
->with('unleash.cache.failover')
391399
->willReturn(true);
@@ -444,6 +452,10 @@ public function testCanHandleErrorsFromUnleash()
444452
$config->expects($this->at(1))
445453
->method('get')
446454
->with('unleash.cache.isEnabled')->willReturn(false);
455+
$config->expects($this->at(2))
456+
->method('get')
457+
->with('unleash.featuresEndpoint')
458+
->willReturn('/api/client/features');
447459

448460
$request = $this->createMock(Request::class);
449461

@@ -484,6 +496,10 @@ public function testIsFeatureEnabled()
484496
->method('get')
485497
->with('unleash.cache.isEnabled')
486498
->willReturn(false);
499+
$config->expects($this->at(2))
500+
->method('get')
501+
->with('unleash.featuresEndpoint')
502+
->willReturn('/api/client/features');
487503

488504
$request = $this->createMock(Request::class);
489505

@@ -530,6 +546,10 @@ public function testIsFeatureEnabledWithValidStrategy()
530546
->with('unleash.cache.isEnabled')
531547
->willReturn(false);
532548
$config->expects($this->at(2))
549+
->method('get')
550+
->with('unleash.featuresEndpoint')
551+
->willReturn('/api/client/features');
552+
$config->expects($this->at(3))
533553
->method('get')
534554
->with('unleash.strategies')
535555
->willReturn(
@@ -583,6 +603,10 @@ public function testIsFeatureDisabledWithInvalidStrategy()
583603
->with('unleash.cache.isEnabled')
584604
->willReturn(false);
585605
$config->expects($this->at(2))
606+
->method('get')
607+
->with('unleash.featuresEndpoint')
608+
->willReturn('/api/client/features');
609+
$config->expects($this->at(3))
586610
->method('get')
587611
->with('unleash.strategies')
588612
->willReturn(
@@ -636,6 +660,10 @@ public function testIsFeatureDisabledWithStrategyThatDoesNotImplementBaseStrateg
636660
->with('unleash.cache.isEnabled')
637661
->willReturn(false);
638662
$config->expects($this->at(2))
663+
->method('get')
664+
->with('unleash.featuresEndpoint')
665+
->willReturn('/api/client/features');
666+
$config->expects($this->at(3))
639667
->method('get')
640668
->with('unleash.strategies')
641669
->willReturn(
@@ -687,6 +715,10 @@ public function testIsFeatureDisabled()
687715
->method('get')
688716
->with('unleash.cache.isEnabled')
689717
->willReturn(false);
718+
$config->expects($this->at(2))
719+
->method('get')
720+
->with('unleash.featuresEndpoint')
721+
->willReturn('/api/client/features');
690722

691723
$request = $this->createMock(Request::class);
692724

0 commit comments

Comments
 (0)