Skip to content

Commit 4867bd6

Browse files
committed
add debug mode
1 parent 9232a9a commit 4867bd6

File tree

4 files changed

+43
-20
lines changed

4 files changed

+43
-20
lines changed

Source/FikaAmazonAPI.SampleCode/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static async Task Main(string[] args)
3636
ClientSecret = config.GetSection("FikaAmazonAPI:ClientSecret").Value,
3737
RefreshToken = config.GetSection("FikaAmazonAPI:RefreshToken").Value,
3838
MarketPlace = MarketPlace.GetMarketPlaceByID(config.GetSection("FikaAmazonAPI:MarketPlaceID").Value),
39+
IsDebugMode=true
3940
});
4041

4142

Source/FikaAmazonAPI/AmazonCredential.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class AmazonCredential
2020
public Environments Environment { get; set; } = Environments.Production;
2121
public int MaxThrottledRetryCount { get; set; } = 3;
2222
public ShippingBusiness? ShippingBusiness { get; set; }
23+
public bool IsDebugMode { get; set; }
2324

2425
public AmazonCredential()
2526
{

Source/FikaAmazonAPI/Services/RequestService.cs

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using System.Linq;
1414
using System.Net;
1515
using System.Threading.Tasks;
16-
using Newtonsoft.Json.Serialization;
1716
using RestSharp.Serializers.NewtonsoftJson;
1817
using static FikaAmazonAPI.AmazonSpApiSDK.Models.Token.CacheTokenData;
1918
using static FikaAmazonAPI.Utils.Constants;
@@ -111,6 +110,7 @@ protected void CreateAuthorizedPagedRequest(AmazonFilter filter, string url, Res
111110

112111
Request = await TokenGeneration.SignWithSTSKeysAndSecurityTokenAsync(Request, RequestClient.Options.BaseUrl.Host, AmazonCredential);
113112
var response = await RequestClient.ExecuteAsync<T>(Request);
113+
LogRequest(Request, response);
114114
SaveLastRequestHeader(response.Headers);
115115
SleepForRateLimit(response.Headers, rateLimitType);
116116
ParseResponse(response);
@@ -131,7 +131,41 @@ private void SaveLastRequestHeader(IReadOnlyCollection<RestSharp.HeaderParameter
131131
LastHeaders.Add(new KeyValuePair<string, string>(parameter.Name.ToString(), parameter.Value.ToString()));
132132
}
133133
}
134+
}
134135

136+
private void LogRequest(RestRequest request,RestResponse response)
137+
{
138+
if (AmazonCredential.IsDebugMode)
139+
{
140+
var requestToLog = new
141+
{
142+
resource = request.Resource,
143+
parameters = request.Parameters.Select(parameter => new
144+
{
145+
name = parameter.Name,
146+
value = parameter.Value,
147+
type = parameter.Type.ToString()
148+
}),
149+
// ToString() here to have the method as a nice string otherwise it will just show the enum value
150+
method = request.Method.ToString(),
151+
// This will generate the actual Uri used in the request
152+
//uri = request. _restClient.BuildUri(request),
153+
};
154+
155+
var responseToLog = new
156+
{
157+
statusCode = response.StatusCode,
158+
content = response.Content,
159+
headers = response.Headers,
160+
// The Uri that actually responded (could be different from the requestUri if a redirection occurred)
161+
responseUri = response.ResponseUri,
162+
errorMessage = response.ErrorMessage,
163+
};
164+
165+
Console.WriteLine(string.Format("Request completed, Request: {1}, Response: {2}",
166+
JsonConvert.SerializeObject(requestToLog),
167+
JsonConvert.SerializeObject(responseToLog)));
168+
}
135169
}
136170
private void RestHeader()
137171
{
@@ -164,9 +198,9 @@ private void RestHeader()
164198
{
165199
if (tryCount >= AmazonCredential.MaxThrottledRetryCount)
166200
{
167-
#if DEBUG
168-
Console.WriteLine("Throttle max try count reached");
169-
#endif
201+
if(AmazonCredential.IsDebugMode)
202+
Console.WriteLine("Throttle max try count reached");
203+
170204
throw;
171205
}
172206

@@ -214,19 +248,6 @@ private void SleepForRateLimit(IReadOnlyCollection<RestSharp.Parameter> headers,
214248
}
215249
}
216250

217-
218-
protected async Task<T> ExecuteUnAuthorizedRequest<T>() where T : new()
219-
{
220-
var response = await RequestClient.ExecuteAsync<T>(Request);
221-
ParseResponse(response);
222-
SaveLastRequestHeader(response.Headers);
223-
if (response.StatusCode == HttpStatusCode.OK && !string.IsNullOrEmpty(response.Content) && response.Data == null)
224-
{
225-
response.Data = JsonConvert.DeserializeObject<T>(response.Content);
226-
}
227-
return response.Data;
228-
}
229-
230251
protected void ParseResponse(RestResponse response)
231252
{
232253
if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Accepted || response.StatusCode == HttpStatusCode.Created)
@@ -237,7 +258,8 @@ protected void ParseResponse(RestResponse response)
237258
}
238259
else
239260
{
240-
Console.WriteLine("Amazon Api didn't respond with Okay, see exception for more details" + response.Content);
261+
if (AmazonCredential.IsDebugMode)
262+
Console.WriteLine("Amazon Api didn't respond with Okay, see exception for more details" + response.Content);
241263

242264
var errorResponse = response.Content.ConvertToErrorResponse();
243265
if (errorResponse != null)

Source/FikaAmazonAPI/Utils/RateLimits.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ public RateLimits NextRate(RateLimitType rateLimitType)
2626

2727
int ratePeriodMs = GetRatePeriodMs();
2828

29-
#if DEBUG
3029
var nextRequestsSent = RequestsSent + 1;
3130
var nextRequestsSentTxt = (nextRequestsSent > Burst) ? "FULL" : nextRequestsSent.ToString();
3231
string output = $"[RateLimits ,{rateLimitType,15}]: {DateTime.UtcNow.ToString(),10}\t Request/Burst: {nextRequestsSentTxt}/{Burst}\t Rate: {Rate}/{ratePeriodMs}ms";
3332
Console.WriteLine(output);
34-
#endif
33+
3534

3635
if (RequestsSent >= Burst)
3736
{

0 commit comments

Comments
 (0)