Amazon-SP-API-CSharp
Amazon-SP-API-CSharp copied to clipboard
FeedSerivce response document can sometimes be gzipped
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);
}
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);
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.
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
@tank104 please let us know so we can add this to nuget
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)
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
@tank104 do you solve the problem ?
@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
@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.