예제 #1
0
        /// <summary>
        /// Connect to user streams.
        /// </summary>
        /// <param name="accessor">API access preference</param>
        /// <param name="parser">Line handler</param>
        /// <param name="readTimeout">stream read timeout</param>
        /// <param name="cancellationToken">cancellation token object</param>
        /// <param name="tracksOrNull">tracks parameter(can be null)</param>
        /// <param name="stallWarnings">request stall warnings</param>
        /// <param name="filterLevel">stream filtering level</param>
        /// <param name="repliesAll">repliesAll parameter</param>
        /// <param name="followingsActivity">include_followings_activity parameter</param>
        /// <returns></returns>
        public static Task ConnectAsync([NotNull] IApiAccessor accessor,
                                        [NotNull] Action <string> parser, TimeSpan readTimeout, CancellationToken cancellationToken,
                                        [CanBeNull] IEnumerable <string> tracksOrNull = null, bool stallWarnings = false,
                                        StreamFilterLevel filterLevel = StreamFilterLevel.None,
                                        bool repliesAll = false, bool followingsActivity = false)
        {
            if (accessor == null)
            {
                throw new ArgumentNullException(nameof(accessor));
            }
            if (parser == null)
            {
                throw new ArgumentNullException(nameof(parser));
            }

            // remove empty string and remove duplicates, concatenate strings
            var filteredTracks = tracksOrNull?.Select(t => t?.Trim())
                                 .Where(t => !String.IsNullOrEmpty(t))
                                 .Distinct()
                                 .JoinString(",");
            // build parameter
            var param = new Dictionary <string, object>
            {
                { "track", String.IsNullOrEmpty(filteredTracks) ? null : filteredTracks },
                { "stall_warnings", stallWarnings ? "true" : null },
                { "filter_level", filterLevel == StreamFilterLevel.None ? null : filterLevel.ToParamString() },
                { "replies", repliesAll ? "all" : null },
                { "include_followings_activity", followingsActivity ? "true" : null }
            };

            // begin connection
            return(accessor.ConnectStreamAsync("user.json", param,
                                               stream => StreamWinder.Run(stream, parser, readTimeout, cancellationToken),
                                               cancellationToken));
        }
예제 #2
0
파일: UserStreams.cs 프로젝트: karno/Cadena
        /// <summary>
        /// Connect to user streams.
        /// </summary>
        /// <param name="accessor">API access preference</param>
        /// <param name="parser">Line handler</param>
        /// <param name="readTimeout">stream read timeout</param>
        /// <param name="cancellationToken">cancellation token object</param>
        /// <param name="tracksOrNull">tracks parameter(can be null)</param>
        /// <param name="stallWarnings">request stall warnings</param>
        /// <param name="filterLevel">stream filtering level</param>
        /// <param name="repliesAll">repliesAll parameter</param>
        /// <param name="followingsActivity">include_followings_activity parameter</param>
        /// <returns></returns>
        public static async Task ConnectAsync([NotNull] ApiAccessor accessor,
            [NotNull] Action<string> parser, TimeSpan readTimeout, CancellationToken cancellationToken,
            [CanBeNull] IEnumerable<string> tracksOrNull = null, bool stallWarnings = false,
            StreamFilterLevel filterLevel = StreamFilterLevel.None,
            bool repliesAll = false, bool followingsActivity = false)
        {
            if (accessor == null) throw new ArgumentNullException(nameof(accessor));
            if (parser == null) throw new ArgumentNullException(nameof(parser));

            // remove empty string and remove duplicates, concat strings
            var filteredTracks = tracksOrNull?.Select(t => t?.Trim())
                                              .Where(t => !String.IsNullOrEmpty(t))
                                              .Distinct()
                                              .JoinString(",");
            // bulid parameter
            var param = new Dictionary<string, object>
            {
                {"track", String.IsNullOrEmpty(filteredTracks) ? null : filteredTracks},
                {"stall_warnings", stallWarnings ? "true" : null},
                {"filter_level", filterLevel == StreamFilterLevel.None ? null : filterLevel.ToParamString()},
                {"replies", repliesAll ? "all" : null},
                {"include_followings_activity", followingsActivity ? "true" : null}
            }.ParametalizeForGet();
            var endpoint = HttpUtility.ConcatUrl(accessor.Endpoint, "user.json");

            // join parameters to endpoint URL
            if (!String.IsNullOrEmpty(param))
            {
                endpoint += "?" + param;
            }

            // begin connection
            HttpClient client = null;
            try
            {
                // prepare HttpClient
                client = accessor.GetClientForStreaming();
                // set parameters for receiving UserStreams.
                client.Timeout = Timeout.InfiniteTimeSpan;
                // begin connection
                using (var resp = await client.GetAsync(endpoint, HttpCompletionOption.ResponseHeadersRead,
                    cancellationToken).ConfigureAwait(false))
                using (var stream = await resp.Content.ReadAsStreamAsync().ConfigureAwait(false))
                {
                    // winding data from user stream
                    await StreamWinder.Run(stream, parser, readTimeout, cancellationToken).ConfigureAwait(false);
                }
            }
            finally
            {
                if (client != null)
                {
                    // cancel pending requests
                    client.CancelPendingRequests();
                    client.Dispose();
                }
            }
        }
예제 #3
0
 public UserStreamReceiver(ApiAccessor accessor, IStreamHandler handler)
 {
     _accessor = accessor;
     _handler = handler;
     ChangeState(StreamState.Disconnected);
     // set default values to parameters
     StallWarnings = true;
     StreamFilterLevel = StreamFilterLevel.None;
     _currentState = StreamState.Connected;
     _backoffMode = BackoffMode.None;
     _backoffWait = 0;
     _hardErrorCount = 0;
 }
예제 #4
0
        public static string ToParamString(this StreamFilterLevel level)
        {
            switch (level)
            {
            case StreamFilterLevel.None:
                return("none");

            case StreamFilterLevel.Low:
                return("low");

            case StreamFilterLevel.Middle:
                return("middle");

            default:
                throw new ArgumentOutOfRangeException(nameof(level), level, null);
            }
        }