Esempio n. 1
0
        public static byte[] ToBinary(this ITwitterResponse twitterResponse)
        {
            var stream = twitterResponse.ResultStream;

            if (stream == null)
            {
                return(null);
            }

            byte[] binary;

            using (var tempMemStream = new MemoryStream())
            {
                byte[] buffer = new byte[128];

                while (true)
                {
                    int read = stream.Read(buffer, 0, buffer.Length);

                    if (read <= 0)
                    {
                        binary = tempMemStream.ToArray(); break;
                    }

                    tempMemStream.Write(buffer, 0, read);
                }
            }

            return(binary);
        }
Esempio n. 2
0
 public ITwitterResult <T> Create <T>(ITwitterRequest request, ITwitterResponse response) where T : class
 {
     return(new TwitterResult <T>(_jsonObjectConverter)
     {
         Response = response,
         Request = request
     });
 }
Esempio n. 3
0
 public ITwitterResult Create(ITwitterRequest request, ITwitterResponse response)
 {
     return(new TwitterResult
     {
         Response = response,
         Request = request
     });
 }
Esempio n. 4
0
 public TwitterException(
     IWebExceptionInfoExtractor webExceptionInfoExtractor,
     ITwitterResponse twitterResponse,
     ITwitterRequest request)
     : this(request)
 {
     StatusCode            = twitterResponse.StatusCode;
     TwitterExceptionInfos = webExceptionInfoExtractor.GetTwitterExceptionInfosFromStream(twitterResponse.ResultStream);
     TwitterDescription    = webExceptionInfoExtractor.GetStatusCodeDescription(StatusCode);
 }
Esempio n. 5
0
        public static string ToJson(this ITwitterResponse twitterResponse)
        {
            var resultStream = twitterResponse.ResultStream;

            if (resultStream != null)
            {
                var responseReader = new StreamReader(resultStream);
                var json           = responseReader.ReadLine();

                return(json);
            }

            return(null);
        }
        private void QueryCompleted(
            ITwitterRequest request,
            ITwitterResponse twitterResponse,
            IRateLimitUpdater rateLimitUpdater)
        {
            if (request.ExecutionContext.RateLimitTrackerMode != RateLimitTrackerMode.None)
            {
                var rateLimitHeaders = twitterResponse.Headers.Where(kvp => kvp.Key.StartsWith("x-rate-limit-")).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                rateLimitUpdater.QueryExecutedAsync(request.Query.Url, request.Query.TwitterCredentials, rateLimitHeaders);
            }

            request.ExecutionContext.Events.RaiseAfterExecutingQuery(new AfterExecutingQueryEventArgs(request.Query, twitterResponse.Content, twitterResponse.Headers));
        }
        public static AlexaRequest Map(ITwitterResponse tweet)
        {
            int timeZone = -8;

            return(new AlexaRequest()
            {
                UID = tweet.UID,
                UpdateDate = tweet.CreatedAt,
                TitleText = $"Sound Transit Tweet from {ApplyOffset(tweet.CreatedAt, timeZone).ToString("M/d/yy h:mm tt")}",
                MainText = $"{GenerateDayText(tweet.CreatedAt)}, {new AlexaRequestFormatter(tweet.FullText).Format()}",
                RedirectionUrl = $"http://www.twitter.com/SoundTransit/status/{tweet.UID}"
            });

            string GenerateDayText(DateTimeOffset createdAt)
            {
                var sB = new StringBuilder();

                if (ApplyOffset(createdAt, timeZone).Date == ApplyOffset(DateTimeOffset.Now, timeZone).Date)
                {
                    sB.Append("Today");
                }
                else if (ApplyOffset(createdAt, timeZone).Date == ApplyOffset(DateTimeOffset.Now, timeZone).AddDays(-1).Date)
                {
                    sB.Append("Yesterday");
                }
                else
                {
                    sB.Append(createdAt.ToString("MMMM dd"));
                }

                sB.Append(" at ");
                sB.Append(createdAt.DateTime.AddHours(-8).ToString("h:mm tt"));

                return(sB.ToString());
            }

            DateTime ApplyOffset(DateTimeOffset originalDateTime, int offset)
            {
                if (originalDateTime.TimeOfDay.Hours < offset)
                {
                    return(originalDateTime.DateTime.AddDays(-1).AddHours(offset));
                }
                else
                {
                    return(originalDateTime.DateTime.AddHours(offset));
                }
            }
        }
Esempio n. 8
0
        public TwitterException(
            IWebExceptionInfoExtractor webExceptionInfoExtractor,
            ITwitterResponse twitterResponse,
            ITwitterRequest request)
            : this(request, twitterResponse.ReasonPhrase)
        {
            StatusCode = twitterResponse.StatusCode;
            if (twitterResponse.ResultStream != null)
            {
                using (var reader = new StreamReader(twitterResponse.ResultStream))
                {
                    Content = reader.ReadToEnd();
                    TwitterExceptionInfos = webExceptionInfoExtractor.GetTwitterExceptionInfos(Content);
                }
            }

            TwitterDescription = webExceptionInfoExtractor.GetStatusCodeDescription(StatusCode);
        }
Esempio n. 9
0
        private async Task QueryCompleted(
            ITwitterRequest request,
            ITwitterResponse twitterResponse,
            IRateLimitUpdater rateLimitUpdater)
        {
            if (request.ExecutionContext.RateLimitTrackerMode != RateLimitTrackerMode.None)
            {
                var credentials      = request.Query.TwitterCredentials;
                var rateLimitHeaders = twitterResponse.Headers
                                       .Where(kvp => kvp.Key.StartsWith("x-rate-limit-"))
                                       .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                await rateLimitUpdater.QueryExecutedAsync(request.Query.Url, credentials, rateLimitHeaders).ConfigureAwait(false);

                if (request.Query.QueryRateLimit == null)
                {
                    var getEndPointParameters = new GetEndpointRateLimitsParameters(request.Query.Url);
                    request.Query.QueryRateLimit = await _rateLimitCacheManager.GetQueryRateLimitAsync(getEndPointParameters, credentials).ConfigureAwait(false);
                }
            }

            request.ExecutionContext.Events.RaiseAfterExecutingQuery(new AfterExecutingQueryEventArgs(request.Query, twitterResponse.Content, twitterResponse.Headers));
        }
Esempio n. 10
0
 public TwitterException Create(ITwitterResponse twitterResponse, ITwitterRequest request)
 {
     return(new TwitterException(_webExceptionInfoExtractor, twitterResponse, request));
 }
Esempio n. 11
0
 public ITwitterResult <TDTO, TModel> Create <TDTO, TModel>(ITwitterRequest request, ITwitterResponse response, Func <TDTO, TModel> convert)
     where TDTO : class
 {
     return(new TwitterResult <TDTO, TModel>(_jsonObjectConverter, convert)
     {
         Response = response,
         Request = request
     });
 }