private void DownloadSalesData() { this.StatusDescription = string.Format("Creating MWS client"); // Set up MWS client string accessKey = System.Configuration.ConfigurationManager.AppSettings["MwsAccK"]; string secretKey = System.Configuration.ConfigurationManager.AppSettings["MwsSecK"]; string sellerId = System.Configuration.ConfigurationManager.AppSettings["MwsSeller"]; string marketplaceId = System.Configuration.ConfigurationManager.AppSettings["MwsMktpl"]; MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig(); config.ServiceURL = "https://mws.amazonservices.com "; config.SetUserAgentHeader("CMA", "1.0", "C#", new string[] { }); MarketplaceWebServiceClient client = new MarketplaceWebServiceClient(accessKey, secretKey, config); // Submit the report request MarketplaceWebService.Model.RequestReportRequest request = new MarketplaceWebService.Model.RequestReportRequest(); request.ReportType = "_GET_FLAT_FILE_ALL_ORDERS_DATA_BY_LAST_UPDATE_"; request.Merchant = sellerId; request.StartDate = DateTime.Now.AddDays(-30).AddHours(-6); request.EndDate = DateTime.Now.AddHours(-6); this.StatusDescription = string.Format("Requesting Report Type {0} for {1} through {2}", request.ReportType, request.StartDate.ToShortDateString(), request.EndDate.ToShortDateString()); var responseToRequestReport = client.RequestReport(request); Thread.Sleep(1000); string reportRequestId = ""; if (responseToRequestReport.IsSetRequestReportResult()) { if (responseToRequestReport.RequestReportResult.IsSetReportRequestInfo()) { if (responseToRequestReport.RequestReportResult.ReportRequestInfo.IsSetReportRequestId()) { reportRequestId = responseToRequestReport.RequestReportResult.ReportRequestInfo.ReportRequestId; } else { // Would be good to implement wait-and-retry here throw new Exception("ReportRequestId was not returned from RequestReport()"); } } } this.StatusDescription = string.Format("Report Request ID: {0}", reportRequestId); Thread.Sleep(1000); // Check for the report to have a _DONE_ status string reportId = ""; bool reportDone = false; do { this.StatusDescription = string.Format("Checking report status"); MarketplaceWebService.Model.GetReportRequestListRequest requestGetReportRequestList = new MarketplaceWebService.Model.GetReportRequestListRequest(); requestGetReportRequestList.ReportRequestIdList = new MarketplaceWebService.Model.IdList(); requestGetReportRequestList.ReportRequestIdList.Id = new List<string>(); requestGetReportRequestList.ReportRequestIdList.Id.Add(reportRequestId); requestGetReportRequestList.Merchant = sellerId; var responseToGetReportRequestList = client.GetReportRequestList(requestGetReportRequestList); if (responseToGetReportRequestList.IsSetGetReportRequestListResult() && responseToGetReportRequestList.GetReportRequestListResult.IsSetReportRequestInfo() && responseToGetReportRequestList.GetReportRequestListResult.ReportRequestInfo.Count != 0) { this.StatusDescription = string.Format("Report status: {0}", responseToGetReportRequestList.GetReportRequestListResult.ReportRequestInfo[0].ReportProcessingStatus); Thread.Sleep(500); if (responseToGetReportRequestList.GetReportRequestListResult.ReportRequestInfo[0].ReportProcessingStatus.Equals("_DONE_")) { reportDone = true; if (responseToGetReportRequestList.GetReportRequestListResult.ReportRequestInfo[0].IsSetGeneratedReportId()) { reportId = responseToGetReportRequestList.GetReportRequestListResult.ReportRequestInfo[0].GeneratedReportId; this.StatusDescription = string.Format("Report ID: {0}", reportId); } else { // ReportId was not returned, call GetReportList() this.StatusDescription = string.Format("Report ID was not returned. Trying GetReportList()"); var requestGetReportList = new MarketplaceWebService.Model.GetReportListRequest(); requestGetReportList.ReportRequestIdList = new MarketplaceWebService.Model.IdList(); requestGetReportList.ReportRequestIdList.Id = new List<string>(); requestGetReportList.ReportRequestIdList.Id.Add(reportRequestId); requestGetReportList.Merchant = sellerId; var responseToGetReportList = client.GetReportList(requestGetReportList); if (responseToGetReportList.IsSetGetReportListResult() && responseToGetReportList.GetReportListResult.IsSetReportInfo() && responseToGetReportList.GetReportListResult.ReportInfo.Count != 0) { reportId = responseToGetReportList.GetReportListResult.ReportInfo[0].ReportId; this.StatusDescription = string.Format("Report ID: {0}", reportId); } else { throw new Exception("ReportId could not be retrieved."); } } } else if (responseToGetReportRequestList.GetReportRequestListResult.ReportRequestInfo[0].ReportProcessingStatus.Equals("_DONE_NO_DATA_")) { throw new Exception("Report returned with status _DONE_NO_DATA_"); } else { for (int secondsTilRetry = 60; secondsTilRetry > 0; secondsTilRetry--) { this.StatusDescription = string.Format("Report status: {0} (Will check again in {1}s)", responseToGetReportRequestList.GetReportRequestListResult.ReportRequestInfo[0].ReportProcessingStatus, secondsTilRetry); Thread.Sleep(1000); } } } } while (!reportDone); Thread.Sleep(1000); // Retrieve the report string saveFileName = reportId + ".txt"; this.StatusDescription = string.Format("Downloading report to {0}", saveFileName); MarketplaceWebService.Model.GetReportRequest requestGetReport = new MarketplaceWebService.Model.GetReportRequest(); requestGetReport.ReportId = reportId; requestGetReport.Merchant = sellerId; using (Stream file = File.Open(saveFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { requestGetReport.Report = file; var responseToGetReport = client.GetReport(requestGetReport); } }
private void DownloadSalesData() { this.StatusDescription = string.Format("Creating MWS client"); // Set up MWS client string accessKey = System.Configuration.ConfigurationManager.AppSettings["MwsAccK"]; string secretKey = System.Configuration.ConfigurationManager.AppSettings["MwsSecK"]; string sellerId = System.Configuration.ConfigurationManager.AppSettings["MwsSeller"]; string marketplaceId = System.Configuration.ConfigurationManager.AppSettings["MwsMktpl"]; MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig(); config.ServiceURL = "https://mws.amazonservices.com "; config.SetUserAgentHeader("CMA", "1.0", "C#", new string[] { }); MarketplaceWebServiceClient client = new MarketplaceWebServiceClient(accessKey, secretKey, config); // Submit the report request MarketplaceWebService.Model.RequestReportRequest request = new MarketplaceWebService.Model.RequestReportRequest(); request.ReportType = "_GET_FLAT_FILE_ALL_ORDERS_DATA_BY_LAST_UPDATE_"; request.Merchant = sellerId; request.StartDate = DateTime.Now.AddDays(-30).AddHours(-6); request.EndDate = DateTime.Now.AddHours(-6); this.StatusDescription = string.Format("Requesting Report Type {0} for {1} through {2}", request.ReportType, request.StartDate.ToShortDateString(), request.EndDate.ToShortDateString()); var responseToRequestReport = client.RequestReport(request); Thread.Sleep(1000); string reportRequestId = ""; if (responseToRequestReport.IsSetRequestReportResult()) { if (responseToRequestReport.RequestReportResult.IsSetReportRequestInfo()) { if (responseToRequestReport.RequestReportResult.ReportRequestInfo.IsSetReportRequestId()) { reportRequestId = responseToRequestReport.RequestReportResult.ReportRequestInfo.ReportRequestId; } else { // Would be good to implement wait-and-retry here throw new Exception("ReportRequestId was not returned from RequestReport()"); } } } this.StatusDescription = string.Format("Report Request ID: {0}", reportRequestId); Thread.Sleep(1000); // Check for the report to have a _DONE_ status string reportId = ""; bool reportDone = false; do { this.StatusDescription = string.Format("Checking report status"); MarketplaceWebService.Model.GetReportRequestListRequest requestGetReportRequestList = new MarketplaceWebService.Model.GetReportRequestListRequest(); requestGetReportRequestList.ReportRequestIdList = new MarketplaceWebService.Model.IdList(); requestGetReportRequestList.ReportRequestIdList.Id = new List <string>(); requestGetReportRequestList.ReportRequestIdList.Id.Add(reportRequestId); requestGetReportRequestList.Merchant = sellerId; var responseToGetReportRequestList = client.GetReportRequestList(requestGetReportRequestList); if (responseToGetReportRequestList.IsSetGetReportRequestListResult() && responseToGetReportRequestList.GetReportRequestListResult.IsSetReportRequestInfo() && responseToGetReportRequestList.GetReportRequestListResult.ReportRequestInfo.Count != 0) { this.StatusDescription = string.Format("Report status: {0}", responseToGetReportRequestList.GetReportRequestListResult.ReportRequestInfo[0].ReportProcessingStatus); Thread.Sleep(500); if (responseToGetReportRequestList.GetReportRequestListResult.ReportRequestInfo[0].ReportProcessingStatus.Equals("_DONE_")) { reportDone = true; if (responseToGetReportRequestList.GetReportRequestListResult.ReportRequestInfo[0].IsSetGeneratedReportId()) { reportId = responseToGetReportRequestList.GetReportRequestListResult.ReportRequestInfo[0].GeneratedReportId; this.StatusDescription = string.Format("Report ID: {0}", reportId); } else { // ReportId was not returned, call GetReportList() this.StatusDescription = string.Format("Report ID was not returned. Trying GetReportList()"); var requestGetReportList = new MarketplaceWebService.Model.GetReportListRequest(); requestGetReportList.ReportRequestIdList = new MarketplaceWebService.Model.IdList(); requestGetReportList.ReportRequestIdList.Id = new List <string>(); requestGetReportList.ReportRequestIdList.Id.Add(reportRequestId); requestGetReportList.Merchant = sellerId; var responseToGetReportList = client.GetReportList(requestGetReportList); if (responseToGetReportList.IsSetGetReportListResult() && responseToGetReportList.GetReportListResult.IsSetReportInfo() && responseToGetReportList.GetReportListResult.ReportInfo.Count != 0) { reportId = responseToGetReportList.GetReportListResult.ReportInfo[0].ReportId; this.StatusDescription = string.Format("Report ID: {0}", reportId); } else { throw new Exception("ReportId could not be retrieved."); } } } else if (responseToGetReportRequestList.GetReportRequestListResult.ReportRequestInfo[0].ReportProcessingStatus.Equals("_DONE_NO_DATA_")) { throw new Exception("Report returned with status _DONE_NO_DATA_"); } else { for (int secondsTilRetry = 60; secondsTilRetry > 0; secondsTilRetry--) { this.StatusDescription = string.Format("Report status: {0} (Will check again in {1}s)", responseToGetReportRequestList.GetReportRequestListResult.ReportRequestInfo[0].ReportProcessingStatus, secondsTilRetry); Thread.Sleep(1000); } } } } while (!reportDone); Thread.Sleep(1000); // Retrieve the report string saveFileName = reportId + ".txt"; this.StatusDescription = string.Format("Downloading report to {0}", saveFileName); MarketplaceWebService.Model.GetReportRequest requestGetReport = new MarketplaceWebService.Model.GetReportRequest(); requestGetReport.ReportId = reportId; requestGetReport.Merchant = sellerId; using (Stream file = File.Open(saveFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { requestGetReport.Report = file; var responseToGetReport = client.GetReport(requestGetReport); } }
public IEnumerable <object> GetReportList(DateTime startDate, DateTime endDate) { var request = new GetReportListRequest { Merchant = "A12HYFEDED6DEW", AvailableFromDate = startDate, AvailableToDate = endDate, ReportTypeList = new TypeList { Type = new List <string> { "_GET_V2_SETTLEMENT_REPORT_DATA_XML_" } } }; var response = _amazonClient.GetReportList(request); var reportInfoList = response.GetReportListResult.ReportInfo; var nextToken = response.GetReportListResult.NextToken; var hasNext = response.GetReportListResult.HasNext; GetReportListByNextTokenResponse nextResponse = null; var reportCounter = 0; // pause for 2 seconds to give the Amazon MWS a little bit time to process the others Thread.Sleep(2000); // let's find the report we are interested in var reports = findReportTypes(reportInfoList); while (hasNext) { var nextRequest = new GetReportListByNextTokenRequest { Merchant = "A12HYFEDED6DEW", NextToken = nextToken }; nextResponse = _amazonClient.GetReportListByNextToken(nextRequest); reportInfoList = nextResponse.GetReportListByNextTokenResult.ReportInfo; nextToken = nextResponse.GetReportListByNextTokenResult.NextToken; hasNext = nextResponse.GetReportListByNextTokenResult.HasNext; // pause for 2 seconds, this is the restore rate for GetReportListByNextToken Thread.Sleep(2000); // find and add it to the list reports.AddRange(findReportTypes(reportInfoList)); foreach (var reportInfo in reports) { // check if the report request quota reach to 15 if (reportCounter % 15 == 0) { // if so, pause for 1 minute, this is the restore rate for GetReport Thread.Sleep(60000); } var stream = downloadReportStream(reportInfo.ReportId); // save the stream into a file saveStreamToFile(stream, string.Format("{0}_{1}", reportInfo.ReportType, reportInfo.ReportId)); reportCounter++; } // clear the reports lisst reports.Clear(); } return(null); }
public List <SettlementReportDataItem> GetSettlementReports(string reportType, string sellerId) { try { MarketplaceWebServiceClient service = new MarketplaceWebServiceClient(accessKeyId, secretAccessKey, applicationName, applicationVersion, config); GetReportListRequest reportListRequest = new GetReportListRequest(); reportListRequest.Merchant = sellerId; reportListRequest.MWSAuthToken = mwsAuthToken; //reportListRequest.ReportRequestIdList = lstRequestID; reportListRequest.ReportTypeList = new TypeList(); reportListRequest.ReportTypeList.Type = new List <string>() { reportType }; var reportList = service.GetReportList(reportListRequest); GetReportListResult reportListResult = reportList.GetReportListResult; //TODO - Implement GetReportsListByNextToken if (reportListResult.ReportInfo.Count > 0) { for (int i = 0; i < reportListResult.ReportInfo.Count; i++) { GetReportRequest reportRequest = new GetReportRequest(); reportRequest.Merchant = sellerId; reportRequest.MWSAuthToken = mwsAuthToken; reportRequest.ReportId = reportListResult.ReportInfo[i].ReportId; string path = Path.Combine(_reportsPath, reportListResult.ReportInfo[i].ReportId + ".xml"); reportRequest.Report = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite); 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; } return(null); }
public List <string> SubmitSettlementReportRequest(DateTime createdAfter, bool includeAcknowledged) { var request = new GetReportListRequest { Merchant = _merchantId, AvailableFromDate = createdAfter, AvailableToDate = DateTime.Now, ReportTypeList = new TypeList { Type = new List <string> { _SETTLEMENT_REPORT_TYPE } } }; var response = _amazonClient.GetReportList(request); var reportInfoList = response.GetReportListResult.ReportInfo; var nextToken = response.GetReportListResult.NextToken; var hasNext = response.GetReportListResult.HasNext; var reportCounter = 1; bool hasNextReport; var reportFilePaths = new List <string>(); var reportIds = new List <string>(); // pause for 2 seconds to give the Amazon MWS a little bit time to process the others Thread.Sleep(2000); do { hasNextReport = false; // download and parse the report info foreach (var reportInfo in reportInfoList) { if (!includeAcknowledged && reportInfo.Acknowledged) { continue; } // check if the report request quota reach to 15 if (reportCounter % 15 == 0) { // if so, pause for 1 minute, this is the restore rate for GetReport Thread.Sleep(60000); } var stream = downloadReportStream(reportInfo.ReportId); // save the stream into a file var filePath = saveStreamToFile(stream, string.Format("{0}{1}", reportInfo.ReportId, reportInfo.ReportType)); // add the filepath and report id to the lists reportFilePaths.Add(filePath); reportIds.Add(reportInfo.ReportId); reportCounter++; } // send another request for the next report list if (hasNext) { var nextRequest = new GetReportListByNextTokenRequest { Merchant = _merchantId, NextToken = nextToken }; var nextResponse = _amazonClient.GetReportListByNextToken(nextRequest); reportInfoList = nextResponse.GetReportListByNextTokenResult.ReportInfo; nextToken = nextResponse.GetReportListByNextTokenResult.NextToken; hasNext = nextResponse.GetReportListByNextTokenResult.HasNext; hasNextReport = reportInfoList.Any(); } } while (hasNextReport); // then let's send the acknowledgment to the Reports Ids submitReportsAcknowledgement(reportIds); return(reportFilePaths); }