@@ -9,6 +9,7 @@ import '../config/defaults/credential_type.dart';
99import '../config/defaults/default_config.dart' ;
1010import 'data_source.dart' ;
1111import 'data_source_status.dart' ;
12+ import 'data_source_requestor.dart' ;
1213import 'get_environment_id.dart' ;
1314
1415HttpClient _defaultClientFactory (HttpProperties httpProperties) {
@@ -28,6 +29,8 @@ final class PollingDataSource implements DataSource {
2829
2930 late final HttpClient _client;
3031
32+ late final DataSourceRequestor _requestor;
33+
3134 Timer ? _pollTimer;
3235
3336 final Stopwatch _pollStopwatch = Stopwatch ();
@@ -49,8 +52,6 @@ final class PollingDataSource implements DataSource {
4952 @override
5053 Stream <DataSourceEvent > get events => _eventController.stream;
5154
52- String ? _lastEtag;
53-
5455 /// Used to track if there has been an unrecoverable error.
5556 bool _permanentShutdown = false ;
5657
@@ -87,6 +88,9 @@ final class PollingDataSource implements DataSource {
8788 _client = clientFactory (httpProperties);
8889 }
8990
91+ _requestor = DataSourceRequestor (
92+ client: _client, logger: _logger, credential: credential);
93+
9094 final plainContextString =
9195 jsonEncode (LDContextSerialization .toJson (context, isEvent: false ));
9296 if (dataSourceConfig.useReport) {
@@ -111,66 +115,42 @@ final class PollingDataSource implements DataSource {
111115 }
112116
113117 Future <void > _makeRequest () async {
114- try {
115- _logger.debug (
116- 'Making polling request, method: $_method , uri: $_uri , etag: $_lastEtag ' );
117- final res = await _client.request (_method, _uri,
118- additionalHeaders: _lastEtag != null ? {'etag' : _lastEtag! } : null ,
119- body: _dataSourceConfig.useReport ? _contextString : null );
120- await _handleResponse (res);
121- } catch (err) {
122- _logger.error ('encountered error with polling request: $err , will retry' );
123- _eventController.sink
124- .add (StatusEvent (ErrorKind .networkError, null , err.toString ()));
125- }
126- }
127-
128- Future <void > _handleResponse (http.Response res) async {
129- // The data source has been instructed to stop, so we discard the response.
130118 if (_stopped) {
131119 return ;
132120 }
133121
134- if (res.statusCode == 200 || res.statusCode == 304 ) {
135- final etag = res.headers['etag' ];
136- if (etag != null && etag == _lastEtag) {
137- // The response has not changed, so we don't need to do the work of
138- // updating the store, calculating changes, or persisting the payload.
122+ final chainId = _requestor.startRequestChain ();
123+
124+ try {
125+ final res = await _requestor.makeRequest (
126+ chainId,
127+ _uri,
128+ _method,
129+ body: _dataSourceConfig.useReport ? _contextString : null ,
130+ );
131+
132+ final result = _requestor.processResponse (
133+ res,
134+ chainId,
135+ isRecoverableStatus: isHttpGloballyRecoverable,
136+ );
137+
138+ if (result == null ) {
139139 return ;
140140 }
141- _lastEtag = etag;
142141
143- var environmentId = getEnvironmentId (res.headers);
144-
145- if (environmentId == null &&
146- DefaultConfig .credentialConfig.credentialType ==
147- CredentialType .clientSideId) {
148- // When using a client-side ID we can use it to represent the
149- // environment.
150- environmentId = _credential;
142+ if (result.event != null ) {
143+ _eventController.sink.add (result.event! );
151144 }
152145
153- _eventController.sink
154- .add (DataEvent ('put' , res.body, environmentId: environmentId));
155- } else {
156- if (isHttpGloballyRecoverable (res.statusCode)) {
157- _eventController.sink.add (StatusEvent (
158- ErrorKind .networkError,
159- res.statusCode,
160- 'Received unexpected status code: ${res .statusCode }' ));
161- _logger.error (
162- 'received unexpected status code when polling: ${res .statusCode }, will retry' );
163- } else {
164- _logger.error (
165- 'received unexpected status code when polling: ${res .statusCode }, stopping polling' );
166- _eventController.sink.add (StatusEvent (
167- ErrorKind .networkError,
168- res.statusCode,
169- 'Received unexpected status code: ${res .statusCode }' ,
170- shutdown: true ));
146+ if (result.shutdown) {
171147 _permanentShutdown = true ;
172148 stop ();
173149 }
150+ } catch (err) {
151+ _logger.error ('encountered error with polling request: $err , will retry' );
152+ _eventController.sink
153+ .add (StatusEvent (ErrorKind .networkError, null , err.toString ()));
174154 }
175155 }
176156
0 commit comments