private static IObservable<StreamingMessage> AccessStreamingApiAsObservableImpl(StreamingApi e, StreamingType type, IEnumerable<KeyValuePair<string, object>> parameters) { return Observable.Create<StreamingMessage>((observer, cancel) => { return e.IncludedTokens.SendStreamingRequestAsync(GetMethodType(type), e.GetUrl(type), parameters, cancel) .ContinueWith(task => { if(task.IsFaulted) task.Exception.InnerException.Rethrow(); return task.Result.GetResponseStreamAsync(); }, cancel) .Unwrap() .ContinueWith(task => { if(task.IsFaulted) task.Exception.InnerException.Rethrow(); try { using (var reader = new StreamReader(task.Result)) using (cancel.Register(() => reader.Dispose())) { foreach (var s in reader.EnumerateLines().Where(x => !string.IsNullOrEmpty(x))) { try { observer.OnNext(StreamingMessage.Parse(s)); } catch (ParsingException ex) { observer.OnNext(RawJsonMessage.Create(s, ex)); } } } } finally { cancel.ThrowIfCancellationRequested(); } }, cancel, TaskContinuationOptions.LongRunning, TaskScheduler.Default); }); }
private static IObservable<StreamingMessage> AccessStreamingApiAsObservable(StreamingApi e, StreamingType type, object parameters) { return AccessStreamingApiAsObservableImpl(e, type, InternalUtils.ResolveObject(parameters)); }
private static IObservable<StreamingMessage> AccessStreamingApiAsObservable(StreamingApi e, StreamingType type, Expression<Func<string, object>>[] parameters) { return AccessStreamingApiAsObservableImpl(e, type, InternalUtils.ExpressionsToDictionary(parameters)); }
private static IObservable<StreamingMessage> AccessStreamingApiAsObservable(StreamingApi e, StreamingType type, IDictionary<string, object> parameters) { return AccessStreamingApiAsObservableImpl(e, type, parameters); }
/// <summary> /// Returns a small random sample of all public statuses. /// <para>The Tweets returned by the default access level are the same, so if two different clients connect to this endpoint, they will see the same Tweets.</para> /// <para>Available parameters:</para> /// <para>- <c>string</c> delimited (optional, not affects CoreTweet)</para> /// <para>- <c>bool</c> stall_warnings (optional)</para> /// </summary> /// <param name="e">The <see cref="StreamingApi"/> instance.</param> /// <param name="parameters">The parameters.</param> /// <returns>The stream messages.</returns> public static IObservable <StreamingMessage> SampleAsObservable(this StreamingApi e, params Expression <Func <string, object> >[] parameters) { return(AccessStreamingApiAsObservable(e, StreamingType.Sample, parameters)); }
/// <summary> /// Returns a small random sample of all public statuses. /// <para>The Tweets returned by the default access level are the same, so if two different clients connect to this endpoint, they will see the same Tweets.</para> /// <para>Available parameters:</para> /// <para>- <c>string</c> delimited (optional, not affects CoreTweet)</para> /// <para>- <c>bool</c> stall_warnings (optional)</para> /// </summary> /// <param name="e">The <see cref="StreamingApi"/> instance.</param> /// <param name="parameters">The parameters.</param> /// <returns>The stream messages.</returns> public static IObservable <StreamingMessage> SampleAsObservable(this StreamingApi e, object parameters) { return(AccessStreamingApiAsObservable(e, StreamingType.Sample, parameters)); }
/// <summary> /// Returns public statuses that match one or more filter predicates. /// <para>Multiple parameters may be specified which allows most clients to use a single connection to the Streaming API.</para> /// <para>Note: At least one predicate parameter (follow, locations, or track) must be specified.</para> /// <para>Available parameters:</para> /// <para>- <c>IEnumerable<long></c> follow (optional)</para> /// <para>- <c>string</c> track (optional)</para> /// <para>- <c>IEnumerable<double></c> locations (optional)</para> /// <para>- <c>string</c> delimited (optional, not affects CoreTweet)</para> /// <para>- <c>bool</c> stall_warnings (optional)</para> /// </summary> /// <param name="e">The <see cref="StreamingApi"/> instance.</param> /// <param name="parameters">The parameters.</param> /// <returns>The stream messages.</returns> public static IObservable <StreamingMessage> FilterAsObservable(this StreamingApi e, IDictionary <string, object> parameters) { return(AccessStreamingApiAsObservable(e, StreamingType.Filter, parameters)); }
private static IObservable <StreamingMessage> AccessStreamingApiAsObservable(StreamingApi e, StreamingType type, object parameters) { return(AccessStreamingApiAsObservableImpl(e, type, InternalUtils.ResolveObject(parameters))); }
private static IObservable <StreamingMessage> AccessStreamingApiAsObservable(StreamingApi e, StreamingType type, IDictionary <string, object> parameters) { return(AccessStreamingApiAsObservableImpl(e, type, parameters)); }
private static IObservable <StreamingMessage> AccessStreamingApiAsObservable(StreamingApi e, StreamingType type, Expression <Func <string, object> >[] parameters) { return(AccessStreamingApiAsObservableImpl(e, type, InternalUtils.ExpressionsToDictionary(parameters))); }
/// <summary> /// Starts the stream. /// </summary> /// <returns>The observable object.</returns> /// <param name="e">Tokens.</param> /// <param name="type">Type of streaming API.</param> /// <param name="parameters">Parameters.</param> public static IObservable <StreamingMessage> StartObservableStream(this StreamingApi e, StreamingType type, StreamingParameters parameters = null) { if (parameters == null) { parameters = new StreamingParameters(); } return(Observable.Create <StreamingMessage>((observer, cancel) => { var url = type == StreamingType.User ? "https://userstream.twitter.com/1.1/user.json" : type == StreamingType.Site ? " https://sitestream.twitter.com/1.1/site.json " : type == StreamingType.Filter ? "https://stream.twitter.com/1.1/statuses/filter.json" : type == StreamingType.Sample ? "https://stream.twitter.com/1.1/statuses/sample.json" : type == StreamingType.Firehose ? "https://stream.twitter.com/1.1/statuses/firehose.json" : ""; return e.IncludedTokens.SendStreamingRequestAsync(type == StreamingType.Filter ? MethodType.Post : MethodType.Get, url, parameters.Parameters, cancel) .ContinueWith(task => { if (task.IsFaulted) { task.Exception.InnerException.Rethrow(); } return task.Result.GetResponseStreamAsync(); }, cancel) .Unwrap() .ContinueWith(task => { if (task.IsFaulted) { task.Exception.InnerException.Rethrow(); } try { using (var reader = new StreamReader(task.Result)) using (var reg = cancel.Register(() => reader.Dispose())) { foreach (var s in reader.EnumerateLines().Where(x => !string.IsNullOrEmpty(x))) { #if !DEBUG try { #endif observer.OnNext(StreamingMessage.Parse(s)); #if !DEBUG } catch { observer.OnNext(RawJsonMessage.Create(s)); } #endif } } } finally { cancel.ThrowIfCancellationRequested(); } }, cancel, TaskContinuationOptions.LongRunning, TaskScheduler.Default); })); }