/// <summary> /// Creates the SubmitFeedRequest given a stream of content /// </summary> /// <param name="dataContent">The stream of the content</param> /// <param name="feedType">The type of feed to be submitted.</param> /// <returns>A SubmitFeedRequest object</returns> private SubmitFeedRequest CreateSubmitFeedRequest(Stream dataContent, string feedType) { var request = new SubmitFeedRequest(); request.Merchant = _MerchantId; request.MarketplaceIdList = new IdList(); request.MarketplaceIdList.Id = new List <string>(new string[] { _MarketplaceId }); request.FeedContent = dataContent; // Set the MD5 hash of the content request.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(request.FeedContent); // Reset the position of the reader of the FeedContent request.FeedType = feedType; return(request); }
public SubmitFeedRequest GetSubmitFeedRequest(AmazonFeedType feedType, FileStream feedContent) { var request = new SubmitFeedRequest() { Merchant = _amazonSellerSettings.SellerId, ContentType = new ContentType(MediaType.XML), FeedContent = feedContent, FeedType = feedType.ToString(), MarketplaceIdList = new IdList { Id = new List <string>(new[] { _amazonSellerSettings.MarketplaceId }) } }; request.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(request.FeedContent); return(request); }
/// <summary> /// Create SubmitFeedRequest object with the specified XML file path and the type of the feed /// </summary> /// <param name="filePath">The full file path of the XML</param> /// <param name="feedType">The type of Amazon feed</param> /// <returns></returns> public static SubmitFeedRequest CreateSubmitFeedRequest(string filePath, AmazonFeedType feedType) { // create submit feed request var feedRequest = new SubmitFeedRequest { Merchant = MerchantId, MarketplaceIdList = new IdList { Id = MarketplaceIdList }, FeedType = feedType.ToString(), ContentType = new ContentType(MediaType.OctetStream), FeedContent = File.Open(filePath, FileMode.Open, FileAccess.Read) }; feedRequest.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(feedRequest.FeedContent); return(feedRequest); }
private SubmitFeedResponse InvokeSubmitFeed(SubmitFeedParamaters objParams, string xmlFeedContent) { SubmitFeedResponse resSubmitFeed = new SubmitFeedResponse(); try { UTF8Encoding encoding = new UTF8Encoding(); Stream streamData = new MemoryStream(encoding.GetBytes(xmlFeedContent)); SubmitFeedRequest request = new SubmitFeedRequest(); request.Merchant = objParams.objSOAmazonSetup.SellerId; request.FeedContent = streamData; request.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(request.FeedContent); request.FeedContent.Position = 0; request.PurgeAndReplace = false; request.FeedType = SOConstants.feedTypePostOrderFulfillment; request.MWSAuthToken = objParams.objSOAmazonSetup.AuthToken; resSubmitFeed = clientFeed.SubmitFeed(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(objParams.objPartialMaint, SOConstants.apiSubmitFeed)); resSubmitFeed = InvokeSubmitFeed(objParams, xmlFeedContent); } 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(resSubmitFeed); }
/// <summary> /// Create SubmitFeedRequest given an XML file path and feed type /// </summary> /// <param name="xmlFilePath">The xml full file path</param> /// <param name="feedType">The type of feed to be submitted</param> /// <returns></returns> private SubmitFeedRequest createSubmitFeedRequest(string xmlFilePath, AmazonFeedType feedType) { // create submit feed request var feedRequest = new SubmitFeedRequest { Merchant = _merchantId, MarketplaceIdList = new IdList { Id = new List <string> { _marketplaceId } }, FeedType = feedType.ToString(), ContentType = new ContentType(MediaType.OctetStream), FeedContent = File.Open(xmlFilePath, FileMode.Open, FileAccess.ReadWrite) }; feedRequest.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(feedRequest.FeedContent); return(feedRequest); }
public SubmitFeedResult SubmitFeed(MarketplaceWebService service, FileInfo fileInfo, AmazonFeedType feedType) { var response = new SubmitFeedResponse(); var sfRequest = new SubmitFeedRequest(); sfRequest.Merchant = _merchantId; sfRequest.MarketplaceIdList = new IdList { Id = new List <string>(new[] { _marketplaceId }) }; using (var stream = new FileStream(fileInfo.Name, FileMode.Open, FileAccess.ReadWrite)) { sfRequest.FeedContent = stream; sfRequest.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(sfRequest.FeedContent); sfRequest.FeedContent.Position = 0; sfRequest.FeedType = feedType.ToString(); response = service.SubmitFeed(sfRequest); } return(response.SubmitFeedResult); }
public void UpdateOurPrices(IEnumerable <UpdatedItemPrice> newPrices) { SubmitFeedRequest request = new SubmitFeedRequest { Merchant = m_merchantId, MarketplaceIdList = new IdList { Id = new List <string>(new[] { m_marketPlaceId }) } }; // This chunk of code takes our queue, writes the formatted xml string needed, deletes the queue. // Puts the xml string into a temporary stream, and makes the request to Amazon. string newPricesXmlString = ConstructXmlFile(newPrices); byte[] byteArray = Encoding.ASCII.GetBytes(newPricesXmlString); request.FeedContent = new MemoryStream(byteArray); request.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(request.FeedContent); request.FeedType = "_POST_PRODUCT_PRICING_DATA_"; InvokeSubmitFeed(request); // No memory leaks? I think this is unnecessary. request.FeedContent.Close(); }
public static string SubmitAmazonTrackingFeed(string filepath, string merchantId, string marketplaceId, string accessKeyId, string secretAccessKey) { string feedSubmissionId = ""; SubmitFeedRequest request = new SubmitFeedRequest(); request.Merchant = merchantId; request.MarketplaceIdList = new IdList(); request.MarketplaceIdList.Id = new List <string>(new string[] { marketplaceId }); request.FeedContent = File.Open(filepath, FileMode.Open, FileAccess.Read); request.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(request.FeedContent); request.FeedContent.Position = 0; request.FeedType = "_POST_ORDER_FULFILLMENT_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 { SubmitFeedResponse response = service.SubmitFeed(request); Console.WriteLine("Service Response"); Console.WriteLine("============================================================================="); Console.WriteLine(); Console.WriteLine(" SubmitFeedResponse"); if (response.IsSetSubmitFeedResult()) { Console.WriteLine(" SubmitFeedResult"); SubmitFeedResult submitFeedResult = response.SubmitFeedResult; if (submitFeedResult.IsSetFeedSubmissionInfo()) { Console.WriteLine(" FeedSubmissionInfo"); FeedSubmissionInfo feedSubmissionInfo = submitFeedResult.FeedSubmissionInfo; if (feedSubmissionInfo.IsSetFeedSubmissionId()) { Console.WriteLine(" FeedSubmissionId"); Console.WriteLine(" {0}", feedSubmissionInfo.FeedSubmissionId); feedSubmissionId = feedSubmissionInfo.FeedSubmissionId; } if (feedSubmissionInfo.IsSetFeedType()) { Console.WriteLine(" FeedType"); Console.WriteLine(" {0}", feedSubmissionInfo.FeedType); } if (feedSubmissionInfo.IsSetSubmittedDate()) { Console.WriteLine(" SubmittedDate"); Console.WriteLine(" {0}", feedSubmissionInfo.SubmittedDate); } if (feedSubmissionInfo.IsSetFeedProcessingStatus()) { Console.WriteLine(" FeedProcessingStatus"); Console.WriteLine(" {0}", feedSubmissionInfo.FeedProcessingStatus); } if (feedSubmissionInfo.IsSetStartedProcessingDate()) { Console.WriteLine(" StartedProcessingDate"); Console.WriteLine(" {0}", feedSubmissionInfo.StartedProcessingDate); } if (feedSubmissionInfo.IsSetCompletedProcessingDate()) { Console.WriteLine(" CompletedProcessingDate"); Console.WriteLine(" {0}", feedSubmissionInfo.CompletedProcessingDate); } } } 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, "SubmitAmazonTrackingFeed Error:", ex.Message.ToString(), senderEmail, messageFromPassword, messageToEmail, smtpClient, smtpPortNum); 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); } return(feedSubmissionId); }
public List <XElement> SubmitFeed(string accountName, XElement feed) { List <XElement> errors = new List <XElement>(); SubmitFeedRequest request = new SubmitFeedRequest(); request.FeedType = "_POST_INVENTORY_AVAILABILITY_DATA_"; request.MWSAuthToken = mwsAuthTokenDictionary[accountName]; request.Merchant = sellerIdDictionary[accountName]; request.MarketplaceIdList = new IdList(); request.MarketplaceIdList.Id = new List <string>(); request.MarketplaceIdList.Id.Add(marketplaceId); MemoryStream memoryStream = new MemoryStream(); feed.Save(memoryStream); request.FeedContent = memoryStream; request.FeedContent.Position = 0; request.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(request.FeedContent); request.FeedContent.Position = 0; int retryCount = 0; SubmitFeedResponse response = null; while (retryCount <= 4 && response == null) { Thread.Sleep(TimeSpan.FromMinutes(retryCount * 2)); try { response = mwsClient.SubmitFeed(request); } catch (Exception e) { if (retryCount >= 4) { throw e; } retryCount++; } } memoryStream.Close(); string feedSubmissionId = response.SubmitFeedResult.FeedSubmissionInfo.FeedSubmissionId; int count = 0; bool done = false; while (count < 100 && !done) { Thread.Sleep(60000); GetFeedSubmissionListRequest feedSubmissionListRequest = new GetFeedSubmissionListRequest(); feedSubmissionListRequest.MWSAuthToken = mwsAuthTokenDictionary[accountName]; feedSubmissionListRequest.Merchant = sellerIdDictionary[accountName]; feedSubmissionListRequest.FeedSubmissionIdList = new IdList(); feedSubmissionListRequest.FeedSubmissionIdList.Id.Add(feedSubmissionId); GetFeedSubmissionListResponse feedSubmissionResponse = mwsClient.GetFeedSubmissionList(feedSubmissionListRequest); foreach (FeedSubmissionInfo info in feedSubmissionResponse.GetFeedSubmissionListResult.FeedSubmissionInfo) { if (info.FeedSubmissionId == feedSubmissionId) { if (info.FeedProcessingStatus == "_DONE_") { done = true; GetFeedSubmissionResultRequest feedSubmissionResultRequest = new GetFeedSubmissionResultRequest(); feedSubmissionResultRequest.MWSAuthToken = mwsAuthTokenDictionary[accountName]; feedSubmissionResultRequest.Merchant = sellerIdDictionary[accountName]; feedSubmissionResultRequest.FeedSubmissionId = feedSubmissionId; MemoryStream stream = new MemoryStream(); feedSubmissionResultRequest.FeedSubmissionResult = stream; retryCount = 0; GetFeedSubmissionResultResponse feedSubmissionResultResponse = null; while (retryCount <= m_maxRetry && feedSubmissionResultResponse == null) { Thread.Sleep(TimeSpan.FromMinutes(retryCount * 2)); try { feedSubmissionResultResponse = mwsClient.GetFeedSubmissionResult(feedSubmissionResultRequest); } catch (MarketplaceWebServiceException e) { if (e.ErrorCode == "RequestThrottled") { retryCount++; } } } XElement responseElement = XElement.Load(stream); IEnumerable <XElement> messages = responseElement.Descendants("Message"); foreach (XElement message in messages) { XElement processingReportElement = message.Element("ProcessingReport"); int nError = (int)processingReportElement.Element("ProcessingSummary").Element("MessagesWithError"); if (nError > 0) { int messageId = (int)message.Element("MessageID"); } } } } } count++; } return(errors); }
private List <string> SendAmazonFeeds(List <List <object> > amazonUpdateList, AmazonEnvelopeMessageType messageType, AmazonFeedType feedType) { { var masterCounter = 1; var returnResult = new List <string>(); foreach (var amazonUpdateGroup in amazonUpdateList) { var amazonEnvelope = new AmazonEnvelope { Header = new Header { DocumentVersion = "1.01", MerchantIdentifier = _merchantId, }, MessageType = messageType, PurgeAndReplace = false, PurgeAndReplaceSpecified = true }; var updates = new List <AmazonEnvelopeMessage>(); var counter = 1; foreach (var amazonUpdate in amazonUpdateGroup) { var curUpdate = new AmazonEnvelopeMessage { MessageID = counter.ToString(), OperationType = AmazonEnvelopeMessageOperationType.Update, //OperationTypeSpecified = true, Item = amazonUpdate }; updates.Add(curUpdate); counter++; } amazonEnvelope.Message = updates.ToArray(); var xmlString = MarketplaceHelper.ParseObjectToXML(amazonEnvelope); var path = "D:\\logs\\"; var fileName = string.Format("Amazon{0}Feed_{1}{3}.{2}", messageType, masterCounter, "xml", DateTime.Now.Second); var documentFileName = Path.Combine(path, fileName); File.WriteAllText(documentFileName, xmlString); if (!File.Exists(documentFileName)) { throw new ArgumentException("SendFeed document not generated properly"); } var feedRequest = new SubmitFeedRequest { Merchant = _merchantId, MarketplaceIdList = new IdList { Id = new List <string>(new[] { _marketplaceId }) }, FeedType = feedType.ToString(), ContentType = new ContentType(MediaType.OctetStream), FeedContent = File.Open(documentFileName, FileMode.Open, FileAccess.Read) }; feedRequest.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(feedRequest.FeedContent); var feedConfig = new MarketplaceWebServiceConfig { ServiceURL = "https://mws.amazonservices.com" }; var feedService = new MarketplaceWebServiceClient(_awsAccessKey, _secretKey, "Demac", "1.01", feedConfig); var uploadSuccess = false; var retryCount = 0; while (!uploadSuccess) { try { var feedResponse = feedService.SubmitFeed(feedRequest); var submissionId = feedResponse.SubmitFeedResult.FeedSubmissionInfo.FeedSubmissionId; returnResult.Add(submissionId); uploadSuccess = true; masterCounter++; //Thread.Sleep(120000); } catch (Exception ex) { retryCount++; if (retryCount == 3) { break; } //Thread.Sleep(18000); if (ex.ToString().ToLowerInvariant().Contains("request is throttled")) { continue; } returnResult.Add(string.Format("ERROR: {0}", ex)); } } } return(returnResult); } }
public static SubmitFeedResponse SendAmazonFeeds(IMarketplaceWebServiceClient feedService, IEnumerable <Product> amazonUpdateList, AmazonEnvelopeMessageType messageType, AmazonFeedType feedType, string AmazonMerchantId, string AmazonMarketplaceId, string AmazonServiceUrl, string AmazonAccessKeyId, string AmazonSecretAccessKey) { //var requestResponse = new List<string>(); SubmitFeedResponse feedResponse = null; var amazonEnvelope = new AmazonEnvelope { Header = new Header { DocumentVersion = "1.01", MerchantIdentifier = AmazonMerchantId, }, MessageType = messageType }; var updates = new List <AmazonEnvelopeMessage>(); var counter = 1; foreach (var amazonUpdate in amazonUpdateList) { var curUpdate = new AmazonEnvelopeMessage { MessageID = counter.ToString(), Item = amazonUpdate }; updates.Add(curUpdate); counter++; } //add all update products to envelope's message amazonEnvelope.Message = updates.ToArray(); var serializer = new XmlSerializer(amazonEnvelope.GetType()); var stringReader = new StringWriter(); serializer.Serialize(stringReader, amazonEnvelope); var xmlResult = stringReader.ToString(); using (MemoryStream feedStream = new MemoryStream()) { serializer.Serialize(feedStream, amazonEnvelope); var feedRequest = new SubmitFeedRequest { Merchant = AmazonMerchantId, MarketplaceIdList = new IdList { Id = new List <string>(new[] { AmazonMarketplaceId }) }, FeedType = feedType.ToString(), ContentType = new ContentType(MediaType.OctetStream), FeedContent = feedStream }; // Calculating the MD5 hash value exhausts the stream, and therefore we must either reset the // position, or create another stream for the calculation. feedRequest.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(feedRequest.FeedContent); //var feedService = new MockMarketplaceWebServiceClient(); var uploadSuccess = false; var retryCount = 0; while (!uploadSuccess) { try { feedResponse = feedService.SubmitFeed(feedRequest); //var submissionId = feedResponse.SubmitFeedResult.FeedSubmissionInfo.FeedSubmissionId; //requestResponse.Add(submissionId); uploadSuccess = true; } catch (Exception ex) { //if sending not succeed after 3 attempts stop retrying retryCount++; if (retryCount == 3) { break; } //pause sending for 3 minutes Thread.Sleep(18000); if (ex.ToString().ToLowerInvariant().Contains("request is throttled")) { continue; } //requestResponse.Add(string.Format("ERROR: {0}", ex)); } } } return(feedResponse); }