Skip to content

Commit 53a392c

Browse files
committed
fix #466
1 parent 692d74a commit 53a392c

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

Source/FikaAmazonAPI/Services/FeedService.cs

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -196,22 +196,34 @@ public async Task<CreateFeedDocumentResult> CreateFeedDocumentAsync(ContentType
196196
return null;
197197
}
198198

199-
public string SubmitFeed(string XmlContentOrFilePath, FeedType feedType, List<string> marketPlaceIds = null, FeedOptions feedOptions = null, ContentType contentType = ContentType.XML) =>
200-
Task.Run(() => SubmitFeedAsync(XmlContentOrFilePath, feedType, marketPlaceIds, feedOptions, contentType)).ConfigureAwait(false).GetAwaiter().GetResult();
199+
public string SubmitFeedFile(string FilePath, FeedType feedType, List<string> marketPlaceIds = null, FeedOptions feedOptions = null, ContentType contentType = ContentType.XML) =>
200+
Task.Run(() => SubmitFeedAsync(FilePath, feedType, marketPlaceIds, feedOptions, contentType, ContentFormate.File)).ConfigureAwait(false).GetAwaiter().GetResult();
201+
202+
public async Task<string> SubmitFeedFileAsync(string FilePath, FeedType feedType, List<string> marketPlaceIds = null, FeedOptions feedOptions = null, ContentType contentType = ContentType.XML) =>
203+
await SubmitFeedAsync(FilePath, feedType, marketPlaceIds, feedOptions, contentType, ContentFormate.File);
204+
205+
public string SubmitFeedContent(string Content, FeedType feedType, List<string> marketPlaceIds = null, FeedOptions feedOptions = null, ContentType contentType = ContentType.XML) =>
206+
Task.Run(() => SubmitFeedAsync(Content, feedType, marketPlaceIds, feedOptions, contentType, ContentFormate.Text)).ConfigureAwait(false).GetAwaiter().GetResult();
207+
208+
public async Task<string> SubmitFeedContentAsync(string Content, FeedType feedType, List<string> marketPlaceIds = null, FeedOptions feedOptions = null, ContentType contentType = ContentType.XML) =>
209+
await SubmitFeedAsync(Content, feedType, marketPlaceIds, feedOptions, contentType, ContentFormate.Text);
210+
211+
public string SubmitFeed(string XmlContentOrFilePath, FeedType feedType, List<string> marketPlaceIds = null, FeedOptions feedOptions = null, ContentType contentType = ContentType.XML, ContentFormate contentFormate = ContentFormate.AutoDetect) =>
212+
Task.Run(() => SubmitFeedAsync(XmlContentOrFilePath, feedType, marketPlaceIds, feedOptions, contentType, contentFormate)).ConfigureAwait(false).GetAwaiter().GetResult();
201213

202214
/// <summary>
203215
/// read full step https://github.com/amzn/selling-partner-api-docs/blob/main/guides/en-US/use-case-guides/feeds-api-use-case-guide/feeds-api-use-case-guide_2021-06-30.md
204216
/// </summary>
205217
/// <param name="xml"></param>
206218
/// <param name="feedType"></param>
207219
/// <returns></returns>
208-
public async Task<string> SubmitFeedAsync(string feedContentOrFilePath, FeedType feedType, List<string> marketPlaceIds = null, FeedOptions feedOptions = null, ContentType contentType = ContentType.XML)
220+
public async Task<string> SubmitFeedAsync(string feedContentOrFilePath, FeedType feedType, List<string> marketPlaceIds = null, FeedOptions feedOptions = null, ContentType contentType = ContentType.XML, ContentFormate contentFormate = ContentFormate.AutoDetect)
209221
{
210222
//We are creating Feed Document
211223
var feedCreate = CreateFeedDocument(contentType);
212224

213225
//Uploading encoded invoice file
214-
_ = await PostFileDataAsync(feedCreate.Url, feedContentOrFilePath, contentType);
226+
_ = await PostFileDataAsync(feedCreate.Url, feedContentOrFilePath, contentType, contentFormate);
215227

216228
CreateFeedSpecification createFeedSpecification = new CreateFeedSpecification()
217229
{
@@ -238,51 +250,63 @@ private static async Task<Stream> GetStreamFromUrlAsync(string url)
238250
return new MemoryStream(imageData);
239251
}
240252

241-
private async Task<string> PostFileDataAsync(string destinationUrl, string contentOrFilePath, ContentType contentType = ContentType.XML)
253+
private async Task<string> PostFileDataAsync(string destinationUrl, string contentOrFilePath, ContentType contentType = ContentType.XML, ContentFormate contentFormate = ContentFormate.AutoDetect)
242254
{
243255
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
244256

245-
byte[] bytes;
246-
if (System.IO.Path.IsPathRooted(contentOrFilePath))
257+
byte[] bytes = null;
258+
if (contentFormate == ContentFormate.File)
247259
{
248-
// The string looks like a file path, so try to read the file
249-
if (System.IO.File.Exists(contentOrFilePath))
250-
{
251-
bytes = System.IO.File.ReadAllBytes(contentOrFilePath);
252-
}
253-
else
254-
{
255-
// The file does not exist, so treat the string as content
256-
bytes = System.Text.Encoding.UTF8.GetBytes(contentOrFilePath);
257-
}
260+
bytes = System.IO.File.ReadAllBytes(contentOrFilePath);
261+
}
262+
else if (contentFormate == ContentFormate.Text)
263+
{
264+
bytes = System.Text.Encoding.UTF8.GetBytes(contentOrFilePath);
258265
}
259-
else if (Uri.IsWellFormedUriString(contentOrFilePath, UriKind.RelativeOrAbsolute))
266+
else if (contentFormate == ContentFormate.AutoDetect)
260267
{
261-
// The string looks like a URI, so try to parse it as a file URI
262-
var uri = new Uri(contentOrFilePath);
263-
if (uri.IsFile)
268+
if (System.IO.Path.IsPathRooted(contentOrFilePath))
264269
{
265-
if (System.IO.File.Exists(uri.LocalPath))
270+
// The string looks like a file path, so try to read the file
271+
if (System.IO.File.Exists(contentOrFilePath))
266272
{
267-
bytes = System.IO.File.ReadAllBytes(uri.LocalPath);
273+
bytes = System.IO.File.ReadAllBytes(contentOrFilePath);
268274
}
269275
else
270276
{
271277
// The file does not exist, so treat the string as content
272278
bytes = System.Text.Encoding.UTF8.GetBytes(contentOrFilePath);
273279
}
274280
}
281+
else if (Uri.IsWellFormedUriString(contentOrFilePath, UriKind.RelativeOrAbsolute))
282+
{
283+
// The string looks like a URI, so try to parse it as a file URI
284+
var uri = new Uri(contentOrFilePath);
285+
if (uri.IsFile)
286+
{
287+
if (System.IO.File.Exists(uri.LocalPath))
288+
{
289+
bytes = System.IO.File.ReadAllBytes(uri.LocalPath);
290+
}
291+
else
292+
{
293+
// The file does not exist, so treat the string as content
294+
bytes = System.Text.Encoding.UTF8.GetBytes(contentOrFilePath);
295+
}
296+
}
297+
else
298+
{
299+
// The URI is not a file URI, so treat the string as content
300+
bytes = System.Text.Encoding.UTF8.GetBytes(contentOrFilePath);
301+
}
302+
}
275303
else
276304
{
277-
// The URI is not a file URI, so treat the string as content
305+
// The string is not a file path or a URI, so treat it as content
278306
bytes = System.Text.Encoding.UTF8.GetBytes(contentOrFilePath);
279307
}
280308
}
281-
else
282-
{
283-
// The string is not a file path or a URI, so treat it as content
284-
bytes = System.Text.Encoding.UTF8.GetBytes(contentOrFilePath);
285-
}
309+
286310

287311
request.ContentType = LinqHelper.GetEnumMemberValue(contentType);
288312
request.ContentLength = bytes.Length;

Source/FikaAmazonAPI/Utils/Constants.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,13 @@ public enum ContentType
542542
TXT,
543543
}
544544

545+
public enum ContentFormate
546+
{
547+
AutoDetect,
548+
File,
549+
Text
550+
}
551+
545552
[JsonConverter(typeof(StringEnumConverter))]
546553
public enum FeedMessageType
547554
{

0 commit comments

Comments
 (0)