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