Ejemplo n.º 1
0
        public MemoryStream GetReportData(ReportType reportType, DateTime?startDate = null, DateTime?endDate = null)
        {
            RequestReportRequest requestReportRequest = new RequestReportRequest()
                                                        .WithMarketplaceIdList(new IdList {
                Id = new List <string> {
                    m_marketPlaceId
                }
            })
                                                        .WithReportType(reportType.ToString())
                                                        .WithMerchant(m_sellerId)
                                                        .WithReportOptions("true");

            if (startDate.HasValue)
            {
                requestReportRequest.WithStartDate(startDate.Value);
            }

            if (endDate.HasValue)
            {
                requestReportRequest.WithEndDate(endDate.Value);
            }

            Func <MemoryStream> getMemoryStream = () =>
            {
                RequestReportResponse requestReportResponse = m_service.RequestReport(requestReportRequest);
                Func <string>         getReportRequestId    = () =>
                {
                    ReportRequestInfo reportRequestInfo = requestReportResponse.RequestReportResult.ReportRequestInfo;

                    if (reportRequestInfo.ReportRequestId == null)
                    {
                        throw new RetryException("Need to wait for ReportRequestId to become not null in the getReportRequestId");
                    }

                    return(reportRequestInfo.ReportRequestId);
                };

                string reportRequestId = getReportRequestId.Retry(12, 5000);

                if (reportRequestId == null)
                {
                    throw new QuitException("reportRequestId is null after getReportRequestId");
                }

                GetReportRequestListRequest reportRequestList = new GetReportRequestListRequest()
                                                                .WithMerchant(m_sellerId)
                                                                .WithReportRequestIdList(new IdList {
                    Id = new List <string> {
                        reportRequestId
                    }
                });

                Func <string> getGeneratedReportId = () =>
                {
                    string generatedReportId;

                    try
                    {
                        GetReportRequestListResponse reportRequestListResponse = m_service.GetReportRequestList(reportRequestList);
                        ReportRequestInfo            reportRequestInfo         = reportRequestListResponse.GetReportRequestListResult.ReportRequestInfo
                                                                                 .FirstOrDefault(w => w.ReportRequestId == reportRequestId);

                        if (reportRequestInfo == null)
                        {
                            throw new RetryException("reportRequestInfo is null in getReport");
                        }

                        if (reportRequestInfo.ReportProcessingStatus != "_SUBMITTED_" && reportRequestInfo.ReportProcessingStatus != "_IN_PROGRESS_")
                        {
                            if (reportRequestInfo.GeneratedReportId != null)
                            {
                                generatedReportId = reportRequestInfo.GeneratedReportId;
                            }
                            else
                            {
                                throw new QuitException("generatedReportId was null in waitForReportToProcess");
                            }
                        }
                        else
                        {
                            throw new RetryException("Had to wait for report to finish processing in waitForReportToProcess");
                        }
                    }
                    catch (MarketplaceWebServiceException)
                    {
                        // Caused by throttling, lets wait longer.
                        Thread.Sleep(60000);
                        throw new RetryException("Throttled due to MarketplaceWebServiceException.");
                    }

                    return(generatedReportId);
                };

                string returnedGeneratedReportId = getGeneratedReportId.Retry(30, 30000);

                if (returnedGeneratedReportId == null)
                {
                    throw new QuitException("reportId was null, from getReport");
                }

                MemoryStream returnedMemoryStream = null;

                try
                {
                    MemoryStream     memoryStream  = new MemoryStream();
                    GetReportRequest reportRequest = new GetReportRequest {
                        Merchant = m_sellerId, ReportId = returnedGeneratedReportId, Report = memoryStream
                    };
                    GetReportResponse response = m_service.GetReport(reportRequest);

                    string md5String = response.GetReportResult.ContentMD5;

                    if (!md5String.Equals(memoryStream.ComputeMd5(), StringComparison.InvariantCulture))
                    {
                        throw new QuitException("contentMd5 did not match!");
                    }

                    returnedMemoryStream = memoryStream;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

                if (returnedMemoryStream == null)
                {
                    throw new QuitException("returnedMemoryStream was null.");
                }

                return(returnedMemoryStream);
            };

            return(getMemoryStream.Retry(30, 180000, true));
        }