GetRecentTweets() public method

public GetRecentTweets ( int startIndex, int pageSize = 20 ) : IEnumerable
startIndex int
pageSize int
return IEnumerable
Esempio n. 1
0
        // get : api/twitter?start=0&page=20
        protected override string ProcessRequest()
        {
            int startIndex = 0;
            int pageSize = 20;
            string name = string.Empty;
            string tweetType = string.Empty;

            // get query string parameters
            string queryParameters = this.Request.RequestUri.Query;
            if (!string.IsNullOrWhiteSpace(queryParameters))
            {
                var qpParams = HttpUtility.ParseQueryString(queryParameters);

                if (!string.IsNullOrEmpty(qpParams["start"]))
                {
                    int.TryParse(qpParams["start"].ToString(), out startIndex);
                }

                if (!string.IsNullOrEmpty(qpParams["page"]))
                {
                    int.TryParse(qpParams["page"].ToString(), out pageSize);
                }
                if (!string.IsNullOrEmpty(qpParams["type"]))
                {
                    tweetType = qpParams["type"].ToString().ToLower();
                }
                if (!string.IsNullOrEmpty(qpParams["name"]))
                {
                    name = qpParams["name"].ToString().ToLower();
                }
            }

            try
            {
                var tableMgr = new TableManager();
                IEnumerable<TwitterEntity> tweets = null;
                if (string.IsNullOrEmpty(tweetType))
                {
                    tweets = tableMgr.GetRecentTweets(startIndex, pageSize);
                }
                else
                {
                    tweets = tableMgr.GetRecentTweets(tweetType, name, startIndex, pageSize);
                }

                return jsonSerializer.Value.Serialize(tweets);
            }
            catch (Exception ex)
            {
                // if any error occured then return User friendly message with system error message
                return jsonSerializer.Value.Serialize(
                  new
                  {
                      Status = "Error",
                      UserMessage = "Unable to get tweets",
                      ActualError = ex.Message
                  });
            }
        }