private void Setup_GetReport_Returns_ReportContentStream(string expectedGeneratedReportId, string reportContent) { var stream = StreamHelper.CreateMemoryStream(reportContent); var validMd5Hash = MD5ChecksumHelper.ComputeHashForAmazon(stream); var response = new GetReportResponse { ResponseHeaderMetadata = new ResponseHeaderMetadata("requestId", "responseContext", "timestamp"), GetReportResult = new GetReportResult { ContentMD5 = validMd5Hash } }; _mwsClientMock .Setup(mws => mws.GetReport(It.Is <GetReportRequest>(grr => grr.ReportId == expectedGeneratedReportId && grr.Merchant == _merchantId))) .Callback <GetReportRequest>(request => { stream.CopyTo(request.Report); }) .Returns(response); }
public void SubmitFeedToAmazon(IFeedSubmissionEntryService feedSubmissionService, FeedSubmissionEntry feedSubmission) { void HandleSubmitFeedSuccess(SubmitFeedResponse response) { var requestId = response?.ResponseHeaderMetadata?.RequestId ?? "unknown"; var timestamp = response?.ResponseHeaderMetadata?.Timestamp ?? "unknown"; feedSubmission.FeedSubmissionId = response?.SubmitFeedResult?.FeedSubmissionInfo?.FeedSubmissionId; feedSubmission.FeedSubmissionRetryCount = 0; _logger.Info($"AmazonMWS feed submission has succeeded for {feedSubmission.EntryIdentityDescription}. FeedSubmissionId:'{feedSubmission.FeedSubmissionId}'.", new RequestInfo(timestamp, requestId)); } void HandleMissingFeedSubmissionId() { feedSubmission.FeedSubmissionRetryCount++; _logger.Warn($"SubmitFeed did not generate a FeedSubmissionId for {feedSubmission.EntryIdentityDescription}. Feed submission will be retried. FeedSubmissionRetryCount is now : {feedSubmission.FeedSubmissionRetryCount}."); } void HandleNonFatalOrGenericException(Exception e) { feedSubmission.FeedSubmissionRetryCount++; feedSubmission.LastSubmitted = DateTime.UtcNow; _logger.Warn($"AmazonMWS SubmitFeed failed for {feedSubmission.EntryIdentityDescription}. Feed submission will be retried. FeedSubmissionRetryCount is now : {feedSubmission.FeedSubmissionRetryCount}.", e); } void HandleFatalException(Exception e) { feedSubmissionService.Delete(feedSubmission); _logger.Error($"AmazonMWS SubmitFeed failed for {feedSubmission.EntryIdentityDescription}. The entry will now be removed from queue", e); } var missingInformationExceptionMessage = "Cannot submit queued feed to amazon due to missing feed submission information"; if (feedSubmission == null) { throw new ArgumentNullException($"{missingInformationExceptionMessage}: Feed submission entry is null"); } if (feedSubmission.FeedSubmissionData == null) { throw new ArgumentNullException($"{missingInformationExceptionMessage}: Feed submission data is null"); } var feedContentZip = feedSubmission.Details?.FeedContent; if (feedContentZip == null) { throw new ArgumentNullException($"{missingInformationExceptionMessage}: Feed content is missing"); } if (string.IsNullOrEmpty(feedSubmission?.FeedType)) { throw new ArgumentException($"{missingInformationExceptionMessage}: Feed type is missing"); } _logger.Debug($"Attempting to submit the next feed in queue to Amazon: {feedSubmission.EntryIdentityDescription}."); var feedSubmissionData = feedSubmission.GetPropertiesContainer(); using (var stream = ZipHelper.ExtractArchivedSingleFileToStream(feedContentZip)) { var submitFeedRequest = new SubmitFeedRequest { Merchant = feedSubmission.MerchantId, FeedType = feedSubmission.FeedType, FeedContent = stream, MarketplaceIdList = feedSubmissionData.MarketplaceIdList == null ? null : new IdList { Id = feedSubmissionData.MarketplaceIdList }, PurgeAndReplace = feedSubmissionData.PurgeAndReplace ?? false, ContentMD5 = MD5ChecksumHelper.ComputeHashForAmazon(stream), }; if (!string.IsNullOrEmpty(_mWSAuthToken)) { submitFeedRequest.MWSAuthToken = _mWSAuthToken; } try { feedSubmissionService.Unlock(feedSubmission, "Unlocking single feed submission entry - finished attempt to submit feed to amazon."); feedSubmissionService.Update(feedSubmission); var response = _marketplaceWebServiceClient.SubmitFeed(submitFeedRequest); feedSubmission.LastSubmitted = DateTime.UtcNow; if (string.IsNullOrEmpty(response?.SubmitFeedResult?.FeedSubmissionInfo?.FeedSubmissionId)) { HandleMissingFeedSubmissionId(); } else { HandleSubmitFeedSuccess(response); } } catch (MarketplaceWebServiceException e) when(e.StatusCode == HttpStatusCode.BadRequest && IsAmazonErrorCodeFatal(e.ErrorCode)) { HandleFatalException(e); } catch (MarketplaceWebServiceException e) when(IsAmazonErrorCodeNonFatal(e.ErrorCode)) { HandleNonFatalOrGenericException(e); } catch (Exception e) { HandleNonFatalOrGenericException(e); } finally { stream.Dispose(); feedSubmissionService.SaveChanges(); } } }