Skip to content

Commit 17cdcf1

Browse files
authored
Merge pull request #401 from tank104/AllowSendingXmlAsFile
Changed to allow sending XML as file.
2 parents db98ab3 + 945bf09 commit 17cdcf1

File tree

1 file changed

+20
-56
lines changed

1 file changed

+20
-56
lines changed

Source/FikaAmazonAPI/Services/FeedService.cs

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,7 @@ public async Task<ProcessingReportMessage> GetFeedDocumentProcessingReportAsync(
140140
}
141141
catch (AmazonProcessingReportDeserializeException ex)
142142
{
143-
throw ex;
144-
}
145-
catch (Exception ex)
146-
{
147-
143+
throw;
148144
}
149145
return processingReport;
150146
}
@@ -209,30 +205,14 @@ public string SubmitFeed(string XmlContentOrFilePath, FeedType feedType, List<st
209205
/// <param name="xml"></param>
210206
/// <param name="feedType"></param>
211207
/// <returns></returns>
212-
public async Task<string> SubmitFeedAsync(string XmlContentOrFilePath, FeedType feedType, List<string> marketPlaceIds = null, FeedOptions feedOptions = null, ContentType contentType = ContentType.XML)
208+
public async Task<string> SubmitFeedAsync(string feedContentOrFilePath, FeedType feedType, List<string> marketPlaceIds = null, FeedOptions feedOptions = null, ContentType contentType = ContentType.XML)
213209
{
214-
215210
//We are creating Feed Document
216211
var feedCreate = CreateFeedDocument(contentType);
217212

218213
//Uploading encoded invoice file
219-
if (contentType == ContentType.PDF)
220-
{
221-
_ = await PostFileDataAsync(feedCreate.Url, XmlContentOrFilePath, contentType);
222-
}
223-
else if (contentType == ContentType.JSON)
224-
{
225-
_ = await PostFileDataAsync(feedCreate.Url, XmlContentOrFilePath, contentType);
226-
}
227-
else if (contentType == ContentType.TXT)
228-
{
229-
_ = await PostFileDataAsync(feedCreate.Url, XmlContentOrFilePath, contentType);
230-
}
231-
else
232-
{
233-
_ = await PostXMLDataAsync(feedCreate.Url, XmlContentOrFilePath);
234-
}
235-
214+
_ = await PostFileDataAsync(feedCreate.Url, feedContentOrFilePath, contentType);
215+
236216
CreateFeedSpecification createFeedSpecification = new CreateFeedSpecification()
237217
{
238218
FeedType = feedType.ToString(),
@@ -258,45 +238,29 @@ private static async Task<Stream> GetStreamFromUrlAsync(string url)
258238
return new MemoryStream(imageData);
259239
}
260240

261-
private async Task<string> PostXMLDataAsync(string destinationUrl, string requestXml, ContentType contentType = ContentType.XML)
241+
private async Task<string> PostFileDataAsync(string destinationUrl, string contentOrFilePath, ContentType contentType = ContentType.XML)
262242
{
263243
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
264-
byte[] bytes;
265-
bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
266-
request.ContentType = LinqHelper.GetEnumMemberValue(contentType);
267-
request.ContentLength = bytes.Length;
268-
request.Method = "PUT";
269-
Stream requestStream = await request.GetRequestStreamAsync();
270-
requestStream.Write(bytes, 0, bytes.Length);
271-
requestStream.Close();
272-
HttpWebResponse response;
273-
response = (HttpWebResponse)await request.GetResponseAsync();
274-
if (response.StatusCode == HttpStatusCode.OK)
275-
{
276-
Stream responseStream = response.GetResponseStream();
277-
string responseStr = new StreamReader(responseStream).ReadToEnd();
278-
return responseStr;
279-
}
280-
return null;
281-
}
282244

283-
private async Task<string> PostFileDataAsync(string destinationUrl, string pathFile, ContentType contentType = ContentType.XML)
284-
{
285-
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
286-
byte[] bytes = File.ReadAllBytes(pathFile);
287-
//bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
245+
byte[] bytes;
246+
if (Uri.IsWellFormedUriString(contentOrFilePath, UriKind.RelativeOrAbsolute))
247+
bytes = File.ReadAllBytes(contentOrFilePath);
248+
else
249+
bytes = System.Text.Encoding.UTF8.GetBytes(contentOrFilePath);
288250
request.ContentType = LinqHelper.GetEnumMemberValue(contentType);
289251
request.ContentLength = bytes.Length;
290252
request.Method = "PUT";
291-
Stream requestStream = request.GetRequestStream();
292-
requestStream.Write(bytes, 0, bytes.Length);
293-
requestStream.Close();
294-
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
295-
if (response.StatusCode == HttpStatusCode.OK)
253+
using (Stream requestStream = request.GetRequestStream())
296254
{
297-
Stream responseStream = response.GetResponseStream();
298-
string responseStr = new StreamReader(responseStream).ReadToEnd();
299-
return responseStr;
255+
requestStream.Write(bytes, 0, bytes.Length);
256+
requestStream.Close();
257+
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
258+
if (response.StatusCode == HttpStatusCode.OK)
259+
{
260+
Stream responseStream = response.GetResponseStream();
261+
string responseStr = await new StreamReader(responseStream).ReadToEndAsync();
262+
return responseStr;
263+
}
300264
}
301265
return null;
302266
}

0 commit comments

Comments
 (0)