Exemplo n.º 1
0
        public HttpResponseMessage Post([FromBody] ReviewDownloadRequest input)
        {
            var downloadedReviews = new ReviewDownloadLogic().DownloadReviews(input);
            var finalResponse     = Request.CreateResponse(downloadedReviews);

            return(finalResponse);
        }
Exemplo n.º 2
0
        public ReviewDownloadResponse DownloadReviews(ReviewDownloadRequest input)
        {
            IReviewService reviewService = null;
            string         productUrl    = input.ProductUrl != null?input
                                           .ProductUrl
                                           .Trim()
                                           .Replace("http://", "")
                                           .Replace("https://", "") : null;

            if (productUrl == null)
            {
                return(InvalidUrlResponse);
            }

            if (productUrl.StartsWith("itunes.apple.com"))
            {
                reviewService = new AppleStoreReviewService();
            }

            else if (productUrl.StartsWith("play.google.com"))
            {
                reviewService = new PlayStoreReviewService();
            }

            else if (productUrl.StartsWith("www.amazon.com"))
            {
                reviewService = new AmazonReviewService();
            }

            else
            {
                return(InvalidUrlResponse);
            }

            productUrl = $"https://{productUrl}";

            var theReviews = new List <Review>();


            var    downloadTimeoutMilliSeconds = _downloadTimeoutSeconds * 1000;
            bool   isJobDone      = false;
            string errorMessage   = null;
            string reviewsFileUrl = null;

            HostingEnvironment.QueueBackgroundWorkItem(ct =>
            {
                {
                    try
                    {
                        reviewService.GetReviewsFromUrl(theReviews, productUrl, 0, false, Math.Min(input.Count, _enforcedMaximum));
                        if (isTimeout)
                        {
                            reviewsFileUrl = GenerateAndSaveReviews(theReviews, reviewService.Name);

                            HostingEnvironment.QueueBackgroundWorkItem(cz =>
                            {
                                SendLinkToUser(input.Email, reviewsFileUrl, reviewService.Name);
                            });
                        }
                    }

                    catch (ReviewDownloadException ex)
                    {
                        //Log: There was an issue while downloading
                        errorMessage = ex.Message;
                    }

                    finally
                    {
                        isJobDone = true;
                    }
                }
            });

            var timer = new System.Timers.Timer(downloadTimeoutMilliSeconds);

            timer.Elapsed += SetTimeoutElasped;
            timer.Start();

            while (!isTimeout && !isJobDone)
            {
                // wait for the first of the above to occur
                System.Threading.Thread.Sleep(1000);
            }

            timer.Close();

            if (isTimeout)
            {
                return(new ReviewDownloadResponse
                {
                    FirstFewReviews = theReviews.Take(20).ToList(),
                    ReviewSource = reviewService.Name
                });
            }

            // Generate the CSV
            // put the link in the response
            if (errorMessage != null)
            {
                return(new ReviewDownloadResponse
                {
                    ErrorDisplayMessage = errorMessage
                });
            }

            reviewsFileUrl = GenerateAndSaveReviews(theReviews, reviewService.Name);
            return(new ReviewDownloadResponse
            {
                FirstFewReviews = theReviews.Take(20).ToList(),
                ReviewsFileUrl = reviewsFileUrl,
                ReviewSource = reviewService.Name
            });
        }