Skip to content

Commit 025163b

Browse files
authored
Merge pull request #63 from kevinvenclovas/main
Optimize some code
2 parents 414fd7c + c018997 commit 025163b

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

Source/FikaAmazonAPI/AmazonSpApiSDK/Models/Exceptions/AmazonException.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,17 @@ public AmazonException(string msg, IRestResponse response = null) : base(msg)
2020

2121
}
2222

23-
public class NotFoundException : AmazonException
23+
public class AmazonNotFoundException : AmazonException
2424
{
25-
public NotFoundException(string msg, IRestResponse response = null) : base(msg, response)
25+
public AmazonNotFoundException(string msg, IRestResponse response = null) : base(msg, response)
26+
{
27+
28+
}
29+
}
30+
31+
public class AmazonUnauthorizedException : AmazonException
32+
{
33+
public AmazonUnauthorizedException(string msg, IRestResponse response = null) : base(msg, response)
2634
{
2735

2836
}

Source/FikaAmazonAPI/Services/FeedService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public CreateFeedDocumentResult CreateFeedDocument(ContentType contentType)
126126
/// <param name="xml"></param>
127127
/// <param name="feedType"></param>
128128
/// <returns></returns>
129-
public string SubmitFeed(string xml, FeedType feedType, FeedOptions feedOptions = null)
129+
public string SubmitFeed(string xml, FeedType feedType, List<string> marketPlaceIds = null, FeedOptions feedOptions = null)
130130
{
131131

132132
//FIrst Step get doc
@@ -139,7 +139,7 @@ public string SubmitFeed(string xml, FeedType feedType, FeedOptions feedOptions
139139
{
140140
FeedType = feedType.ToString(),
141141
InputFeedDocumentId = feedCreate.FeedDocumentId,
142-
MarketplaceIds = new List<string> { MarketPlace.ID },
142+
MarketplaceIds = marketPlaceIds ?? new List<string> { MarketPlace.ID },
143143
FeedOptions = feedOptions
144144
};
145145

@@ -148,6 +148,8 @@ public string SubmitFeed(string xml, FeedType feedType, FeedOptions feedOptions
148148

149149
return feed.FeedId;
150150
}
151+
152+
151153
private static Stream GetStreamFromUrl(string url)
152154
{
153155
byte[] imageData = null;

Source/FikaAmazonAPI/Services/RequestService.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,18 @@ protected void ParseResponse(IRestResponse response)
158158
if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Accepted || response.StatusCode == HttpStatusCode.Created)
159159
return;
160160
if (response.StatusCode == HttpStatusCode.NotFound)
161-
throw new NotFoundException("Resource that you are looking for is not found", response);
162-
else
161+
throw new AmazonNotFoundException("Resource that you are looking for is not found", response);
162+
if (response.StatusCode == HttpStatusCode.Forbidden)
163163
{
164-
Console.WriteLine("Amazon Api didn't respond with Okay, see exception for more details"+ response.Content);
165-
throw new AmazonException("Amazon Api didn't respond with Okay, see exception for more details", response);
164+
var error = response.Content.ConvertToErrorResponse();
165+
if (error != null && error.Errors.Any(x => x.Code == HttpStatusCode.Unauthorized))
166+
{
167+
throw new AmazonUnauthorizedException("Access to requested resource is denied.", response);
168+
}
166169
}
167-
170+
171+
Console.WriteLine("Amazon Api didn't respond with Okay, see exception for more details" + response.Content);
172+
throw new AmazonException("Amazon Api didn't respond with Okay, see exception for more details", response);
168173
}
169174

170175
protected void AddQueryParameters(List<KeyValuePair<string, string>> queryParameters)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Net;
4+
5+
namespace FikaAmazonAPI.Utils
6+
{
7+
public static class ErrorResponseHelper
8+
{
9+
public static ErrorResponse ConvertToErrorResponse(this string response)
10+
{
11+
if (String.IsNullOrEmpty(response)) return null;
12+
13+
try
14+
{
15+
return JsonConvert.DeserializeObject<ErrorResponse>(response);
16+
}
17+
catch
18+
{
19+
return null;
20+
}
21+
}
22+
}
23+
24+
25+
public class ErrorResponse
26+
{
27+
[JsonProperty("errors")]
28+
public ErrorResponseElement[] Errors;
29+
}
30+
31+
public class ErrorResponseElement
32+
{
33+
[JsonProperty("message")]
34+
public string Message { get; set; }
35+
[JsonProperty("code")]
36+
public HttpStatusCode Code { get; set; }
37+
[JsonProperty("details")]
38+
public string Details { get; set; }
39+
}
40+
}

0 commit comments

Comments
 (0)