add comment

This commit is contained in:
Tommy Parnell
2015-09-05 13:07:47 -04:00
parent ea03c9ff1f
commit cc56b8cc73

View File

@@ -9,117 +9,118 @@ using Untappd.Net.Exception;
namespace Untappd.Net.Request namespace Untappd.Net.Request
{ {
public sealed partial class Repository public sealed partial class Repository
{ {
internal IRestClient Client; internal IRestClient Client;
internal IRestRequest Request; internal IRestRequest Request;
public bool FailFast { get; set; } public bool FailFast { get; set; }
/// <summary> /// <summary>
/// Event to listen to when failFast is set to false /// Event to listen to when failFast is set to false
/// This allows you to capture the excpetion, before its swallowed /// This allows you to capture the excpetion, before its swallowed
/// </summary> /// </summary>
public event UnhandledExceptionEventHandler OnExceptionThrown; public event UnhandledExceptionEventHandler OnExceptionThrown;
/// <summary> /// <summary>
/// Make a repository /// Make a repository
/// </summary> /// </summary>
/// <param name="failFast">Should we throw exceptions? or just return null</param> /// <param name="failFast">Should we throw exceptions? or just return null</param>
public Repository(bool failFast = true, int timeout = 0) /// <param name="timeout">Set a timeout in milliseconds</param>
{ public Repository(bool failFast = true, int timeout = 0)
Client = new RestClient(Constants.BaseRequestString); {
Request = new RestRequest Client = new RestClient(Constants.BaseRequestString);
{ Request = new RestRequest
Timeout = timeout {
}; Timeout = timeout
FailFast = failFast; };
} FailFast = failFast;
}
[Obsolete("This constructor is used for mocking purposes only", false)] [Obsolete("This constructor is used for mocking purposes only", false)]
internal Repository(IRestClient client, IRestRequest request) internal Repository(IRestClient client, IRestRequest request)
{ {
Client = client; Client = client;
Request = request; Request = request;
} }
internal Repository ConfigureRequest(string endPoint, IDictionary<string, object> bodyParameters = null, Method webMethod = Method.GET) internal Repository ConfigureRequest(string endPoint, IDictionary<string, object> bodyParameters = null, Method webMethod = Method.GET)
{ {
Request.Resource = endPoint; Request.Resource = endPoint;
Request.Method = webMethod; Request.Method = webMethod;
if (Request.Parameters != null && Request.Parameters.Count > 0) if (Request.Parameters != null && Request.Parameters.Count > 0)
{ {
Request.Parameters.Clear(); Request.Parameters.Clear();
} }
if (bodyParameters == null) return this; if (bodyParameters == null) return this;
foreach (var param in bodyParameters) foreach (var param in bodyParameters)
{ {
Request.AddParameter(param.Key, param.Value); Request.AddParameter(param.Key, param.Value);
} }
return this; return this;
} }
internal Repository ConfigureRequest(IUntappdCredentials credentials, string endPoint, IDictionary<string, object> bodyParameters = null, Method webMethod = Method.GET) internal Repository ConfigureRequest(IUntappdCredentials credentials, string endPoint, IDictionary<string, object> bodyParameters = null, Method webMethod = Method.GET)
{ {
ConfigureRequest(endPoint, bodyParameters, webMethod); ConfigureRequest(endPoint, bodyParameters, webMethod);
foreach (var untappdCredential in credentials.AuthenticationData) foreach (var untappdCredential in credentials.AuthenticationData)
{ {
Request.AddParameter(untappdCredential.Key, untappdCredential.Value); Request.AddParameter(untappdCredential.Key, untappdCredential.Value);
} }
return this; return this;
} }
private TResult ExecuteRequest<TResult>() private TResult ExecuteRequest<TResult>()
where TResult : class where TResult : class
{ {
return ProcessExecution<TResult>(Client.Execute(Request)); return ProcessExecution<TResult>(Client.Execute(Request));
} }
private async Task<TResult> ExecuteRequestAsync<TResult>() private async Task<TResult> ExecuteRequestAsync<TResult>()
where TResult : class where TResult : class
{ {
return ProcessExecution<TResult>(await Client.ExecuteTaskAsync(Request)); return ProcessExecution<TResult>(await Client.ExecuteTaskAsync(Request));
} }
private TResult ProcessExecution<TResult>(IRestResponse response) private TResult ProcessExecution<TResult>(IRestResponse response)
where TResult : class where TResult : class
{ {
//if the return type is not 200 throw errors //if the return type is not 200 throw errors
if (response.StatusCode != HttpStatusCode.OK) if (response.StatusCode != HttpStatusCode.OK)
{ {
var excpetion = new HttpErrorException(Request, response); var excpetion = new HttpErrorException(Request, response);
var eventThrow = OnExceptionThrown; var eventThrow = OnExceptionThrown;
if (eventThrow != null) if (eventThrow != null)
{ {
eventThrow(this, new UnhandledExceptionEventArgs(excpetion, FailFast)); eventThrow(this, new UnhandledExceptionEventArgs(excpetion, FailFast));
} }
if (FailFast) if (FailFast)
{ {
throw excpetion; throw excpetion;
} }
return null; return null;
} }
//try to deserialize //try to deserialize
try try
{ {
return JsonConvert.DeserializeObject<TResult>(response.Content); return JsonConvert.DeserializeObject<TResult>(response.Content);
} }
catch (System.Exception e) catch (System.Exception e)
{ {
var eventThrow = OnExceptionThrown; var eventThrow = OnExceptionThrown;
if (eventThrow != null) if (eventThrow != null)
{ {
eventThrow(this, new UnhandledExceptionEventArgs(e, FailFast)); eventThrow(this, new UnhandledExceptionEventArgs(e, FailFast));
} }
if (FailFast) if (FailFast)
{ {
throw; throw;
} }
return null; return null;
} }
} }
} }
} }