Lean  $LEAN_TAG$
RestSubscriptionStreamReader.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
17 using System;
18 using System.Collections.Generic;
19 using System.IO;
21 using QuantConnect.Logging;
22 using RestSharp;
23 
25 {
26  /// <summary>
27  /// Represents a stream reader capable of polling a rest client
28  /// </summary>
30  {
31  private readonly RestClient _client;
32  private readonly RestRequest _request;
33  private readonly bool _isLiveMode;
34  private bool _delivered;
35 
36  /// <summary>
37  /// Gets whether or not this stream reader should be rate limited
38  /// </summary>
39  public bool ShouldBeRateLimited => _isLiveMode;
40 
41  /// <summary>
42  /// Direct access to the StreamReader instance
43  /// </summary>
44  public StreamReader StreamReader => null;
45 
46  /// <summary>
47  /// Initializes a new instance of the <see cref="RestSubscriptionStreamReader"/> class.
48  /// </summary>
49  /// <param name="source">The source url to poll with a GET</param>
50  /// <param name="headers">Defines header values to add to the request</param>
51  /// <param name="isLiveMode">True for live mode, false otherwise</param>
52  public RestSubscriptionStreamReader(string source, IEnumerable<KeyValuePair<string, string>> headers, bool isLiveMode)
53  {
54  _client = new RestClient(source);
55  _request = new RestRequest(Method.GET);
56  _isLiveMode = isLiveMode;
57  _delivered = false;
58 
59  if (headers != null)
60  {
61  foreach (var header in headers)
62  {
63  _request.AddHeader(header.Key, header.Value);
64  }
65  }
66  }
67 
68  /// <summary>
69  /// Gets <see cref="SubscriptionTransportMedium.Rest"/>
70  /// </summary>
72  {
73  get { return SubscriptionTransportMedium.Rest; }
74  }
75 
76  /// <summary>
77  /// Gets whether or not there's more data to be read in the stream
78  /// </summary>
79  public bool EndOfStream
80  {
81  get { return !_isLiveMode && _delivered; }
82  }
83 
84  /// <summary>
85  /// Gets the next line/batch of content from the stream
86  /// </summary>
87  public string ReadLine()
88  {
89  try
90  {
91  var response = _client.Execute(_request);
92  if (response != null)
93  {
94  _delivered = true;
95  return response.Content;
96  }
97  }
98  catch (Exception err)
99  {
100  Log.Error(err);
101  }
102 
103  return string.Empty;
104  }
105 
106  /// <summary>
107  /// This stream reader doesn't require disposal
108  /// </summary>
109  public void Dispose()
110  {
111  }
112  }
113 }