Ejemplo n.º 1
0
        public static string ParseObjectToXML(AmazonEnvelope envelope)
        {
            string xmlBody;
            var    objectType = envelope.GetType();

            var xmlSerializer = new XmlSerializer(objectType);

            using (var ms = new MemoryStream())
            {
                xmlSerializer.Serialize(ms, envelope);
                ms.Position = 0;

                var sr = new StreamReader(ms, Encoding.UTF8);
                xmlBody = sr.ReadToEnd();

                //xmlBody = getInnerXmlBody(xmlResult);
            }

            return(xmlBody);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The the XML string to write into XML file with the specified file name
        /// </summary>
        /// <param name="envelope"></param>
        /// <param name="fileName"></param>
        /// <returns>The full file name information</returns>
        public static string WriteXmlToFile(AmazonEnvelope envelope, string fileName)
        {
            var documentFileName = Path.Combine(ConfigurationManager.AppSettings["MarketplaceFeedRoot"],
                                                string.Format("{1}_{0:yyyy_MM_dd_HHmmss}.xml", DateTime.Now, fileName));
            var xmlString = string.Empty;

            using (var ms = new MemoryStream())
            {
                var xmlSerializer = new XmlSerializer(envelope.GetType());
                xmlSerializer.Serialize(ms, envelope);
                ms.Position = 0;

                var sr = new StreamReader(ms, Encoding.UTF8);
                xmlString = sr.ReadToEnd();

                // save it to the directory
                File.WriteAllText(documentFileName, xmlString);
            }

            return(documentFileName);
        }
Ejemplo n.º 3
0
        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);
        }