コード例 #1
0
ファイル: UserStreams.cs プロジェクト: Rires-Magica/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 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
ファイル: UserStreamReceiver.cs プロジェクト: karno/Cadena
 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);
            }
        }