Amazon-SP-API-CSharp icon indicating copy to clipboard operation
Amazon-SP-API-CSharp copied to clipboard

FeedSerivce response document can sometimes be gzipped

Open tank104 opened this issue 3 years ago • 6 comments

When calling GetFeedDocumentProcessingReportAsync sometimes the response can be gzipped (if its large enough?). Confirmed getting it for the product feed. It seems you are using WebClient, that doesn't support automatic decompression out of the box.

To turn it on there are these options: https://stackoverflow.com/questions/4567313/uncompressing-gzip-response-from-webclient


      private static async Task<Stream> GetStreamFromUrlAsync(string url)
        {
            byte[] imageData = null;

            using (var wc = new System.Net.WebClient())
                imageData = await wc.DownloadDataTaskAsync(new Uri(url));

            return new MemoryStream(imageData);
        }

tank104 avatar Oct 03 '22 01:10 tank104

ITs actually more complex than that - feedDocument returns the compression algo. And sadly if it is gzipped, Amazon don't return the gzip header to automatically decompress on response.

So something like the below works:


      var processingReport = await GetFeedDocumentProcessingReportAsync(feedDocument.Url, feedDocument.CompressionAlgorithm);

    publicasync Task<ProcessingReportMessage> GetFeedDocumentProcessingReportAsync(string url, FikaAmazonAPI.AmazonSpApiSDK.Models.Feeds.FeedDocument.CompressionAlgorithmEnum? compressionAlgorithm = null)
    {
      ProcessingReportMessage processingReport = null;
      string responseContent;
      try
      {
        var stream = await GetStreamFromUrlAsync(url);
        if (compressionAlgorithm.HasValue && compressionAlgorithm.Value == FikaAmazonAPI.AmazonSpApiSDK.Models.Feeds.FeedDocument.CompressionAlgorithmEnum.GZIP)
          stream = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress);


tank104 avatar Oct 03 '22 03:10 tank104

I think this should work, though I wonder if it might be better to have a new overload that takes FeedDocument as a parameter and maybe obsolete the current that takes the URL for a few versions. This way we don't have to remember to pass the second argument every time we are coding this.

RenzoF avatar Oct 04 '22 11:10 RenzoF

If you try this and you sour its work for all please send PULL REQUEST with this change we will add it to the library

abuzuhri avatar Oct 06 '22 15:10 abuzuhri

@tank104 please let us know so we can add this to nuget

abuzuhri avatar Oct 06 '22 15:10 abuzuhri

Yup will get it done this week - sorry been on holiday @RenzoF I will overload it as you mention, and obsolete the other one - so people know that really they need to send compression (well the whole feedDocument)

tank104 avatar Oct 09 '22 22:10 tank104

great thanks @tank104 I think it has been done similarly in the ReportService and apparently it could come encrypted as well, have a look this for inpiration https://github.com/abuzuhri/Amazon-SP-API-CSharp/blob/8c27498dfb0ba0b7e7138be79d4070f8d41e5e6d/Source/FikaAmazonAPI/Services/ReportService.cs#L178

RenzoF avatar Oct 10 '22 09:10 RenzoF

@tank104 do you solve the problem ?

abuzuhri avatar Oct 23 '22 09:10 abuzuhri

@abuzuhri I think you closed this prematurely as can't see it fixed? (also can't re-open this request)

Sorry got pulled off the project for a bit. This is done now - PR https://github.com/abuzuhri/Amazon-SP-API-CSharp/pull/400

tank104 avatar Nov 14 '22 01:11 tank104

@RenzoF - thanks for pointing me to the Feed Service. I did it slightly differently since we already had a stream, I just added the GZipStream around it - slightly more efficient I think.

tank104 avatar Nov 14 '22 01:11 tank104