Ejemplo n.º 1
0
		public void RequestReportTest()
		{

			MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
			config.ServiceURL = serviceURL;

			MarketplaceWebService.MarketplaceWebService service =
				new MarketplaceWebServiceClient(
					creds.AccessKey,
					creds.SecretKey,
					appName,
					appVersion,
					config);

			string reportType = "_GET_FLAT_FILE_ORDERS_DATA_";

			RequestReportRequest request = new RequestReportRequest();
			request.ReportType = reportType;
			request.Merchant = sellerId;
			request.MWSAuthToken = mWSAuthToken; // Optional
												 //@TODO: set additional request parameters here
			RequestReportResponse response = service.RequestReport(request);

			Assert.IsTrue(response.RequestReportResult.ReportRequestInfo.ReportType == reportType);
			Console.WriteLine("GeneratedReportId: " + response.RequestReportResult.ReportRequestInfo.GeneratedReportId);
		}
Ejemplo n.º 2
0
        private void RequestReports(RequestsListInfo reportRequestList)
        {
            var marketplaceIdList = new IdList {
                Id = reportRequestList.MarketPlaceId
            };
            var access = reportRequestList.Access;

            foreach (var requestInfo in reportRequestList)
            {
                var reportType = requestInfo.Name;

                var requestReportRequest = new RequestReportRequest {
                    MarketplaceIdList = marketplaceIdList,
                    Merchant          = reportRequestList.UserId,
                    ReportOptions     = "ShowSalesChannel=true",
                    ReportType        = reportType
                };

                if (reportRequestList.StartDate.HasValue)
                {
                    requestReportRequest.StartDate = reportRequestList.StartDate.Value.ToUniversalTime();
                }

                if (reportRequestList.EndDate.HasValue)
                {
                    requestReportRequest.EndDate = reportRequestList.EndDate.Value.ToUniversalTime();
                }

                RequestInfo info = requestInfo;

                var resp = AmazonWaitBeforeRetryHelper.DoServiceAction(
                    reportRequestList.ErrorRetryingInfo,
                    RequestGetReportTrapForThrottling,
                    reportType,
                    access,
                    reportRequestList.RequestsCounter,
                    () => _Service.RequestReport(requestReportRequest),
                    "RequestReport");

                if (resp != null && resp.IsSetRequestReportResult())
                {
                    info.SetId(resp.RequestReportResult.ReportRequestInfo.ReportRequestId);
                }
            }
        }
Ejemplo n.º 3
0
        public void RefreshListings(ERPContext context, string accountName)
        {
            try
            {
                RequestReportRequest request = new RequestReportRequest();
                request.MWSAuthToken = mwsAuthTokenDictionary[accountName];
                request.Merchant     = sellerIdDictionary[accountName];
                request.ReportType   = "_GET_FLAT_FILE_OPEN_LISTINGS_DATA_";

                RequestReportResponse response = mwsClient.RequestReport(request);
                string requestId = response.RequestReportResult.ReportRequestInfo.ReportRequestId;

                int  count = 0;
                bool done  = false;
                while (count < 100 && !done)
                {
                    Thread.Sleep(60000);
                    GetReportRequestListRequest reportListRequest = new GetReportRequestListRequest();
                    reportListRequest.MWSAuthToken        = mwsAuthTokenDictionary[accountName];
                    reportListRequest.Merchant            = sellerIdDictionary[accountName];
                    reportListRequest.ReportRequestIdList = new IdList();
                    reportListRequest.ReportRequestIdList.Id.Add(requestId);
                    GetReportRequestListResponse reportListResponse = mwsClient.GetReportRequestList(reportListRequest);
                    foreach (ReportRequestInfo info in reportListResponse.GetReportRequestListResult.ReportRequestInfo)
                    {
                        if (info.ReportRequestId == requestId)
                        {
                            if (info.ReportProcessingStatus == "_DONE_")
                            {
                                done = true;
                                GetInventoryReport(context, accountName, info.GeneratedReportId);
                            }
                        }
                    }
                    count++;
                }
            }
            catch (Exception e)
            {
                m_logger.Error(e.Message);
            }
        }
Ejemplo n.º 4
0
        private RequestReportResponse InvokeRequestReport(PXGraph graph, ReportParameters reportParams)
        {
            RequestReportResponse reqReportResponse = new RequestReportResponse();

            try
            {
                RequestReportRequest request          = new RequestReportRequest();
                DateTime?            businessDateTime = PX.Common.PXTimeZoneInfo.Now;
                request.Merchant     = reportParams.objSOAmazonSetup.SellerId;
                request.MWSAuthToken = reportParams.objSOAmazonSetup.AuthToken;
                request.ReportType   = SOConstants.amazonFulfilledShipmentData;
                request.StartDate    = Convert.ToDateTime(reportParams.fromDate);
                request.EndDate      = Convert.ToDateTime(reportParams.toDate).Date >= businessDateTime.Value.Date ? businessDateTime.Value.AddMinutes(-2) : Convert.ToDateTime(reportParams.toDate).AddDays(1);
                reqReportResponse    = clientReports.RequestReport(request);
            }
            catch (Exception ex)
            {
                if (ex is MarketplaceWebServiceException)
                {
                    MarketplaceWebServiceException exception = ex as MarketplaceWebServiceException;
                    if (!string.IsNullOrEmpty(exception.ErrorCode) && exception.ErrorCode.ToLower().Trim() == SOMessages.requestThrottled)
                    {
                        Thread.Sleep(SOHelper.DelayProcess(graph, SOConstants.apiRequestReport));
                        reqReportResponse = InvokeRequestReport(graph, reportParams);
                    }
                    else
                    {
                        throw new PXException(!string.IsNullOrEmpty(ex.Message) ? ex.Message :
                                              ex.InnerException != null && ex.InnerException.InnerException != null ? ex.InnerException.InnerException.Message
                             : SOConstants.exceptionIsEmpty);
                    }
                }
                else
                {
                    throw new PXException(!string.IsNullOrEmpty(ex.Message) ? ex.Message :
                                          ex.InnerException != null && ex.InnerException.InnerException != null ? ex.InnerException.InnerException.Message
                             : SOConstants.exceptionIsEmpty);
                }
            }
            return(reqReportResponse);
        }
Ejemplo n.º 5
0
        public RequestReportResult RequestReport(string reportType, DateTime startDate, DateTime endDate, string reportOptions = "")
        {
            if (string.IsNullOrWhiteSpace(reportType))
            {
                throw new ArgumentNullException("reportType is empty");
            }

            nLogger.Info(string.Format("RequestReport start for reportType: {0}, startDate: {1}, endDate: {2}", reportType, startDate, endDate));

            RequestReportRequest request = new RequestReportRequest();

            request.ReportType    = reportType;
            request.Merchant      = serviceContext.SellerId;;
            request.MWSAuthToken  = serviceContext.MwsAuthToken;;             // Optional
            request.StartDate     = startDate;
            request.EndDate       = endDate;
            request.ReportOptions = reportOptions;
            RequestReportResponse response = service.RequestReport(request);

            return(response.RequestReportResult);
        }
Ejemplo n.º 6
0
        public ReportRequestInfo RequestReport(EReportType ReportType, DateTime StartDate, DateTime EndDate)
        {
            this.Throttle();
            AmazonCredential            Credentials = this.GetCredential();
            MarketplaceWebServiceClient Client      = this.GetClient(Credentials);

            RequestReportRequest Request = new RequestReportRequest()
            {
                Merchant   = Credentials.MerchantID,
                ReportType = EnumStringHandler.GetEnumString <EReportType>(ReportType)
            };

            if (StartDate != default(DateTime))
            {
                Request.StartDate = StartDate;
            }
            if (EndDate != default(DateTime))
            {
                Request.EndDate = EndDate;
            }

            RequestReportResponse Response = Client.RequestReport(Request);

            if (Response.IsSetRequestReportResult())
            {
                RequestReportResult Result = Response.RequestReportResult;
                if (Result.IsSetReportRequestInfo())
                {
                    return(Result.ReportRequestInfo);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 7
0
        public string DoRequestReport(string reportType, DateTime startDate)
        {
            var request = new RequestReportRequest
            {
                Merchant   = "A12HYFEDED6DEW",
                StartDate  = startDate,
                EndDate    = DateTime.Now.AddDays(-2),
                ReportType = reportType
            };

            try
            {
                var response          = _amazonClient.RequestReport(request);
                var reportRequestInfo = response.RequestReportResult.ReportRequestInfo;

                return(reportRequestInfo.ReportRequestId);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Ejemplo n.º 8
0
        public string RequestReport(string Type, DateTime From, DateTime To)
        {
            try
            {
                RequestReportRequest requestReport = new RequestReportRequest();
#pragma warning disable CS0618 // Type or member is obsolete
                requestReport.Marketplace = api.MarketplaceId;
#pragma warning restore CS0618 // Type or member is obsolete
                requestReport.Merchant = api.Merchant;

                requestReport.StartDate = From;
                requestReport.EndDate   = To;

                requestReport.ReportOptions = "true";

                IdList idList = new IdList();
                idList.Id = new List <string>()
                {
                    api.MarketplaceId
                };
                requestReport.MarketplaceIdList = idList;

                requestReport.ReportType = Type;

                MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
                config.ServiceURL = webService.SERVICE_URL;
                config.WithUserAgent(webService.USER_AGENT);

                MarketplaceWebServiceClient client = new MarketplaceWebServiceClient(api.AccessKey, api.SecretKey, config);

                return(InvokeRequestReport(client, requestReport));
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
        /// <summary>
        /// returns the number of feeds matching all of the specified criteria
        ///
        /// </summary>
        /// <param name="service">Instance of MarketplaceWebService service</param>
        /// <param name="request">GetFeedSubmissionCountRequest request</param>
        public static void InvokeRequestReport(MarketplaceWebService service, RequestReportRequest request)
        {
            try
            {
                RequestReportResponse response = service.RequestReport(request);


                Console.WriteLine("Service Response");
                Console.WriteLine("=============================================================================");
                Console.WriteLine();

                Console.WriteLine("        RequestReportResponse");

                if (response.IsSetRequestReportResult())
                {
                    Console.WriteLine("            RequestReportResult");
                    RequestReportResult requestReportResult = response.RequestReportResult;
                    ReportRequestInfo   reportRequestInfo   = requestReportResult.ReportRequestInfo;
                    Console.WriteLine("                  ReportRequestInfo");

                    if (reportRequestInfo.IsSetReportProcessingStatus())
                    {
                        Console.WriteLine("               ReportProcessingStatus");
                        Console.WriteLine("                                  {0}", reportRequestInfo.ReportProcessingStatus);
                    }
                    if (reportRequestInfo.IsSetReportRequestId())
                    {
                        Console.WriteLine("                      ReportRequestId");
                        Console.WriteLine("                                  {0}", reportRequestInfo.ReportRequestId);
                    }
                    if (reportRequestInfo.IsSetReportType())
                    {
                        Console.WriteLine("                           ReportType");
                        Console.WriteLine("                                  {0}", reportRequestInfo.ReportType);
                    }
                    if (reportRequestInfo.IsSetStartDate())
                    {
                        Console.WriteLine("                            StartDate");
                        Console.WriteLine("                                  {0}", reportRequestInfo.StartDate);
                    }
                    if (reportRequestInfo.IsSetEndDate())
                    {
                        Console.WriteLine("                              EndDate");
                        Console.WriteLine("                                  {0}", reportRequestInfo.EndDate);
                    }
                    if (reportRequestInfo.IsSetSubmittedDate())
                    {
                        Console.WriteLine("                        SubmittedDate");
                        Console.WriteLine("                                  {0}", reportRequestInfo.SubmittedDate);
                    }
                }
                if (response.IsSetResponseMetadata())
                {
                    Console.WriteLine("            ResponseMetadata");
                    ResponseMetadata responseMetadata = response.ResponseMetadata;
                    if (responseMetadata.IsSetRequestId())
                    {
                        Console.WriteLine("                RequestId");
                        Console.WriteLine("                    {0}", responseMetadata.RequestId);
                    }
                }

                Console.WriteLine("            ResponseHeaderMetadata");
                Console.WriteLine("                RequestId");
                Console.WriteLine("                    " + response.ResponseHeaderMetadata.RequestId);
                Console.WriteLine("                ResponseContext");
                Console.WriteLine("                    " + response.ResponseHeaderMetadata.ResponseContext);
                Console.WriteLine("                Timestamp");
                Console.WriteLine("                    " + response.ResponseHeaderMetadata.Timestamp);
            }
            catch (MarketplaceWebServiceException ex)
            {
                Console.WriteLine("Caught Exception: " + ex.Message);
                Console.WriteLine("Response Status Code: " + ex.StatusCode);
                Console.WriteLine("Error Code: " + ex.ErrorCode);
                Console.WriteLine("Error Type: " + ex.ErrorType);
                Console.WriteLine("Request ID: " + ex.RequestId);
                Console.WriteLine("XML: " + ex.XML);
                Console.WriteLine("ResponseHeaderMetadata: " + ex.ResponseHeaderMetadata);
            }
        }
Ejemplo n.º 10
0
        static async Task GetBrowseTreeReport()
        {
            void PrintTreeRecursively(BrowseTree tree, BrowseTreeNode node, int currentDepth)
            {
                var prefix = new string(Enumerable.Repeat('\t', currentDepth).ToArray());

                Console.WriteLine($"{prefix}{node.Name}");

                if (node.HasChildren)
                {
                    var childNodes = tree.Nodes.Where(x => node.ChildNodes.Ids.Contains(x.Id));
                    foreach (var childNode in childNodes)
                    {
                        PrintTreeRecursively(tree, childNode, currentDepth + 1);
                    }
                }
            }

            var channel = AmazonMWSChannelFactory.Create(ClientOptions);
            var subject = channel.CreateReportsClient();

            Console.WriteLine("Requesting report");
            var reportRequestResponse = await subject.RequestReport(RequestReportRequest.BrowseTree(rootNodesOnly: RootNodesOnly, browseNodeId: browseNodeId));

            reportRequestResponse.EnsureSuccess();
            Console.WriteLine("Requested report");

            var reportRequestInfo = reportRequestResponse.Result.ReportRequestInfo;

            while (!reportRequestInfo.IsComplete)
            {
                //   await Task.Delay(1000);
                Console.WriteLine("Checking if the report is available...");
                var reportRequestListResponse = await subject.GetReportRequestList(new GetReportRequestListRequest { ReportRequestIds = new[] { reportRequestInfo.ReportRequestId } });

                if (reportRequestListResponse.Error?.Code == ErrorCode.RequestThrottled)
                {
                    Console.WriteLine("Request throttled received, taking a break for 45 secs...");
                    await Task.Delay(TimeSpan.FromSeconds(45));
                }
                else
                {
                    reportRequestListResponse.EnsureSuccess();
                    reportRequestInfo = reportRequestListResponse.Result.ReportRequestInformations.Single();
                }
            }

            Console.WriteLine($"Report available with id {reportRequestInfo.GeneratedReportId}, fetching content (this may take a few minutes)...");
            var reportResponse = await subject.GetReport(reportRequestInfo.GeneratedReportId);

            reportResponse.EnsureSuccess();
            var browseTree = await reportResponse.ReadXmlAsAsync <BrowseTree>();

            Console.WriteLine("Report fetched");
            Console.WriteLine();
            Console.WriteLine();


            foreach (var rootNode in browseTree.Nodes.Where(x => string.IsNullOrEmpty(x.BrowsePathById)))
            {
                PrintTreeRecursively(browseTree, rootNode, 0);
            }
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Request Report
 /// </summary>
 /// <param name="request">Request Report  request</param>
 /// <returns>Request Report  Response from the service</returns>
 /// <remarks>
 /// requests the generation of a report
 ///
 /// </remarks>
 public async Task <RequestReportResponse> RequestReport(RequestReportRequest request)
 {
     return(await Invoke <RequestReportResponse>("RequestReportResponse.xml"));
 }
Ejemplo n.º 12
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));
        }
Ejemplo n.º 13
0
        public void GetReports(string reportType)
        {
            try
            {
                MarketplaceWebServiceClient service = new MarketplaceWebServiceClient(accessKeyId, secretAccessKey, applicationName, applicationVersion, config);

                RequestReportRequest reportRequestRequest = new RequestReportRequest();
                reportRequestRequest.Merchant          = sellerId;
                reportRequestRequest.MWSAuthToken      = mwsAuthToken;
                reportRequestRequest.MarketplaceIdList = new IdList();
                reportRequestRequest.MarketplaceIdList.Id.Add("ATVPDKIKX0DER");
                reportRequestRequest.ReportType = "_GET_FLAT_FILE_OFFAMAZONPAYMENTS_SANDBOX_SETTLEMENT_DATA_";

                // you can change ReportType here:
                //http://docs.developer.amazonservices.com/en_IN/reports/Reports_ReportType.html


                RequestReportResponse requestResponse = service.RequestReport(reportRequestRequest);
                IdList lstRequestID = new IdList();
                lstRequestID.Id.Add(requestResponse.RequestReportResult.ReportRequestInfo.ReportRequestId);

                GetReportRequestListRequest reportRequestListRequest = new GetReportRequestListRequest();
                reportRequestListRequest.Merchant            = sellerId;
                reportRequestListRequest.ReportRequestIdList = lstRequestID;
                reportRequestListRequest.MWSAuthToken        = mwsAuthToken;

                List <ReportRequestInfo> myListzz = new List <ReportRequestInfo>();

                GetReportRequestListResponse reportRequestListResponse = new GetReportRequestListResponse();
                reportRequestListResponse = service.GetReportRequestList(reportRequestListRequest);
                GetReportRequestListResult reportRequestListResult = new GetReportRequestListResult();
                reportRequestListResult = reportRequestListResponse.GetReportRequestListResult;
                myListzz = reportRequestListResult.ReportRequestInfo;

                if (myListzz.Count > 0)
                {
                    while (myListzz[0].ReportProcessingStatus.ToString() != "_DONE_")
                    {
                        Console.WriteLine("Waiting for Report");
                        Thread.Sleep(60000);
                        reportRequestListResponse =
                            service.GetReportRequestList(reportRequestListRequest);
                        reportRequestListResult =
                            reportRequestListResponse.GetReportRequestListResult;
                        myListzz = reportRequestListResult.ReportRequestInfo;
                    }
                }



                //if (myListzz[0].GeneratedReportId == null) {
                //    GetReportListRequest reportListRequest = new GetReportListRequest();
                //    reportListRequest.Merchant = sellerId;
                //    reportListRequest.MWSAuthToken = mwsAuthToken;
                //    reportListRequest.ReportRequestIdList = lstRequestID;
                //    reportListRequest.ReportTypeList = new TypeList();
                //    reportListRequest.ReportTypeList.Type = new List<string>() { "_GET_MERCHANT_LISTINGS_ALL_DATA_" };
                //    var reportList = service.GetReportList(reportListRequest);

                //    if (myListzz.Count > 0)
                //    {
                //        GetReportRequest reportRequest = new GetReportRequest();
                //        reportRequest.Merchant = sellerId;
                //        reportRequest.MWSAuthToken = mwsAuthToken;
                //        reportRequest.ReportId = myListzz[0].GeneratedReportId;

                //        var report = service.GetReport(reportRequest);
                //    }
                //}



                //if (myListzz.Count > 0)
                //{
                //    while (myListzz[0].ReportProcessingStatus.ToString() != "_DONE_")
                //    {
                //        Console.WriteLine("Waiting for Report");
                //        //Thread.Sleep(61000);
                //        reportRequestListResponse =
                //        service.GetReportRequestList(reportRequestListRequest);
                //        reportRequestListResult =
                //        reportRequestListResponse.GetReportRequestListResult;
                //        myListzz = reportRequestListResult.ReportRequestInfo;
                //    }


                //    if (myListzz[0].GeneratedReportId != null)
                //    {
                //        GetReportRequest reportRequest = new GetReportRequest();
                //        reportRequest.Merchant = sellerId;

                //        String source = "C:\\myreport.txt";
                //        reportRequest.ReportId = myListzz[0].GeneratedReportId;
                //        // reportRequest.Report = File.Open(source, FileMode.Create,  FileAccess.ReadWrite);
                //        service.GetReport(reportRequest);
                //    }
                //}
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
            }
        }
Ejemplo n.º 14
0
        public void RequestReportFromAmazon(IReportRequestEntryService reportRequestService, ReportRequestEntry reportRequestEntry)
        {
            var missingInformationExceptionMessage = "Cannot request report from amazon due to missing report request information";

            if (reportRequestEntry?.ReportRequestData == null)
            {
                throw new ArgumentNullException($"{missingInformationExceptionMessage}: Report request data is missing");
            }
            if (string.IsNullOrEmpty(reportRequestEntry?.ReportType))
            {
                throw new ArgumentException($"{missingInformationExceptionMessage}: Report Type is missing");
            }

            var reportRequestData = reportRequestEntry.GetPropertiesContainer();

            _logger.Debug($"Attempting to request the next report in queue from Amazon: {reportRequestEntry.EntryIdentityDescription}.");

            var reportRequest = new RequestReportRequest
            {
                Merchant   = reportRequestEntry.MerchantId,
                ReportType = reportRequestEntry.ReportType,
            };

            if (!string.IsNullOrEmpty(_mWSAuthToken))
            {
                reportRequest.MWSAuthToken = _mWSAuthToken;
            }

            if (reportRequestData.MarketplaceIdList != null)
            {
                reportRequest.MarketplaceIdList = new IdList {
                    Id = reportRequestData.MarketplaceIdList.ToList()
                }
            }
            ;
            if (reportRequestData.StartDate.HasValue)
            {
                reportRequest.StartDate = reportRequestData.StartDate.Value;
            }
            if (reportRequestData.EndDate.HasValue)
            {
                reportRequest.EndDate = reportRequestData.EndDate.Value;
            }
            if (!string.IsNullOrEmpty(reportRequestData.ReportOptions))
            {
                reportRequest.ReportOptions = reportRequestData.ReportOptions;
            }

            try
            {
                reportRequestService.Unlock(reportRequestEntry, "Unlocking single report request entry - attempt to request report from amazon has been completed.");
                reportRequestService.Update(reportRequestEntry);

                var reportResponse = _marketplaceWebServiceClient.RequestReport(reportRequest);
                reportRequestEntry.LastAmazonRequestDate = DateTime.UtcNow;
                reportRequestEntry.RequestReportId       = reportResponse?.RequestReportResult?.ReportRequestInfo?.ReportRequestId;

                var requestId = reportResponse?.ResponseHeaderMetadata?.RequestId ?? "unknown";
                var timestamp = reportResponse?.ResponseHeaderMetadata?.Timestamp ?? "unknown";

                if (string.IsNullOrEmpty(reportRequestEntry.RequestReportId))
                {
                    reportRequestEntry.ReportRequestRetryCount++;
                    _logger.Warn($"RequestReport did not generate a ReportRequestId for {reportRequestEntry.EntryIdentityDescription}. Report request will be retried. ReportRequestRetryCount is now : {reportRequestEntry.ReportRequestRetryCount}.",
                                 new RequestInfo(timestamp, requestId));
                }
                else
                {
                    reportRequestEntry.ReportRequestRetryCount = 0;
                    _logger.Info($"AmazonMWS RequestReport succeeded for {reportRequestEntry.EntryIdentityDescription}. ReportRequestId:'{reportRequestEntry.RequestReportId}'.",
                                 new RequestInfo(timestamp, requestId));
                }
            }
            catch (MarketplaceWebServiceException e) when(e.StatusCode == HttpStatusCode.BadRequest && IsAmazonErrorCodeFatal(e.ErrorCode))
            {
                reportRequestService.Delete(reportRequestEntry);
                _logger.Error($"AmazonMWS RequestReport failed for {reportRequestEntry.EntryIdentityDescription}. The entry will now be removed from queue", e);
            }
            catch (MarketplaceWebServiceException e) when(IsAmazonErrorCodeNonFatal(e.ErrorCode))
            {
                reportRequestEntry.ReportRequestRetryCount++;
                reportRequestEntry.LastAmazonRequestDate = DateTime.UtcNow;
                _logger.Warn($"AmazonMWS RequestReport failed for {reportRequestEntry.EntryIdentityDescription}. Report request will be retried. ReportRequestRetryCount is now : {reportRequestEntry.ReportRequestRetryCount}.", e);
            }
            catch (Exception e)
            {
                reportRequestEntry.ReportRequestRetryCount++;
                reportRequestEntry.LastAmazonRequestDate = DateTime.UtcNow;
                _logger.Warn($"AmazonMWS RequestReport failed for {reportRequestEntry.EntryIdentityDescription}. Report request will be retried. ReportRequestRetryCount is now : {reportRequestEntry.ReportRequestRetryCount}.", e);
            }
            finally
            {
                reportRequestService.SaveChanges();
            }
        }
Ejemplo n.º 15
0
        private static string AdlsAccountName   = "";                                // Name of the Azure Data Lake Store

        static void Main(string[] args)
        {
            // Fetching Prerequisites Data from Azure SQL Server
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                // Fetching data from Report Config Table
                SqlCommand cmd = new SqlCommand("USE CommerceLabsDW;OPEN SYMMETRIC KEY SymmetricKey1 DECRYPTION BY CERTIFICATE Certificate1; SELECT CONVERT(varchar(max), DECRYPTBYKEY(SellerCentral_LoginID)), CONVERT(varchar(max), DECRYPTBYKEY(SellerCentral_LoginPWD)), CONVERT(varchar(max), DECRYPTBYKEY(ADS_RefreshToken)), CONVERT(varchar(max), DECRYPTBYKEY(ADS_ClientID)), CONVERT(varchar(max), DECRYPTBYKEY(ADS_ClientSecret)), CONVERT(varchar(max), DECRYPTBYKEY(ADS_Scope)), CONVERT(varchar(max), DECRYPTBYKEY(WP_Authorization)), CONVERT(varchar(max), DECRYPTBYKEY(MWS_AccessID)), CONVERT(varchar(max), DECRYPTBYKEY(MWS_SecretKey)), CONVERT(varchar(max), DECRYPTBYKEY(MWS_MerchantID)), CONVERT(varchar(max), DECRYPTBYKEY(MWS_AuthToken)), CONVERT(varchar(max), DECRYPTBYKEY(Azure_ClientID)), CONVERT(varchar(max), DECRYPTBYKEY(Azure_ClientSecret)), CONVERT(varchar(max), DECRYPTBYKEY(Azure_TenantID)), CONVERT(varchar(max), DECRYPTBYKEY(Azure_DataLakeName)) FROM stage.rpt_ConfigTable WHERE ID = 1; ", con);
                //Opening the Connection
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    SC_LoginID        = reader.GetString(0);
                    SC_LoginPwd       = reader.GetString(1);
                    ADS_RefreshToken  = reader.GetString(2);
                    ADS_ClientID      = reader.GetString(3);
                    ADS_ClientSecret  = reader.GetString(4);
                    ADS_Scope         = reader.GetString(5);
                    WP_Authorization  = reader.GetString(6);
                    MWS_AccessID      = reader.GetString(7);
                    MWS_SecretKey     = reader.GetString(8);
                    MWS_MerchantID    = reader.GetString(9);
                    MWS_AuthToken     = reader.GetString(10);
                    AzureClientId     = reader.GetString(11);
                    AzureClientSecret = reader.GetString(12);
                    AzureTenantId     = reader.GetString(13);
                    AdlsAccountName   = reader.GetString(14);
                }
                //Closing the Connection
                con.Close();
            }

            // Azure Data Lake Initialisation
            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
            var clientCredential = new ClientCredential(AzureClientId, AzureClientSecret);
            var creds            = ApplicationTokenProvider.LoginSilentAsync(AzureTenantId, clientCredential).Result;

            adlsFileSystemClient = new DataLakeStoreFileSystemManagementClient(creds);
            string Azuresource = "", Azuredestination = "";  // Azure Source and Destination Variable

            // Creating a Folder in the App Path
            var directoryFullPath = AppDomain.CurrentDomain.BaseDirectory + @"DownloadedReports\";    // Creating a Folder in Current Directory if not exists

            Directory.CreateDirectory(directoryFullPath);

            // Web Driver Object
            IWebDriver driver;

            // Setting the needed Chrome Options
            ChromeOptions options = new ChromeOptions();

            options.AddUserProfilePreference("download.default_directory", directoryFullPath); // Setting the default directory for the report to be downloaded
            options.AddUserProfilePreference("intl.accept_languages", "en-us");                // Setting the language as English
            // options.AddArgument("--headless"); // Setting headless browser option

            driver = new ChromeDriver(options);  // Assigning all the above options to Chrome Driver
            Console.WriteLine("Started");

            // Amazon Seller Central Login Page
            driver.Navigate().GoToUrl("https://sellercentral.amazon.com/");
            driver.FindElement(By.ClassName("secondary")).Click();
            driver.FindElement(By.Id("ap_email")).SendKeys(SC_LoginID);
            driver.FindElement(By.Id("ap_password")).SendKeys(SC_LoginPwd);
            driver.FindElement(By.Id("signInSubmit")).Click();

            /*// Generating OTP
             * var otpKeyStr = "WGHSYZEE2RJOO6OFMCBMB6HKNHDZRADH4TK2FDJ77YWQ5YFRWDBQ"; //  2FA secret key.
             * var otpKeyBytes = Base32Encoding.ToBytes(otpKeyStr);
             * var totp = new Totp(otpKeyBytes);
             * var twoFactorCode = totp.ComputeTotp(DateTime.UtcNow); // 2FA code at this time!
             * Console.WriteLine(twoFactorCode);*/

            // OTP
            Console.WriteLine("Please enter the OTP : ");
            var input = Console.ReadLine();

            driver.FindElement(By.Id("auth-mfa-otpcode")).SendKeys(input);
            driver.FindElement(By.Id("auth-mfa-remember-device")).Click();
            driver.FindElement(By.Id("auth-signin-button")).Click();

            // ------------------ REPORT 1 ------------------ // ---- Automating Scraping Method -----
            // Downloading Business Report
            driver.Navigate().GoToUrl("https://sellercentral.amazon.com/gp/site-metrics/report.html#reportID=102%3ADetailSalesTrafficBySKU&runDate=&fromDate=&toDate=");
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1000);
            Thread.Sleep(1000);

            // Downloading Past 3 Days' Reports by looping through dates
            for (int i = -1; i > -4; i--)
            {
                // Looping through Dates and setting the Date Value
                var DateValue = DateTime.Now.AddDays(i).ToString("MM/dd/yyyy").Replace("-", "/");

                // Setting From Date
                IWebElement FromDateBox = driver.FindElement(By.Id("fromDate2"));
                FromDateBox.Clear();
                FromDateBox.SendKeys(DateValue + Keys.Enter);
                // Setting To Date
                IWebElement ToDateBox = driver.FindElement(By.Id("toDate2"));
                ToDateBox.Clear();
                ToDateBox.SendKeys(DateValue + Keys.Enter);

                // Downloading the Report
                Thread.Sleep(1000);
                driver.FindElement(By.Id("export")).Click();
                Thread.Sleep(1000);
                driver.FindElement(By.Id("downloadCSV")).Click();
                Thread.Sleep(1000);

                // Editing the Business Report CSV
                string BusRepfilePath = directoryFullPath + "BusinessReport-" + DateTime.Now.AddDays(-1).ToString("MM/dd/yy") + ".csv";
                Thread.Sleep(1000);

                // Transaction Date
                var Buscol1 = File.ReadLines(BusRepfilePath).Select((line, index) => index == 0 ? line + ", Transaction Date"
                                                                                             : line + "," + DateValue.Replace("/", "-")).ToList();
                File.WriteAllLines(BusRepfilePath, Buscol1);

                // Account
                var Buscol2 = File.ReadLines(BusRepfilePath).Select((line, index) => index == 0 ? line + ",Account"
                                                                                             : line + ",Meal Prep Haven").ToList();
                File.WriteAllLines(BusRepfilePath, Buscol2);

                // Country
                var Buscol3 = File.ReadLines(BusRepfilePath).Select((line, index) => index == 0 ? line + ",Country"
                                                                                             : line + ",US").ToList();
                File.WriteAllLines(BusRepfilePath, Buscol3);

                // Reference URL
                var Buscol4 = File.ReadLines(BusRepfilePath).Select((line, index) => index == 0 ? line + ",Reference"
                                                                                       : line + "," + driver.Url).ToList();
                File.WriteAllLines(BusRepfilePath, Buscol4);

                // Renaming the Report File
                File.Move(BusRepfilePath, directoryFullPath + "BusinessReport" + i + ".csv");
            }

            // Combining all the CSV Files into 1
            string BussourceFolder    = directoryFullPath;
            string BusdestinationFile = directoryFullPath + "BusinessReport-" + DateTime.Now.ToString("dd/MM/yyyy") + ".csv";

            // Matches the CSV files by searching the specified wildcard and combines them to single file
            string[]     filePaths = Directory.GetFiles(BussourceFolder, "BusinessReport-?.csv");
            StreamWriter fileDest  = new StreamWriter(BusdestinationFile, true);

            for (int j = 0; j < filePaths.Length; j++)
            {
                string file = filePaths[j];

                string[] lines = File.ReadAllLines(file);

                if (j > 0)
                {
                    lines = lines.Skip(1).ToArray(); // Skipping Header row for all except first file
                }

                foreach (string line in lines)
                {
                    fileDest.WriteLine(line);
                }
            }

            fileDest.Close();

            // Uploading Business Report to Azure Data Lake
            Azuresource      = directoryFullPath + @"BusinessReport-" + DateTime.Now.ToString("dd/MM/yyyy") + ".csv";
            Azuredestination = "Meal prep haven/CA_AO/Business Reports/" + "BusinessReport-" + DateTime.Now.ToString("dd/MM/yyyy") + ".csv";
            adlsFileSystemClient.FileSystem.UploadFile(AdlsAccountName, Azuresource, Azuredestination, 1, false, true);
            Console.WriteLine("Uploaded Business Report");

            // Deleting the Business Report Files
            Array.ForEach(Directory.GetFiles(directoryFullPath), File.Delete);


            // ------------------ REPORT 2 ------------------ // ---- Automating Scraping Method ----
            // Downloading Reserved Inventory Report
            driver.Navigate().GoToUrl("https://sellercentral.amazon.com/gp/ssof/reports/search.html#orderAscending=&recordType=ReserveBreakdown&noResultType=&merchantSku=&fnSku=&FnSkuXORMSku=&reimbursementId=&orderId=&genericOrderId=&asin=&lpn=&shipmentId=&problemType=ALL_DEFECT_TYPES&hazmatStatus=&inventoryEventTransactionType=&fulfillmentCenterId=&transactionItemId=&inventoryAdjustmentReasonGroup=&eventDateOption=1&fromDate=mm%2Fdd%2Fyyyy&toDate=mm%2Fdd%2Fyyyy&startDate=&endDate=&fromMonth=1&fromYear=2019&toMonth=1&toYear=2019&startMonth=&startYear=&endMonth=&endYear=&specificMonth=1&specificYear=2019");
            Thread.Sleep(1000);
            driver.FindElement(By.XPath("//*[@id=\"requestCsvTsvDownload\"]/tr[1]/td[2]/button")).Click();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1000);
            Thread.Sleep(10000);
            driver.FindElement(By.XPath("//*[@id=\"downloadArchive\"]/table/tbody/tr[1]/td[5]/a")).Click();

            // Editing the Reserved Inventory CSV
            // Appending Columns and Values in Existing CSV File
            string ResRepfilePath = "";

            foreach (string filename in Directory.GetFiles(directoryFullPath))
            {
                ResRepfilePath = directoryFullPath + Path.GetFileName(filename);  // Getting the File Name
            }

            // Account
            var Rescol1 = File.ReadLines(ResRepfilePath).Select((line, index) => index == 0 ? line + ",Account"
                                                                                         : line + ",Meal Prep Haven").ToList();

            File.WriteAllLines(ResRepfilePath, Rescol1);

            // Country
            var Rescol2 = File.ReadLines(ResRepfilePath).Select((line, index) => index == 0 ? line + ",Country"
                                                                                         : line + ",US").ToList();

            File.WriteAllLines(ResRepfilePath, Rescol2);

            // Reference URL
            var Rescol3 = File.ReadLines(ResRepfilePath).Select((line, index) => index == 0 ? line + ",Reference"
                                                                                         : line + "," + driver.Url).ToList();

            File.WriteAllLines(ResRepfilePath, Rescol3);

            // Transaction Date
            var Rescol4 = File.ReadLines(ResRepfilePath).Select((line, index) => index == 0 ? line + ", Transaction Date"
                                                                                         : line + "," + DateTime.Now.ToString("MM/dd/yyyy")).ToList();

            File.WriteAllLines(ResRepfilePath, Rescol4);

            // Renaming the Report File
            File.Move(ResRepfilePath, directoryFullPath + "ReservedInventoryReport.csv");

            // Uploading Reserved Inventory Report to Azure Data Lake
            Azuresource      = directoryFullPath + "ReservedInventoryReport.csv";
            Azuredestination = "Meal prep haven/CA_AO/Reserved Inventory Reports/" + "ReservedInventoryReport-" + DateTime.Now.ToString("dd/MM/yyyy") + ".csv";
            adlsFileSystemClient.FileSystem.UploadFile(AdlsAccountName, Azuresource, Azuredestination, 1, false, true);
            Console.WriteLine("Uploaded Reserved Inventory Report");

            // Deleting the Reserved Inventory Report File
            Array.ForEach(Directory.GetFiles(directoryFullPath), File.Delete);


            // ------------------ REPORT 3 ------------------ // ---- Automating Scraping Method ----
            // Downloading Managed Inventory Report
            driver.Navigate().GoToUrl("https://sellercentral.amazon.com/gp/ssof/reports/search.html?#orderAscending=&recordType=FBA_MYI_UNSUPPRESSED_INVENTORY&noResultType=&merchantSku=&fnSku=&FnSkuXORMSku=&reimbursementId=&orderId=&genericOrderId=&asin=&lpn=&shipmentId=&problemType=ALL_DEFECT_TYPES&hazmatStatus=&inventoryEventTransactionType=&fulfillmentCenterId=&transactionItemId=&inventoryAdjustmentReasonGroup=&eventDateOption=1&fromDate=mm%2Fdd%2Fyyyy&toDate=mm%2Fdd%2Fyyyy&startDate=&endDate=&fromMonth=1&fromYear=2019&toMonth=1&toYear=2019&startMonth=&startYear=&endMonth=&endYear=&specificMonth=1&specificYear=2019");
            Thread.Sleep(1000);
            driver.FindElement(By.XPath("//*[@id=\"requestCsvTsvDownload\"]/tr[1]/td[2]/button")).Click();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1000);
            Thread.Sleep(10000);
            driver.FindElement(By.XPath("//*[@id=\"downloadArchive\"]/table/tbody/tr[1]/td[5]/a")).Click();

            // Editing the Managed Inventory CSV
            // Appending Columns and Values in Existing CSV File
            string ManRepfilePath = "";

            foreach (string filename in Directory.GetFiles(directoryFullPath))
            {
                ManRepfilePath = directoryFullPath + Path.GetFileName(filename);  // Getting the File Name
            }

            // Account
            var Mancol1 = File.ReadLines(ManRepfilePath).Select((line, index) => index == 0 ? line + ",Account"
                                                                                            : line + ",Meal Prep Haven").ToList();

            File.WriteAllLines(ManRepfilePath, Mancol1);

            // Country
            var Mancol2 = File.ReadLines(ManRepfilePath).Select((line, index) => index == 0 ? line + ",Country"
                                                                                            : line + ",US").ToList();

            File.WriteAllLines(ManRepfilePath, Mancol2);

            // Reference URL
            var Mancol3 = File.ReadLines(ManRepfilePath).Select((line, index) => index == 0 ? line + ",Reference"
                                                                                            : line + "," + driver.Url).ToList();

            File.WriteAllLines(ManRepfilePath, Mancol3);

            // Transaction Date
            var Mancol4 = File.ReadLines(ManRepfilePath).Select((line, index) => index == 0 ? line + ", Transaction Date"
                                                                                         : line + "," + DateTime.Now.ToString("MM/dd/yyyy")).ToList();

            File.WriteAllLines(ManRepfilePath, Mancol4);

            // Renaming the Report File
            File.Move(ManRepfilePath, directoryFullPath + "ManagedInventoryReport.csv");

            // Uploading Reserved Inventory Report to Azure Data Lake
            Azuresource      = directoryFullPath + "ManagedInventoryReport.csv";
            Azuredestination = "Meal prep haven/CA_AO/Managed Inventory Reports/" + "ManagedInventoryReport-" + DateTime.Now.ToString("dd/MM/yyyy") + ".csv";
            adlsFileSystemClient.FileSystem.UploadFile(AdlsAccountName, Azuresource, Azuredestination, 1, false, true);
            Console.WriteLine("Uploaded Managed Inventory Report");

            // Deleting the Managed Inventory Report File
            Array.ForEach(Directory.GetFiles(directoryFullPath), File.Delete);


            // ------------------ REPORT 4 ------------------ // ---- Automating Scraping Method ----
            // Downloading Payment Report
            driver.Navigate().GoToUrl("https://sellercentral.amazon.com/payments/reports/custom/request?tbla_daterangereportstable=sort:%7B%22sortOrder%22%3A%22DESCENDING%22%7D;search:undefined;pagination:1;");
            Thread.Sleep(1000);
            driver.FindElement(By.XPath("//*[@id=\"drrGenerateReportButton\"]/span/input")).Click();
            driver.FindElement(By.XPath("//*[@id=\"drrReportTypeRadioTransaction\"]")).Click();
            driver.FindElement(By.XPath("//*[@id=\"drrReportRangeTypeRadioCustom\"]")).Click();
            driver.FindElement(By.XPath("//*[@id=\"drrFromDate\"]")).SendKeys(DateTime.Now.AddDays(-2).ToString("MM/dd/yyyy").Replace("-", "/") + Keys.Enter);
            driver.FindElement(By.XPath("//*[@id=\"drrToDate\"]")).SendKeys(DateTime.Now.AddDays(-2).ToString("MM/dd/yyyy").Replace("-", "/") + Keys.Enter);
            driver.FindElement(By.XPath("//*[@id=\"drrGenerateReportsGenerateButton\"]/span/input")).Click();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1000);
            driver.FindElement(By.ClassName("drrRefreshTable")).Click();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1000);
            Thread.Sleep(10000);
            driver.FindElement(By.XPath("//*[@id=\"downloadButton\"]")).Click();

            // Editing the Payment CSV
            // Appending Columns and Values in Existing CSV File
            string PayRepfilePath = "";

            foreach (string filename in Directory.GetFiles(directoryFullPath))
            {
                PayRepfilePath = directoryFullPath + Path.GetFileName(filename);  // Getting the File Name
            }

            // Account
            var Paycol1 = File.ReadLines(PayRepfilePath).Select((line, index) => index == 0 ? line + ",Account"
                                                                                            : line + ",Meal Prep Haven").ToList();

            File.WriteAllLines(PayRepfilePath, Paycol1);

            // Country
            var Paycol2 = File.ReadLines(PayRepfilePath).Select((line, index) => index == 0 ? line + ",Country"
                                                                                            : line + ",US").ToList();

            File.WriteAllLines(PayRepfilePath, Paycol2);

            // Reference URL
            var Paycol3 = File.ReadLines(PayRepfilePath).Select((line, index) => index == 0 ? line + ",Reference"
                                                                                            : line + ",https://sellercentral.amazon.com/payments/reports/custom/request?tbla_daterangereportstable=sort:%7B%22sortOrder%22%3A%22DESCENDING%22%7D;search:undefined;pagination:1;").ToList(); //+ driver.Url).ToList();

            File.WriteAllLines(PayRepfilePath, Paycol3);

            // Renaming the Report File
            File.Move(PayRepfilePath, directoryFullPath + "PaymentReport.csv");

            // Uploading Payment Report to Azure Data Lake
            Azuresource      = directoryFullPath + "PaymentReport.csv";
            Azuredestination = "Meal prep haven/CA_AO/Payment Reports/" + "PaymentReport-17-10-2019.csv"; //+ DateTime.Now.ToString("dd/MM/yyyy") + ".csv";
            adlsFileSystemClient.FileSystem.UploadFile(AdlsAccountName, Azuresource, Azuredestination, 1, false, true);
            Console.WriteLine("Uploaded Payment Report");

            // Deleting the Payment Report File
            Array.ForEach(Directory.GetFiles(directoryFullPath), File.Delete);


            // ------------------ REPORT 5 ------------------ // ---- Automating Scraping Method ----
            // Downloading Storage Fees Report
            driver.Navigate().GoToUrl("https://sellercentral.amazon.com/gp/ssof/reports/search.html#orderAscending=&recordType=STORAGE_FEE_CHARGES&noResultType=&merchantSku=&fnSku=&FnSkuXORMSku=&reimbursementId=&orderId=&genericOrderId=&asin=&lpn=&shipmentId=&problemType=ALL_DEFECT_TYPES&hazmatStatus=&inventoryEventTransactionType=&fulfillmentCenterId=&transactionItemId=&inventoryAdjustmentReasonGroup=&eventDateOption=1&fromDate=mm%2Fdd%2Fyyyy&toDate=mm%2Fdd%2Fyyyy&startDate=&endDate=&fromMonth=1&fromYear=2019&toMonth=1&toYear=2019&startMonth=&startYear=&endMonth=&endYear=&specificMonth=1&specificYear=2019");
            Thread.Sleep(1000);
            driver.FindElement(By.XPath("//*[@id=\"specificMonthDownload\"]/option[9]")).Click();
            driver.FindElement(By.XPath("//*[@id=\"specificYearDownload\"]/option[1]")).Click();
            driver.FindElement(By.XPath("//*[@id=\"requestCsvTsvDownload\"]/tr[1]/td[2]/button")).Click();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(4000);
            Thread.Sleep(10000);
            driver.FindElement(By.XPath("//*[@id=\"downloadArchive\"]/table/tbody/tr[1]/td[5]/a")).Click();

            // Editing the Storage Fees CSV
            // Appending Columns and Values in Existing CSV File
            string StoRepfilePath = "";

            foreach (string filename in Directory.GetFiles(directoryFullPath))
            {
                StoRepfilePath = directoryFullPath + Path.GetFileName(filename);  // Getting the File Name
            }

            // Account
            var Stocol1 = File.ReadLines(StoRepfilePath).Select((line, index) => index == 0 ? line + ",Account"
                                                                                            : line + ",Meal Prep Haven").ToList();

            File.WriteAllLines(StoRepfilePath, Stocol1);

            // Country
            var Stocol2 = File.ReadLines(StoRepfilePath).Select((line, index) => index == 0 ? line + ",Country"
                                                                                            : line + ",US").ToList();

            File.WriteAllLines(StoRepfilePath, Stocol2);

            // Reference URL
            var Stocol3 = File.ReadLines(StoRepfilePath).Select((line, index) => index == 0 ? line + ",Reference"
                                                                                            : line + "," + driver.Url).ToList();

            File.WriteAllLines(StoRepfilePath, Stocol3);

            // Transaction Date
            var Stocol4 = File.ReadLines(StoRepfilePath).Select((line, index) => index == 0 ? line + ", Transaction Date"
                                                                                         : line + "," + DateTime.Now.ToString("MM/dd/yyyy")).ToList();

            File.WriteAllLines(StoRepfilePath, Stocol4);

            // Renaming the Report File
            File.Move(StoRepfilePath, directoryFullPath + "StorageFeesReport.csv");

            // Uploading Storage Fees Report to Azure Data Lake
            Azuresource      = directoryFullPath + "StorageFeesReport.csv";
            Azuredestination = "Meal prep haven/CA_AO/Storage Fees Reports/" + "StorageFeesReport-" + DateTime.Now.ToString("dd/MM/yyyy") + ".csv";
            adlsFileSystemClient.FileSystem.UploadFile(AdlsAccountName, Azuresource, Azuredestination, 1, false, true);
            Console.WriteLine("Uploaded Storage Fees Report");

            // Deleting the Storage Fees Report File
            Array.ForEach(Directory.GetFiles(directoryFullPath), File.Delete);


            // ------------------ REPORT 6 ------------------ // ---- API Method ----
            // Downloading Amazon ADS Product Report through API
            // Refreshing Access Token
            var client1  = new RestClient("https://api.amazon.com/auth/o2/token");
            var request1 = new RestRequest(Method.POST);

            request1.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            request1.AddParameter("grant_type", "refresh_token");
            request1.AddParameter("refresh_token", ADS_RefreshToken);
            request1.AddParameter("client_id", ADS_ClientID);
            request1.AddParameter("client_secret", ADS_ClientSecret);
            IRestResponse response1       = client1.Execute(request1);
            dynamic       result1         = JsonConvert.DeserializeObject(response1.Content);
            string        ADS_AccessToken = result1.access_token;

            // Getting the Report ID
            var client2  = new RestClient("https://advertising-api.amazon.com/v2/sp/productAds/report");
            var request2 = new RestRequest(Method.POST);

            request2.AddHeader("Content-Type", "application/json");
            request2.AddHeader("Authorization", "Bearer " + ADS_AccessToken);
            request2.AddHeader("Amazon-Advertising-API-ClientId", ADS_ClientID);
            request2.AddHeader("Amazon-Advertising-API-Scope", ADS_Scope);
            request2.AddParameter("undefined", "{\"reportDate\":\"20190930\", \"metrics\":\"campaignName,campaignId,adGroupName,adGroupId,currency,asin,sku,impressions,clicks,cost,attributedConversions1d,attributedConversions7d,attributedConversions14d,attributedConversions30d,attributedConversions1dSameSKU,attributedConversions7dSameSKU,attributedConversions14dSameSKU,attributedConversions30dSameSKU,attributedUnitsOrdered1d,attributedUnitsOrdered7d,attributedUnitsOrdered14d,attributedUnitsOrdered30d,attributedSales1d,attributedSales7d,attributedSales14d,attributedSales30d,attributedSales1dSameSKU,attributedSales7dSameSKU,attributedSales14dSameSKU,attributedSales30dSameSKU\"}", ParameterType.RequestBody);
            IRestResponse response2 = client2.Execute(request2);
            dynamic       result2   = JsonConvert.DeserializeObject(response2.Content);
            string        ReportID  = result2.reportId;

            Thread.Sleep(3000);

            // Retrieving the Report Status
            var client3  = new RestClient("https://advertising-api.amazon.com/v2/reports/" + ReportID);
            var request3 = new RestRequest(Method.GET);

            request3.AddHeader("Content-Type", "application/json");
            request3.AddHeader("Authorization", "Bearer " + ADS_AccessToken);
            request3.AddHeader("Amazon-Advertising-API-ClientId", ADS_ClientID);
            request3.AddHeader("Amazon-Advertising-API-Scope", ADS_Scope);
            IRestResponse response3        = client3.Execute(request3);
            dynamic       result3          = JsonConvert.DeserializeObject(response3.Content);
            string        DownloadLocation = result3.location;

            if (result3.status == "SUCCESS")
            {
                // Retrieving Report Download URL
                var client4  = new RestClient(DownloadLocation);
                var request4 = new RestRequest(Method.GET);
                request4.AddHeader("Authorization", "Bearer " + ADS_AccessToken);
                request4.AddHeader("Amazon-Advertising-API-Scope", ADS_Scope);
                IRestResponse response4   = client4.Execute(request4);
                string        DownloadURL = response4.ResponseUri.ToString();

                // Downloading the Report
                driver.Navigate().GoToUrl(DownloadURL);
            }

            // Extracting .gz Zip file
            DirectoryInfo directorySelected = new DirectoryInfo(directoryFullPath);

            foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))
            {
                Decompress(fileToDecompress);
            }

            // Uploading Amazon ADS Product Report to Azure Data Lake
            Azuresource      = directoryFullPath + "ADSProductReport.json";
            Azuredestination = "Meal prep haven/CA_AO/Advertised Product Reports/" + "AmazonADSProductReport-" + DateTime.Now.ToString("dd/MM/yyyy") + ".json";
            adlsFileSystemClient.FileSystem.UploadFile(AdlsAccountName, Azuresource, Azuredestination, 1, false, true);
            Console.WriteLine("Uploaded Amazon ADS Product Report");

            // Deleting the Amazon ADS Product Report File
            Array.ForEach(Directory.GetFiles(directoryFullPath), File.Delete);


            // ------------------ REPORT 7 ------------------ // ---- API Method ----
            // Downloading WorldPack Report through API
            // Getting the Access Token
            var clientw1  = new RestClient("http://secure-wms.com/AuthServer/api/Token");
            var requestw1 = new RestRequest(Method.POST);

            requestw1.AddHeader("Content-Type", "application/json; charset=utf-8");
            requestw1.AddHeader("Accept", "application/json");
            requestw1.AddHeader("Authorization", "Basic " + WP_Authorization);
            requestw1.AddParameter("undefined", "{\"grant_type\": \"client_credentials\",\"tpl\": \"{1ddbea91-a4ff-4b42-a25d-81a25b8cb727}\",\"user_login_id\": \"759\"}", ParameterType.RequestBody);
            IRestResponse responsew1     = clientw1.Execute(requestw1);
            dynamic       resultw1       = JsonConvert.DeserializeObject(responsew1.Content);
            string        WP_AccessToken = resultw1.access_token;

            // Getting the Inventory Details
            var clientw2  = new RestClient("https://secure-wms.com/inventory/stockdetails?customerid=194&facilityid=4");
            var requestw2 = new RestRequest(Method.GET);

            requestw2.AddHeader("Content-Type", "application/hal+json; charset=utf-8");
            requestw2.AddHeader("Accept", "application/hal+json");
            requestw2.AddHeader("Authorization", "Bearer " + WP_AccessToken);
            IRestResponse responsew2 = clientw2.Execute(requestw2);
            dynamic       resultw2   = JsonConvert.DeserializeObject(responsew2.Content);
            string        jsonstr    = resultw2._embedded.ToString();

            // Uploading WorldPack Report to Azure Data Lake
            Azuresource      = directoryFullPath + "WorldPackReport.json";
            Azuredestination = "Third Party Reports/WorldPack Reports/" + "WorldPackReport-" + DateTime.Now.ToString("dd/MM/yyyy") + ".json";
            adlsFileSystemClient.FileSystem.UploadFile(AdlsAccountName, Azuresource, Azuredestination, 1, false, true);
            Console.WriteLine("Uploaded World Pack Report");

            // Deleting the Amazon ADS Product Report File
            Array.ForEach(Directory.GetFiles(directoryFullPath), File.Delete);


            // ------------------ REPORT 8 ------------------ // ---- API Method ----
            // Downloading Amazon MWS Reports - FBA_StorageFees Report through API
            string accessKeyId                 = MWS_AccessID;
            string secretAccessKey             = MWS_SecretKey;
            MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();

            config.ServiceURL = "https://mws.amazonservices.com";
            const string applicationName    = "ApplicationName";
            const string applicationVersion = "0.1a";

            MarketplaceWebServiceClient service =
                new MarketplaceWebServiceClient(
                    accessKeyId,
                    secretAccessKey,
                    applicationName,
                    applicationVersion,
                    config);

            string merchantID   = MWS_MerchantID;
            string mwsauthtoken = MWS_AuthToken;

            // Requesting the Report
            RequestReportRequest reportRequest = new RequestReportRequest();

            reportRequest.Merchant      = merchantID;
            reportRequest.MWSAuthToken  = mwsauthtoken;
            reportRequest.ReportType    = "_GET_FBA_STORAGE_FEE_CHARGES_DATA_";
            reportRequest.StartDate     = DateTime.Parse(DateTime.Now.AddMonths(-1).ToString("dd/MM/yyyy"));
            reportRequest.EndDate       = DateTime.Now;
            reportRequest.ReportOptions = "true"; //shows sales channel to certain reports

            // Handling the Response of RequestReport
            RequestReportResponse reportResponse = service.RequestReport(reportRequest);

            Thread.Sleep(30000); //sleep for 30 seconds to allow report request to generate prior to requesting the reportRequestID

            string requestID    = reportResponse.RequestReportResult.ReportRequestInfo.ReportRequestId;
            string reportStatus = ""; // holds the status of the report

            // Getting the Report Request List
            GetReportRequestListRequest reportRequestList = new GetReportRequestListRequest();

            reportRequestList.Merchant     = merchantID;
            reportRequestList.MWSAuthToken = mwsauthtoken;

            // Handling the Response of Report Request List
            GetReportRequestListResponse reportRequestListReponse = service.GetReportRequestList(reportRequestList);

            foreach (ReportRequestInfo info in reportRequestListReponse.GetReportRequestListResult.ReportRequestInfo)
            {
                if (info.ReportRequestId.Equals(requestID))
                {
                    reportStatus = info.ReportProcessingStatus;

                    if (reportStatus == "_DONE_")
                    {
                        // Getting the Report using Report ID
                        GetReportRequest getReport = new GetReportRequest();
                        getReport.ReportId     = info.GeneratedReportId;
                        getReport.Merchant     = merchantID;
                        getReport.MWSAuthToken = mwsauthtoken;

                        // Writting the Report as a File
                        string filename = Path.Combine(directoryFullPath, "FBA_StorageFeesReport-" + DateTime.Now.ToString("dd/MM/yyyy") + ".csv");
                        getReport.Report = File.Open(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                        GetReportResponse report_response = service.GetReport(getReport);
                        Console.WriteLine("Sleeping for 1 minute to allow processes to settle. ");
                        Thread.Sleep(60000);
                        getReport.Report.Close();

                        // Uploading FBA Storage Fees to Azure Data Lake
                        Azuresource      = filename;
                        Azuredestination = "Amazon MWS Reports/" + "FBA_StorageFeesReport-" + System.DateTime.Now.ToString("dd/MM/yyyy") + ".csv";
                        adlsFileSystemClient.FileSystem.UploadFile(AdlsAccountName, Azuresource, Azuredestination, 1, false, true);
                        Console.WriteLine("Uploaded Amazon MWS FBA_StorageFees Report");

                        break;
                    }
                }
                else
                {
                    continue; // Only concerning about the requested report, not all the reports being generated
                }
            }

            // Closing the Browser
            driver.Quit();
        }
 /// <summary>
 /// Request Report
 /// </summary>
 /// <param name="request">Request Report  request</param>
 /// <returns>Request Report  Response from the service</returns>
 /// <remarks>
 /// requests the generation of a report
 ///
 /// </remarks>
 public RequestReportResponse RequestReport(RequestReportRequest request)
 {
     return(Invoke <RequestReportResponse>("RequestReportResponse.xml"));
 }
Ejemplo n.º 17
0
        public static string RequestAmazonInventoryReport(string accountName, string merchantId, string marketplaceId, string accessKeyId, string secretAccessKey)
        {
            string senderEmail         = ConfigurationManager.AppSettings["senderEmail"];
            string messageFromPassword = ConfigurationManager.AppSettings["messageFromPassword"];
            string messageToEmail      = ConfigurationManager.AppSettings["messageToEmail"];
            string smtpClient          = ConfigurationManager.AppSettings["smtpClient"];
            int    smtpPortNum         = ConvertUtility.ToInt(ConfigurationManager.AppSettings["smtpPortNum"]);

            string ReportRequestId       = "";
            RequestReportRequest request = new RequestReportRequest();

            request.Merchant             = merchantId;
            request.MarketplaceIdList    = new IdList();
            request.MarketplaceIdList.Id = new List <string>(new string[] { marketplaceId });

            request.ReportType = "_GET_FLAT_FILE_OPEN_LISTINGS_DATA_";
            const string applicationName       = "<Your Application Name>";
            const string applicationVersion    = "<Your Application Version>";
            MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();

            config.ServiceURL = "https://mws.amazonservices.com";
            config.SetUserAgentHeader(
                applicationName,
                applicationVersion,
                "C#",
                "<Parameter 1>", "<Parameter 2>");
            MarketplaceWebService.MarketplaceWebService service = new MarketplaceWebServiceClient(accessKeyId, secretAccessKey, config);

            try
            {
                RequestReportResponse response = service.RequestReport(request);


                Console.WriteLine("Service Response");
                Console.WriteLine("=============================================================================");
                Console.WriteLine();

                Console.WriteLine("        RequestReportResponse");

                if (response.IsSetRequestReportResult())
                {
                    Console.WriteLine("            RequestReportResult");
                    RequestReportResult requestReportResult = response.RequestReportResult;
                    ReportRequestInfo   reportRequestInfo   = requestReportResult.ReportRequestInfo;
                    Console.WriteLine("                  ReportRequestInfo");

                    if (reportRequestInfo.IsSetReportProcessingStatus())
                    {
                        Console.WriteLine("               ReportProcessingStatus");
                        Console.WriteLine("                                  {0}", reportRequestInfo.ReportProcessingStatus);
                    }
                    if (reportRequestInfo.IsSetReportRequestId())
                    {
                        Console.WriteLine("                      ReportRequestId");
                        Console.WriteLine("                                  {0}", reportRequestInfo.ReportRequestId);
                        ReportRequestId = reportRequestInfo.ReportRequestId;
                    }
                    if (reportRequestInfo.IsSetReportType())
                    {
                        Console.WriteLine("                           ReportType");
                        Console.WriteLine("                                  {0}", reportRequestInfo.ReportType);
                    }
                    if (reportRequestInfo.IsSetStartDate())
                    {
                        Console.WriteLine("                            StartDate");
                        Console.WriteLine("                                  {0}", reportRequestInfo.StartDate);
                    }
                    if (reportRequestInfo.IsSetEndDate())
                    {
                        Console.WriteLine("                              EndDate");
                        Console.WriteLine("                                  {0}", reportRequestInfo.EndDate);
                    }
                    if (reportRequestInfo.IsSetSubmittedDate())
                    {
                        Console.WriteLine("                        SubmittedDate");
                        Console.WriteLine("                                  {0}", reportRequestInfo.SubmittedDate);
                    }
                }
                if (response.IsSetResponseMetadata())
                {
                    Console.WriteLine("            ResponseMetadata");
                    ResponseMetadata responseMetadata = response.ResponseMetadata;
                    if (responseMetadata.IsSetRequestId())
                    {
                        Console.WriteLine("                RequestId");
                        Console.WriteLine("                    {0}", responseMetadata.RequestId);
                    }
                }

                Console.WriteLine("            ResponseHeaderMetadata");
                Console.WriteLine("                RequestId");
                Console.WriteLine("                    " + response.ResponseHeaderMetadata.RequestId);
                Console.WriteLine("                ResponseContext");
                Console.WriteLine("                    " + response.ResponseHeaderMetadata.ResponseContext);
                Console.WriteLine("                Timestamp");
                Console.WriteLine("                    " + response.ResponseHeaderMetadata.Timestamp);
            }
            catch (MarketplaceWebServiceException ex)
            {
                ExceptionUtility exceptionUtility = new ExceptionUtility();
                exceptionUtility.CatchMethod(ex, "RequestAmazonInventoryReport ", accountName + " " + ex.Message.ToString(), senderEmail, messageFromPassword, messageToEmail, smtpClient, smtpPortNum);
            }

            return(ReportRequestId);
        }