Files
bnhtrade/src/bnhtrade.Core/Data/Amazon/Feeds/SubmittFeed.cs
2024-04-11 12:26:13 +01:00

114 lines
4.6 KiB
C#

using bnhtrade.Core.Data.Amazon.SellingPartnerAPI;
using FikaAmazonAPI;
using FikaAmazonAPI.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static FikaAmazonAPI.Utils.Constants;
namespace bnhtrade.Core.Data.Amazon.Feeds
{
class SubmittFeed
{
private AmazonConnection amznConn = new SpApiConnection().Connection;
private Logic.Log.LogEvent logEvent = new Logic.Log.LogEvent();
public bool FeedSubmissionRecived { get; set; } = false;
// make these functions only accept a database object
public SubmittFeed()
{
}
public Model.Export.AmazonFeedSubmission FlatFileInvLoaderData(System.IO.MemoryStream filestream)
{
return Post("POST_FLAT_FILE_INVLOADER_DATA", filestream, "TXT");
}
private Model.Export.AmazonFeedSubmission Post(string feedType, System.IO.MemoryStream filestream, string fileExtension)
{
throw new NotImplementedException();
if (filestream.Length == 0) { throw new Exception("Filestreeam is empty"); }
if (string.IsNullOrEmpty(fileExtension)) { throw new Exception("Filestreeam is empty"); }
var feedresultTXT = amznConn.Feed.SubmitFeed(filestream.ToString()
, GetFeedType(feedType)
, new List<string>() { amznConn.GetCurrentMarketplace.ID }
, null
, ContentType.TXT);
string pathURL = string.Empty;
while (pathURL == string.Empty)
{
Thread.Sleep(1000 * 30);
var feedOutput = amznConn.Feed.GetFeed(feedresultTXT);
if (feedOutput.ProcessingStatus == FikaAmazonAPI.AmazonSpApiSDK.Models.Feeds.Feed.ProcessingStatusEnum.DONE)
{
var outPut = amznConn.Feed.GetFeedDocument(feedOutput.ResultFeedDocumentId);
pathURL = outPut.Url;
}
}
}
private FeedType GetFeedType(string feedTypeText)
{
// add more here as needed
if (feedTypeText == "POST_FLAT_FILE_INVLOADER_DATA")
{ return FeedType.POST_FLAT_FILE_INVLOADER_DATA; }
else
{ throw new NotImplementedException("Need to implement feed type for " + feedTypeText); }
}
public void SubmitFeed(Model.Export.AmazonFeedSubmission feedSubmission)
{
FeedSubmissionRecived = false;
if (feedSubmission.FileIsSet == false) { throw new Exception("Filestreeam is empty"); }
// get feed type
var feedType = GetFeedType(feedSubmission.FeedType);
string text = feedSubmission.File.FileData.ToString();
var feedresultTXT = amznConn.Feed.SubmitFeed(text
, feedType
, new List<string>() { amznConn.GetCurrentMarketplace.ID }
, null
, ContentType.TXT);
var result = new FikaAmazonAPI.AmazonSpApiSDK.Models.Feeds.FeedDocument();
int sleepTime = (1000 * 25);
int retryCount = 0;
int retryTimeout = 20;
UI.Console.Wait("Feed:" + feedType + " submitted to Amazon, awaiting status check.", 5000);
while (retryCount <= retryTimeout)
{
var feedOutput = amznConn.Feed.GetFeed(feedresultTXT);
if (feedOutput.ProcessingStatus == FikaAmazonAPI.AmazonSpApiSDK.Models.Feeds.Feed.ProcessingStatusEnum.DONE)
{
result = amznConn.Feed.GetFeedDocument(feedOutput.ResultFeedDocumentId);
break;
}
retryCount++;
UI.Console.Wait("Feed: " + feedType + ", Processing status: " + feedOutput.ProcessingStatus + ", Retry: " + retryCount + " of "+ retryTimeout, sleepTime);
}
// retry timeout
if (retryCount > retryTimeout)
{
logEvent.LogError("Feed submission timeout. Feedtype:" + feedType + " FeedSubmissionId:" + feedSubmission.FeedSubmissionId);
return;
}
// get and do somehting with this
string pathURL = string.Empty;
pathURL = result.Url;
}
}
}