async things, and other minor changes

This commit is contained in:
Tommy Parnell
2015-04-23 21:22:13 -04:00
parent 88ce5b7ac7
commit e537e1ad95
5 changed files with 70 additions and 20 deletions

View File

@@ -16,6 +16,7 @@ using Untappd.Net.Responses.UserInfo;
using Untappd.Net.Responses.VenueInfo;
using UserDistinctBeers = Untappd.Net.Responses.UserDistinctBeer;
using UserWishList = Untappd.Net.Responses.UserWishlist;
using System;
namespace Untappd.Net.UnitTests
{
@@ -125,5 +126,12 @@ namespace Untappd.Net.UnitTests
var json = File.ReadAllText(string.Format(ResponsePath, "UserActivityFeed.json"));
var jsonresult = JsonConvert.DeserializeObject<UserActivityFeed>(json);
}
[Test]
[ExpectedException(typeof(NotImplementedException))]
public void testNotImplementedException()
{
new SingleObjectArrayConverter<UserActivityFeed>().WriteJson(null, null, null);
}
}
}

View File

@@ -27,5 +27,15 @@ namespace Untappd.Net.UnitTests.Responses
&& myType.GetInterface("IRequest") != null).Select(type => (IRequest) Activator.CreateInstance(type)).ToList();
objects.ForEach(a=>Assert.IsNotNullOrEmpty(a.EndPoint("t")));
}
[Test]
public void RunAllEndpointsWithEmptyString()
{
var objects = Assembly.GetAssembly(typeof (IRequest)).GetTypes().Where(myType =>
myType.IsClass
&& !myType.IsAbstract
&& myType.GetInterface("IRequest") != null).Select(type => (IRequest) Activator.CreateInstance(type)).ToList();
objects.ForEach(a=>Assert.IsNotNullOrEmpty(a.EndPoint(string.Empty)));
}
}
}

View File

@@ -1,5 +1,6 @@
namespace Untappd.Net.Exception
{
[System.Serializable]
public class EndpointConfigurationException : BaseUntappdException
{
/// <summary>

View File

@@ -2,6 +2,8 @@
using Newtonsoft.Json;
using RestSharp;
using Untappd.Net.Client;
using System.Threading.Tasks;
using System.Threading;
namespace Untappd.Net.Request
{
@@ -22,10 +24,10 @@ namespace Untappd.Net.Request
Request = request;
}
internal void ConfigureGetRequest(string endPoint, IDictionary<string, string> bodyParameters = null)
internal void ConfigureGetRequest(string endPoint, Method webMethod = Method.GET, IDictionary<string, string> bodyParameters = null)
{
Request.Resource = endPoint;
Request.Method = Method.GET;
Request.Method = webMethod;
if (Request.Parameters != null) Request.Parameters.Clear();
if (bodyParameters == null) return;
@@ -48,14 +50,30 @@ namespace Untappd.Net.Request
where TResult : IUnAuthenticatedRequest,new()
{
var result = new TResult();
ConfigureGetRequest(result.EndPoint(urlParameter), bodyParameters);
ConfigureGetRequest(result.EndPoint(urlParameter), Method.GET, bodyParameters);
Request.AddParameter("client_id", credentials.ClientId);
Request.AddParameter("client_secret", credentials.ClientSecret);
return DoRestRequest<TResult>();
}
/// <summary>
/// Get the things! Async!
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="credentials"></param>
/// <param name="urlParameter"></param>
/// <param name="bodyParameters"></param>
/// <returns></returns>
public Task<TResult> GetAsync<TResult>(IUnAuthenticatedUntappdCredentials credentials, string urlParameter, IDictionary<string, string> bodyParameters = null)
where TResult : IUnAuthenticatedRequest, new()
{
var result = new TResult();
ConfigureGetRequest(result.EndPoint(urlParameter), Method.GET, bodyParameters);
Request.AddParameter("client_id", credentials.ClientId);
Request.AddParameter("client_secret", credentials.ClientSecret);
return DoRestRequestAsync<TResult>();
}
/// <summary>
/// Get the things! authenticated!
/// </summary>
@@ -68,26 +86,38 @@ namespace Untappd.Net.Request
where TResult : IAuthenticatedRequest, new()
{
var result = new TResult();
ConfigureGetRequest(result.EndPoint(urlParameter), bodyParameters);
ConfigureGetRequest(result.EndPoint(urlParameter), Method.GET, bodyParameters);
Request.AddParameter("access_token", credentials.AccessToken);
return DoRestRequest<TResult>();
}
/// <summary>
/// Get the things Authenticated! Async!!
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="credentials"></param>
/// <param name="urlParameter"></param>
/// <param name="bodyParameters"></param>
/// <returns></returns>
public Task<TResult> GetAsync<TResult>(IAuthenticatedUntappdCredentials credentials, string urlParameter = "", IDictionary<string, string> bodyParameters = null)
where TResult : IAuthenticatedRequest, new()
{
var result = new TResult();
ConfigureGetRequest(result.EndPoint(urlParameter), Method.GET, bodyParameters);
Request.AddParameter("access_token", credentials.AccessToken);
return DoRestRequestAsync<TResult>();
}
private TResult DoRestRequest<TResult>()
{
var client = new RestClient(Constants.BaseRequestString);
var resp = client.Execute(Request);
return JsonConvert.DeserializeObject<TResult>(resp.Content);
var response = Client.Execute(this.Request);
return JsonConvert.DeserializeObject<TResult>(response.Content);
}
//private async Task<TResult> DoRestRequestAsync<TResult>(IRestRequest request)
//{
// var client = new RestClient(Constants.BaseRequestString);
// var cancellationTokenSource = new CancellationTokenSource();
// var resp = await client.ExecuteTaskAsync(request, cancellationTokenSource.Token);
// return JsonConvert.DeserializeObject<TResult>(resp.Content);
//}
private async Task<TResult> DoRestRequestAsync<TResult>()
{
var response = await Client.ExecuteTaskAsync(Request);
return JsonConvert.DeserializeObject<TResult>(response.Content);
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using Newtonsoft.Json;
using Untappd.Net.Request;
namespace Untappd.Net
{
@@ -14,12 +15,12 @@ namespace Untappd.Net
{
public override bool CanConvert(Type objectType)
{
return true;
return objectType.IsAssignableFrom(typeof(IAuthenticatedRequest)) || objectType.IsAssignableFrom(typeof(IUnAuthenticatedRequest));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var retval = new Object();
var retval = new object();
switch (reader.TokenType)
{