/// <summary> /// Reads the specified <paramref name="source"/> /// </summary> /// <param name="source">The source to be read</param> /// <returns>An <see cref="IEnumerable{BaseData}"/> that contains the data in the source</returns> public IEnumerable <BaseData> Read(SubscriptionDataSource source) { SubscriptionDataSourceReader.CheckRemoteFileCache(); IStreamReader reader = null; try { switch (source.TransportMedium) { default: case SubscriptionTransportMedium.Rest: reader = new RestSubscriptionStreamReader(source.Source); break; case SubscriptionTransportMedium.LocalFile: reader = new LocalFileSubscriptionStreamReader(_dataCacheProvider, source.Source); break; case SubscriptionTransportMedium.RemoteFile: reader = new RemoteFileSubscriptionStreamReader(_dataCacheProvider, source.Source, Globals.Cache); break; } var raw = ""; while (!reader.EndOfStream) { BaseDataCollection instances; try { raw = reader.ReadLine(); var result = _factory.Reader(_config, raw, _date, _isLiveMode); instances = result as BaseDataCollection; if (instances == null) { OnInvalidSource(source, new Exception("Reader must generate a BaseDataCollection with the FileFormat.Collection")); continue; } } catch (Exception err) { OnReaderError(raw, err); continue; } yield return(instances); } } finally { if (reader != null) { reader.Dispose(); } } }
/// <summary> /// Creates a new <see cref="IStreamReader"/> for the specified <paramref name="subscriptionDataSource"/> /// </summary> /// <param name="subscriptionDataSource">The source to produce an <see cref="IStreamReader"/> for</param> /// <returns>A new instance of <see cref="IStreamReader"/> to read the source, or null if there was an error</returns> private IStreamReader CreateStreamReader(SubscriptionDataSource subscriptionDataSource) { IStreamReader reader; switch (subscriptionDataSource.TransportMedium) { case SubscriptionTransportMedium.LocalFile: reader = HandleLocalFileSource(subscriptionDataSource); break; case SubscriptionTransportMedium.RemoteFile: reader = HandleRemoteSourceFile(subscriptionDataSource); break; case SubscriptionTransportMedium.Rest: reader = new RestSubscriptionStreamReader(subscriptionDataSource.Source); break; default: throw new InvalidEnumArgumentException("Unexpected SubscriptionTransportMedium specified: " + subscriptionDataSource.TransportMedium); } return(reader); }
/// <summary> /// Creates a new <see cref="IStreamReader"/> for the specified <paramref name="subscriptionDataSource"/> /// </summary> /// <param name="subscriptionDataSource">The source to produce an <see cref="IStreamReader"/> for</param> /// <returns>A new instance of <see cref="IStreamReader"/> to read the source, or null if there was an error</returns> protected IStreamReader CreateStreamReader(SubscriptionDataSource subscriptionDataSource) { IStreamReader reader = null; try { switch (subscriptionDataSource.TransportMedium) { case SubscriptionTransportMedium.LocalFile: reader = new LocalFileSubscriptionStreamReader(DataCacheProvider, subscriptionDataSource.Source); break; case SubscriptionTransportMedium.RemoteFile: reader = HandleRemoteSourceFile(subscriptionDataSource); break; case SubscriptionTransportMedium.Rest: reader = new RestSubscriptionStreamReader(subscriptionDataSource.Source, subscriptionDataSource.Headers, IsLiveMode); break; default: throw new InvalidEnumArgumentException("Unexpected SubscriptionTransportMedium specified: " + subscriptionDataSource.TransportMedium); } } catch (Exception e) { OnInvalidSource(subscriptionDataSource, e); return(reader); } if (reader == null || reader.EndOfStream) { OnInvalidSource(subscriptionDataSource, new Exception($"The reader was empty for source: ${subscriptionDataSource.Source}")); return(null); } return(reader); }
/// <summary> /// Reads the specified <paramref name="source"/> /// </summary> /// <param name="source">The source to be read</param> /// <returns>An <see cref="IEnumerable{BaseData}"/> that contains the data in the source</returns> public IEnumerable <BaseData> Read(SubscriptionDataSource source) { SubscriptionDataSourceReader.CheckRemoteFileCache(); IStreamReader reader = null; try { try { switch (source.TransportMedium) { default: case SubscriptionTransportMedium.Rest: reader = new RestSubscriptionStreamReader(source.Source, source.Headers, _isLiveMode); break; case SubscriptionTransportMedium.LocalFile: reader = new LocalFileSubscriptionStreamReader(_dataCacheProvider, source.Source); break; case SubscriptionTransportMedium.RemoteFile: reader = new RemoteFileSubscriptionStreamReader(_dataCacheProvider, source.Source, Globals.Cache, source.Headers); break; } } catch (Exception e) { OnInvalidSource(source, e); yield break; } var raw = ""; while (!reader.EndOfStream) { BaseDataCollection instances = null; try { raw = reader.ReadLine(); var result = _factory.Reader(_config, raw, _date, _isLiveMode); instances = result as BaseDataCollection; if (instances == null && !reader.ShouldBeRateLimited) { OnInvalidSource(source, new Exception("Reader must generate a BaseDataCollection with the FileFormat.Collection")); continue; } } catch (Exception err) { OnReaderError(raw, err); if (!reader.ShouldBeRateLimited) { continue; } } if (_isLiveMode // this shouldn't happen, rest reader is the only one to be rate limited // and in live mode, but just in case... || instances == null && reader.ShouldBeRateLimited) { yield return(instances); } else { foreach (var instance in instances.Data) { if (instance != null && instance.EndTime != default(DateTime)) { yield return(instance); } } } } } finally { if (reader != null) { reader.Dispose(); } } }