Skip to content

Commit 945bf09

Browse files
author
Andrew Thomas
committed
Changed to allow sending XML as file.
1 parent 33642a4 commit 945bf09

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
@@ -138,11 +138,7 @@ public async Task<ProcessingReportMessage> GetFeedDocumentProcessingReportAsync(
138138
}
139139
catch (AmazonProcessingReportDeserializeException ex)
140140
{
141-
throw ex;
142-
}
143-
catch (Exception ex)
144-
{
145-
141+
throw;
146142
}
147143
return processingReport;
148144
}
@@ -171,30 +167,14 @@ public string SubmitFeed(string XmlContentOrFilePath, FeedType feedType, List<st
171167
/// <param name="xml"></param>
172168
/// <param name="feedType"></param>
173169
/// <returns></returns>
174-
public async Task<string> SubmitFeedAsync(string XmlContentOrFilePath, FeedType feedType, List<string> marketPlaceIds = null, FeedOptions feedOptions = null, ContentType contentType = ContentType.XML)
170+
public async Task<string> SubmitFeedAsync(string feedContentOrFilePath, FeedType feedType, List<string> marketPlaceIds = null, FeedOptions feedOptions = null, ContentType contentType = ContentType.XML)
175171
{
176-
177172
//We are creating Feed Document
178173
var feedCreate = CreateFeedDocument(contentType);
179174

180175
//Uploading encoded invoice file
181-
if (contentType == ContentType.PDF)
182-
{
183-
_ = await PostFileDataAsync(feedCreate.Url, XmlContentOrFilePath, contentType);
184-
}
185-
else if (contentType == ContentType.JSON)
186-
{
187-
_ = await PostFileDataAsync(feedCreate.Url, XmlContentOrFilePath, contentType);
188-
}
189-
else if (contentType == ContentType.TXT)
190-
{
191-
_ = await PostFileDataAsync(feedCreate.Url, XmlContentOrFilePath, contentType);
192-
}
193-
else
194-
{
195-
_ = await PostXMLDataAsync(feedCreate.Url, XmlContentOrFilePath);
196-
}
197-
176+
_ = await PostFileDataAsync(feedCreate.Url, feedContentOrFilePath, contentType);
177+
198178
CreateFeedSpecification createFeedSpecification = new CreateFeedSpecification()
199179
{
200180
FeedType = feedType.ToString(),
@@ -220,45 +200,29 @@ private static async Task<Stream> GetStreamFromUrlAsync(string url)
220200
return new MemoryStream(imageData);
221201
}
222202

223-
private async Task<string> PostXMLDataAsync(string destinationUrl, string requestXml, ContentType contentType = ContentType.XML)
203+
private async Task<string> PostFileDataAsync(string destinationUrl, string contentOrFilePath, ContentType contentType = ContentType.XML)
224204
{
225205
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
226-
byte[] bytes;
227-
bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
228-
request.ContentType = LinqHelper.GetEnumMemberValue(contentType);
229-
request.ContentLength = bytes.Length;
230-
request.Method = "PUT";
231-
Stream requestStream = await request.GetRequestStreamAsync();
232-
requestStream.Write(bytes, 0, bytes.Length);
233-
requestStream.Close();
234-
HttpWebResponse response;
235-
response = (HttpWebResponse)await request.GetResponseAsync();
236-
if (response.StatusCode == HttpStatusCode.OK)
237-
{
238-
Stream responseStream = response.GetResponseStream();
239-
string responseStr = new StreamReader(responseStream).ReadToEnd();
240-
return responseStr;
241-
}
242-
return null;
243-
}
244206

245-
private async Task<string> PostFileDataAsync(string destinationUrl, string pathFile, ContentType contentType = ContentType.XML)
246-
{
247-
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
248-
byte[] bytes = File.ReadAllBytes(pathFile);
249-
//bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
207+
byte[] bytes;
208+
if (Uri.IsWellFormedUriString(contentOrFilePath, UriKind.RelativeOrAbsolute))
209+
bytes = File.ReadAllBytes(contentOrFilePath);
210+
else
211+
bytes = System.Text.Encoding.UTF8.GetBytes(contentOrFilePath);
250212
request.ContentType = LinqHelper.GetEnumMemberValue(contentType);
251213
request.ContentLength = bytes.Length;
252214
request.Method = "PUT";
253-
Stream requestStream = request.GetRequestStream();
254-
requestStream.Write(bytes, 0, bytes.Length);
255-
requestStream.Close();
256-
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
257-
if (response.StatusCode == HttpStatusCode.OK)
215+
using (Stream requestStream = request.GetRequestStream())
258216
{
259-
Stream responseStream = response.GetResponseStream();
260-
string responseStr = new StreamReader(responseStream).ReadToEnd();
261-
return responseStr;
217+
requestStream.Write(bytes, 0, bytes.Length);
218+
requestStream.Close();
219+
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
220+
if (response.StatusCode == HttpStatusCode.OK)
221+
{
222+
Stream responseStream = response.GetResponseStream();
223+
string responseStr = await new StreamReader(responseStream).ReadToEndAsync();
224+
return responseStr;
225+
}
262226
}
263227
return null;
264228
}

0 commit comments

Comments
 (0)