-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathGuzzleFeatureRequester.php
130 lines (118 loc) · 4.49 KB
/
GuzzleFeatureRequester.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
namespace LaunchDarkly\Impl\Integrations;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\HandlerStack;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Strategy\PublicCacheStrategy;
use LaunchDarkly\Impl\Model\FeatureFlag;
use LaunchDarkly\Impl\Model\Segment;
use LaunchDarkly\Impl\UnrecoverableHTTPStatusException;
use LaunchDarkly\Impl\Util;
use LaunchDarkly\Subsystems\FeatureRequester;
use Psr\Log\LoggerInterface;
/**
* @ignore
* @internal
*/
class GuzzleFeatureRequester implements FeatureRequester
{
const SDK_FLAGS = "sdk/flags";
const SDK_SEGMENTS = "sdk/segments";
private Client $_client;
private LoggerInterface $_logger;
public function __construct(string $baseUri, string $sdkKey, array $options)
{
$baseUri = \LaunchDarkly\Impl\Util::adjustBaseUri($baseUri);
$this->_logger = $options['logger'];
$stack = HandlerStack::create();
if (class_exists('\Kevinrob\GuzzleCache\CacheMiddleware')) {
$stack->push(
new CacheMiddleware(
new PublicCacheStrategy($options['cache'] ?? null)
),
'cache'
);
}
$defaults = [
'headers' => Util::defaultHeaders($sdkKey, $options),
'timeout' => $options['timeout'],
'connect_timeout' => $options['connect_timeout'],
'handler' => $stack,
'debug' => $options['debug'] ?? false,
'base_uri' => $baseUri
];
$this->_client = new Client($defaults);
}
/**
* Gets feature data from a likely cached store
*
* @param string $key feature key
* @return FeatureFlag|null The decoded FeatureFlag, or null if missing
*/
public function getFeature(string $key): ?FeatureFlag
{
try {
$response = $this->_client->get(self::SDK_FLAGS . "/" . $key);
$body = $response->getBody();
return FeatureFlag::decode(json_decode($body->getContents(), true));
} catch (BadResponseException $e) {
/** @psalm-suppress PossiblyNullReference (resolved in guzzle 7) */
$code = $e->getResponse()->getStatusCode();
if ($code == 404) {
$this->_logger->warning("GuzzleFeatureRequester::get returned 404. Feature flag does not exist for key: " . $key);
} else {
$this->handleUnexpectedStatus($code, "GuzzleFeatureRequester::get");
}
return null;
}
}
/**
* Gets segment data from a likely cached store
*
* @param string $key segment key
* @return Segment|null The decoded Segment, or null if missing
*/
public function getSegment(string $key): ?Segment
{
try {
$response = $this->_client->get(self::SDK_SEGMENTS . "/" . $key);
$body = $response->getBody();
return Segment::decode(json_decode($body->getContents(), true));
} catch (BadResponseException $e) {
/** @psalm-suppress PossiblyNullReference (resolved in guzzle 7) */
$code = $e->getResponse()->getStatusCode();
if ($code == 404) {
$this->_logger->warning("GuzzleFeatureRequester::get returned 404. Segment does not exist for key: " . $key);
} else {
$this->handleUnexpectedStatus($code, "GuzzleFeatureRequester::get");
}
return null;
}
}
/**
* Gets all features from a likely cached store
*
* @return array<string, FeatureFlag>|null The decoded FeatureFlags, or null if missing
*/
public function getAllFeatures(): ?array
{
try {
$response = $this->_client->get(self::SDK_FLAGS);
$body = $response->getBody();
return array_map(FeatureFlag::getDecoder(), json_decode($body->getContents(), true));
} catch (BadResponseException $e) {
/** @psalm-suppress PossiblyNullReference (resolved in guzzle 7) */
$this->handleUnexpectedStatus($e->getResponse()->getStatusCode(), "GuzzleFeatureRequester::getAll");
return null;
}
}
private function handleUnexpectedStatus(int $code, string $method): void
{
$this->_logger->error(Util::httpErrorMessage($code, $method, 'default value was returned'));
if (!Util::isHttpErrorRecoverable($code)) {
throw new UnrecoverableHTTPStatusException($code);
}
}
}