stopping for now

This commit is contained in:
Tommy Parnell
2015-05-11 18:55:31 -04:00
parent 843e1a63f0
commit 022d8b683e
14 changed files with 191 additions and 85 deletions

2
.gitignore vendored
View File

@@ -11,6 +11,8 @@
*.userprefs
**/.settings
NDependOut
*.ndproj
# Build results
[Dd]ebug/
[Dd]ebugPublic/

View File

@@ -21,7 +21,7 @@ namespace Untappd.Net.UnitTests.Client
Assert.AreEqual(token, "awesome");
token = "newString";
//Make sure the reference is not copied over
Assert.AreEqual("awesome", t.AccessToken);
Assert.AreEqual("awesome", t.AuthenticationData["access_token"]);
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Moq;
using Newtonsoft.Json;
@@ -19,8 +20,11 @@ namespace Untappd.Net.UnitTests.Request
public void ConfirmRequestWorks()
{
var mockCreds = new Mock<IUnAuthenticatedUntappdCredentials>();
mockCreds.Setup(a => a.ClientId).Returns("id");
mockCreds.Setup(a => a.ClientSecret).Returns("secret");
mockCreds.Setup(a => a.AuthenticationData).Returns(new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()
{
{"client_id", "id"},
{"client_secret", "secret"}
}));
var bodyParam = new Dictionary<string, object> {{"key", "value"}};
var client = new Mock<IRestClient>();
var request = new Mock<IRestRequest>();
@@ -38,16 +42,19 @@ namespace Untappd.Net.UnitTests.Request
var repository = new Repository(client.Object, request.Object);
repository.Get<BeerInfo>(mockCreds.Object, "awesome", bodyParam);
request.Verify(a => a.AddParameter("client_id", mockCreds.Object.ClientId));
request.Verify(a => a.AddParameter("client_secret", mockCreds.Object.ClientSecret));
request.Verify(a => a.AddParameter("client_id", mockCreds.Object.AuthenticationData["client_id"]));
request.Verify(a => a.AddParameter("client_secret", mockCreds.Object.AuthenticationData["client_secret"]));
request.Verify(a => a.AddParameter("key", "value"));
repository.GetAsync<BeerInfo>(mockCreds.Object, "awesome", bodyParam).Wait();
request.Verify(a => a.AddParameter("client_id", mockCreds.Object.ClientId));
request.Verify(a => a.AddParameter("client_secret", mockCreds.Object.ClientSecret));
request.Verify(a => a.AddParameter("client_id", mockCreds.Object.AuthenticationData["client_id"]));
request.Verify(a => a.AddParameter("client_secret", mockCreds.Object.AuthenticationData["client_secret"]));
request.Verify(a => a.AddParameter("key", "value"));
var mockAuthCreds = new Mock<IAuthenticatedUntappdCredentials>();
mockAuthCreds.Setup(a => a.AccessToken).Returns("accessToken");
mockAuthCreds.Setup(a => a.AuthenticationData).Returns(new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()
{
{"access_token", "accessToken"}
}));
repository.Get<BeerInfo>(mockAuthCreds.Object, "awesome", bodyParam);
request.Verify(a => a.AddParameter("key", "value"));
@@ -56,12 +63,18 @@ namespace Untappd.Net.UnitTests.Request
request.Verify(a => a.AddParameter("key", "value"));
request.Verify(a => a.AddParameter("access_token", "accessToken"));
mockAuthCreds.Setup(a => a.AccessToken).Returns("PostaccessToken");
mockAuthCreds.Setup(a => a.AuthenticationData).Returns(new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()
{
{"access_token", "PostaccessToken"}
}));
var checkin = new CheckIn("-5", "EST", 1044097) { Shout = "Awesome Brew", Rating = 4 };
repository.Post(mockAuthCreds.Object, checkin);
request.Verify(a => a.AddParameter("access_token", "PostaccessToken"));
mockAuthCreds.Setup(a => a.AccessToken).Returns("PostAsyncaccessToken");
mockAuthCreds.Setup(a => a.AuthenticationData).Returns(new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()
{
{"access_token", "PostAsyncaccessToken"}
}));
repository.PostAsync(mockAuthCreds.Object, checkin).Wait();
request.Verify(a => a.AddParameter("access_token", "PostAsyncaccessToken"));
}

View File

@@ -24,7 +24,7 @@ namespace Untappd.Net.Authentication
}
return string.Format("{0}/?client_id={1}&response_type=code&redirect_url={2}", Constants.BaseRequestString,
credentials.ClientId, redirectUrl);
credentials.AuthenticationData["client_id"], redirectUrl);
}
/// <summary>
@@ -51,8 +51,8 @@ namespace Untappd.Net.Authentication
}
return string.Format("{0}/?client_id={1}&client_secret={2}&response_type=code&redirect_url={3}&code={4}",
Constants.OAuthTokenEndPoint,
credentials.ClientId,
credentials.ClientSecret,
credentials.AuthenticationData["client_id"],
credentials.AuthenticationData["client_secret"],
redirectUrl,
code);
}

View File

@@ -1,23 +1,25 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Untappd.Net.Client
{
public class AuthenticatedUntappdCredentials : UntappdCredentials, IAuthenticatedUntappdCredentials
{
public string AccessToken { get; private set; }
/// <summary>
/// Pass your authenticated access token
/// </summary>
/// <param name="accessToken"></param>
/// <param name="clientId"></param>
/// <param name="clientSecret"></param>
public AuthenticatedUntappdCredentials(string accessToken)
{
if (string.IsNullOrWhiteSpace(accessToken))
{
throw new ArgumentNullException("accessToken");
}
AccessToken = string.Copy(accessToken);
AuthenticationData = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()
{
{"access_token", accessToken}
});
}
}
}

View File

@@ -2,6 +2,5 @@
{
public interface IAuthenticatedUntappdCredentials : IUntappdCredentials
{
string AccessToken { get; }
}
}

View File

@@ -1,8 +1,10 @@
namespace Untappd.Net.Client
using System.Collections;
using System.Collections.Generic;
namespace Untappd.Net.Client
{
public interface IUntappdCredentials
{
string ClientId { get; }
string ClientSecret { get; }
IReadOnlyDictionary<string, string> AuthenticationData { get; }
}
}

View File

@@ -1,4 +1,8 @@
namespace Untappd.Net.Client
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Untappd.Net.Client
{
public class UnAuthenticatedUntappdCredentials : UntappdCredentials, IUnAuthenticatedUntappdCredentials
{
@@ -8,8 +12,19 @@
/// <param name="clientId"></param>
/// <param name="clientSecret"></param>
public UnAuthenticatedUntappdCredentials(string clientId, string clientSecret)
: base(clientId, clientSecret)
{
{
if (string.IsNullOrWhiteSpace(clientId))
{
throw new ArgumentNullException("clientId");
}
if (string.IsNullOrWhiteSpace(clientSecret))
{
throw new ArgumentNullException("clientSecret");
}
AuthenticationData = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()
{
{"client_id", clientId}, {"client_secret", clientSecret}
});
}
}
}

View File

@@ -1,33 +1,11 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Untappd.Net.Client
{
public abstract class UntappdCredentials : IUntappdCredentials
{
public string ClientId { get; private set; }
public string ClientSecret { get; private set; }
protected UntappdCredentials()
{
}
/// <summary>
/// UnAuthenticated request. Pass your API id and secret
/// </summary>
/// <param name="clientId"></param>
/// <param name="clientSecret"></param>
protected UntappdCredentials(string clientId, string clientSecret)
{
if (string.IsNullOrWhiteSpace(clientId))
{
throw new ArgumentNullException("clientId");
}
if (string.IsNullOrWhiteSpace(clientSecret))
{
throw new ArgumentNullException("clientSecret");
}
ClientId = string.Copy(clientId);
ClientSecret = string.Copy(clientSecret);
}
public IReadOnlyDictionary<string, string> AuthenticationData { get; protected set; }
}
}

View File

@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RestSharp;
namespace Untappd.Net.Exception
{
public sealed class HttpErrorException : BaseUntappdException
{
public override string Message
{
get
{
return _message;
}
}
private readonly string _message;
public HttpErrorException(IRestRequest request, IRestResponse response)
{
var code = (int) response.StatusCode;
if (code == 200)
{
throw new BaseUntappdException(
"HttpError is being throw with a 200 error. Something has gone horribly wrong");
}
_message = string.Format("HttpError {0} was returned with Message: {1}{2}", code, Environment.NewLine,
response.ErrorMessage);
Data.Add("Request Object", JsonConvert.SerializeObject(request));
Data.Add("Response Object", JsonConvert.SerializeObject(response));
}
}
}

View File

@@ -1,8 +1,12 @@
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using Untappd.Net.Client;
using Untappd.Net.Exception;
using System;
namespace Untappd.Net.Request
{
@@ -10,57 +14,105 @@ namespace Untappd.Net.Request
{
internal IRestClient Client;
internal IRestRequest Request;
private 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;
public Repository()
/// <summary>
/// Make a repository
/// </summary>
/// <param name="failFast">Should we throw exceptions? or just return null</param>
public Repository(bool failFast = true)
{
Client = new RestClient(Constants.BaseRequestString);
Request = new RestRequest();
FailFast = failFast;
}
[Obsolete("This constructor is used for mocking purposes only", false)]
internal Repository(IRestClient client, IRestRequest request)
{
Client = client;
Request = request;
}
internal void 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.Method = webMethod;
if (Request.Parameters != null) Request.Parameters.Clear();
if (Request.Parameters != null && Request.Parameters.Count > 0)
{
Request.Parameters.Clear();
}
if (bodyParameters == null) return;
if (bodyParameters == null) return this;
foreach (var param in bodyParameters)
{
Request.AddParameter(param.Key, param.Value);
}
return this;
}
internal void ConfigureRequest(IUnAuthenticatedUntappdCredentials 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);
Request.AddParameter("client_id", credentials.ClientId);
Request.AddParameter("client_secret", credentials.ClientSecret);
}
internal void ConfigureRequest(IAuthenticatedUntappdCredentials credentials, string endPoint, IDictionary<string, object> bodyParameters = null, Method webMethod = Method.GET)
{
ConfigureRequest(endPoint, bodyParameters, webMethod);
Request.AddParameter("access_token", credentials.AccessToken);
foreach (var untappdCredential in credentials.AuthenticationData)
{
Request.AddParameter(untappdCredential.Key, untappdCredential.Value);
}
return this;
}
private TResult ExecuteRequest<TResult>()
where TResult : class
{
var response = Client.Execute(Request);
return JsonConvert.DeserializeObject<TResult>(response.Content);
return ProcessExecution<TResult>(Client.Execute(Request));
}
private async Task<TResult> ExecuteRequestAsync<TResult>()
where TResult : class
{
var response = await Client.ExecuteTaskAsync(Request);
return JsonConvert.DeserializeObject<TResult>(response.Content);
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);
if (OnExceptionThrown != null)
{
OnExceptionThrown(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)
{
if (OnExceptionThrown != null)
{
OnExceptionThrown(this, new UnhandledExceptionEventArgs(e, FailFast));
}
if (FailFast)
{
throw;
}
return null;
}
}
}
}

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Untappd.Net.Client;
using Untappd.Net.Exception;
namespace Untappd.Net.Request
{
@@ -13,13 +14,14 @@ namespace Untappd.Net.Request
/// <param name="credentials">Pass in a credentials object</param>
/// <param name="urlParameter">this is the main parameter for a request. ie v4/user/checkins/urlParameter. Consult the untappd docs, this can be null for a few requests</param>
/// <param name="bodyParameters">Any additional params you wish to add to the request</param>
/// <exception cref="HttpErrorException"></exception>
/// <returns></returns>
public TResult Get<TResult>(IUnAuthenticatedUntappdCredentials credentials, string urlParameter, IDictionary<string, object> bodyParameters = null)
where TResult : IUnAuthenticatedRequest, new()
where TResult : class, IUnAuthenticatedRequest, new()
{
var result = new TResult();
ConfigureRequest(credentials, result.EndPoint(urlParameter), bodyParameters);
return ExecuteRequest<TResult>();
return ConfigureRequest(credentials, result.EndPoint(urlParameter), bodyParameters)
.ExecuteRequest<TResult>();
}
/// <summary>
@@ -29,13 +31,14 @@ namespace Untappd.Net.Request
/// <param name="credentials"></param>
/// <param name="urlParameter"></param>
/// <param name="bodyParameters"></param>
/// <exception cref="HttpErrorException"></exception>
/// <returns></returns>
public Task<TResult> GetAsync<TResult>(IUnAuthenticatedUntappdCredentials credentials, string urlParameter, IDictionary<string, object> bodyParameters = null)
where TResult : IUnAuthenticatedRequest, new()
where TResult : class, IUnAuthenticatedRequest, new()
{
var result = new TResult();
ConfigureRequest(credentials, result.EndPoint(urlParameter), bodyParameters);
return ExecuteRequestAsync<TResult>();
return ConfigureRequest(credentials, result.EndPoint(urlParameter), bodyParameters)
.ExecuteRequestAsync<TResult>();
}
/// <summary>
@@ -45,13 +48,14 @@ namespace Untappd.Net.Request
/// <param name="credentials">Pass in a credentials object</param>
/// <param name="urlParameter">this is the main parameter for a request. ie v4/user/checkins/urlParameter. Consult the untappd docs, this can be null for a few requests</param>
/// <param name="bodyParameters">Any additional params you wish to add to the request</param>
/// <exception cref="HttpErrorException"></exception>
/// <returns></returns>
public TResult Get<TResult>(IAuthenticatedUntappdCredentials credentials, string urlParameter = "", IDictionary<string, object> bodyParameters = null)
where TResult : IAuthenticatedRequest, new()
where TResult : class,IAuthenticatedRequest, new()
{
var result = new TResult();
ConfigureRequest(credentials, result.EndPoint(urlParameter), bodyParameters);
return ExecuteRequest<TResult>();
return ConfigureRequest(credentials, result.EndPoint(urlParameter), bodyParameters)
.ExecuteRequest<TResult>();
}
/// <summary>
@@ -61,14 +65,14 @@ namespace Untappd.Net.Request
/// <param name="credentials"></param>
/// <param name="urlParameter"></param>
/// <param name="bodyParameters"></param>
/// <exception cref="HttpErrorException"></exception>
/// <returns></returns>
public Task<TResult> GetAsync<TResult>(IAuthenticatedUntappdCredentials credentials, string urlParameter = "", IDictionary<string, object> bodyParameters = null)
where TResult : IAuthenticatedRequest, new()
where TResult : class,IAuthenticatedRequest, new()
{
var result = new TResult();
ConfigureRequest(result.EndPoint(urlParameter), bodyParameters);
Request.AddParameter("access_token", credentials.AccessToken);
return ExecuteRequestAsync<TResult>();
return ConfigureRequest(credentials, result.EndPoint(urlParameter), bodyParameters)
.ExecuteRequestAsync<TResult>();
}
}
}

View File

@@ -13,8 +13,8 @@ namespace Untappd.Net.Request
/// <returns>returns dynamic since often the return doesn't matter</returns>
public dynamic Post(IAuthenticatedUntappdCredentials credentials, IAction action)
{
ConfigureRequest(credentials, action.EndPoint, action.BodyParameters, action.RequestMethod);
return ExecuteRequest<dynamic>();
return ConfigureRequest(credentials, action.EndPoint, action.BodyParameters, action.RequestMethod)
.ExecuteRequest<dynamic>();
}
/// <summary>
@@ -25,8 +25,8 @@ namespace Untappd.Net.Request
/// <returns>returns dynamic since often the return doesn't matter</returns>
public Task<dynamic> PostAsync(IAuthenticatedUntappdCredentials credentials, IAction action)
{
ConfigureRequest(credentials, action.EndPoint, action.BodyParameters, action.RequestMethod);
return ExecuteRequestAsync<dynamic>();
return ConfigureRequest(credentials, action.EndPoint, action.BodyParameters, action.RequestMethod)
.ExecuteRequestAsync<dynamic>();
}
}
}

View File

@@ -47,6 +47,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Authentication\AuthenticationHelper.cs" />
<Compile Include="Exception\HttpErrorException.cs" />
<Compile Include="Request\IAction.cs" />
<Compile Include="Request\RepositoryGet.cs" />
<Compile Include="Request\RepositoryPost.cs" />