started on actions

This commit is contained in:
Tommy Parnell
2015-05-01 05:14:23 -04:00
parent 409ace7da4
commit d4ce7ccded
8 changed files with 244 additions and 76 deletions

View File

@@ -69,7 +69,7 @@ namespace Untappd.Net.UnitTests.Request
var constructorTest = new Repository();
constructorTest.Request.Parameters.Add(new Parameter(){Name = "param"});
Assert.IsTrue(constructorTest.Request.Parameters.Count > 0);
constructorTest.ConfigureGetRequest("endpoint");
constructorTest.ConfigureRequest("endpoint");
Assert.IsTrue(constructorTest.Request.Parameters.Count == 0);
}
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RestSharp;
namespace Untappd.Net.Request
{
public interface IAction
{
Method RequestMethod { get; }
string EndPoint { get; }
IDictionary<string, object> BodyParameters { get; }
}
}

View File

@@ -7,7 +7,7 @@ using System.Threading;
namespace Untappd.Net.Request
{
public class Repository
public partial class Repository
{
internal IRestClient Client;
internal IRestRequest Request;
@@ -24,7 +24,7 @@ namespace Untappd.Net.Request
Request = request;
}
internal void ConfigureGetRequest(string endPoint, Method webMethod = Method.GET, IDictionary<string, string> bodyParameters = null)
internal void ConfigureRequest(string endPoint, IDictionary<string, object> bodyParameters = null , Method webMethod = Method.GET)
{
Request.Resource = endPoint;
Request.Method = webMethod;
@@ -38,83 +38,13 @@ namespace Untappd.Net.Request
}
/// <summary>
/// Get the things!
/// </summary>
/// <typeparam name="TResult">What you want to request</typeparam>
/// <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>
/// <returns></returns>
public TResult Get<TResult> (IUnAuthenticatedUntappdCredentials credentials, string urlParameter, IDictionary<string, string> bodyParameters = null)
where TResult : IUnAuthenticatedRequest,new()
private TResult ExecuteRequest<TResult>()
{
var result = new TResult();
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>
/// <typeparam name="TResult"></typeparam>
/// <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>
/// <returns></returns>
public TResult Get<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 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 response = Client.Execute(this.Request);
var response = Client.Execute(Request);
return JsonConvert.DeserializeObject<TResult>(response.Content);
}
private async Task<TResult> DoRestRequestAsync<TResult>()
private async Task<TResult> ExecuteRequestAsync<TResult>()
{
var response = await Client.ExecuteTaskAsync(Request);
return JsonConvert.DeserializeObject<TResult>(response.Content);

View File

@@ -0,0 +1,79 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Untappd.Net.Client;
namespace Untappd.Net.Request
{
public partial class Repository
{
/// <summary>
/// Get the things!
/// </summary>
/// <typeparam name="TResult">What you want to request</typeparam>
/// <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>
/// <returns></returns>
public TResult Get<TResult>(IUnAuthenticatedUntappdCredentials credentials, string urlParameter, IDictionary<string, string> bodyParameters = null)
where TResult : IUnAuthenticatedRequest, new()
{
var result = new TResult();
ConfigureRequest(result.EndPoint(urlParameter), bodyParameters);
Request.AddParameter("client_id", credentials.ClientId);
Request.AddParameter("client_secret", credentials.ClientSecret);
return ExecuteRequest<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();
ConfigureRequest(result.EndPoint(urlParameter), bodyParameters);
Request.AddParameter("client_id", credentials.ClientId);
Request.AddParameter("client_secret", credentials.ClientSecret);
return ExecuteRequestAsync<TResult>();
}
/// <summary>
/// Get the things! authenticated!
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <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>
/// <returns></returns>
public TResult Get<TResult>(IAuthenticatedUntappdCredentials credentials, string urlParameter = "", IDictionary<string, string> bodyParameters = null)
where TResult : IAuthenticatedRequest, new()
{
var result = new TResult();
ConfigureRequest(result.EndPoint(urlParameter), bodyParameters);
Request.AddParameter("access_token", credentials.AccessToken);
return ExecuteRequest<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();
ConfigureRequest(result.EndPoint(urlParameter), bodyParameters);
Request.AddParameter("access_token", credentials.AccessToken);
return ExecuteRequestAsync<TResult>();
}
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Untappd.Net.Client;
namespace Untappd.Net.Request
{
public partial class Repository
{
public dynamic Post(IAuthenticatedUntappdCredentials credentials, IAction action)
{
ConfigureRequest(action.EndPoint, action.BodyParameters);
Request.AddParameter("access_token", credentials.AccessToken);
return ExecuteRequest<dynamic>();
}
}
}

View File

@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using RestSharp;
using Untappd.Net.Request;
namespace Untappd.Net.Responses.Actions
{
public class CheckIn : IAction
{
private short _rating;
private string _shout;
public Method RequestMethod { get{ return Method.POST;} }
public string EndPoint { get { return "/v4/checkin/add"; } }
public IDictionary<string, object> BodyParameters
{
get
{
var dict = new Dictionary<string, object>();
dict.Add("gmt_offset", gmt_offset);
dict.Add("timezone", timezone);
dict.Add("bid", bid);
if (geolat.HasValue)
{
dict.Add("geolat", geolat);
}
if (geolng.HasValue)
{
dict.Add("geolng", geolat);
}
if (!string.IsNullOrWhiteSpace(shout) && shout.Length <= 140)
{
dict.Add("shout", shout);
}
if (rating > 0 && rating < 6)
{
dict.Add("rating", rating);
}
return dict;
}
}
public string gmt_offset { get; private set; }
public string timezone { get; private set; }
public int bid { get; private set; }
public int? geolat { get; set; }
public int? geolng { get; set; }
public string shout
{
get { return _shout; }
set
{
if (value.Length > 140)
{
throw new ArgumentOutOfRangeException("value", value,"Shout can be no more than 140 characters");
}
_shout = value;
}
}
public short rating
{
get { return _rating; }
set
{
if (value < 1 || value > 5)
{
throw new ArgumentOutOfRangeException("value", value, "Ratings should be between 1 and 5");
}
_rating = value;
}
}
public CheckIn(string gmt_offset, string timezone, int bid)
{
if (string.IsNullOrWhiteSpace(gmt_offset))
{
throw new ArgumentNullException("gmt_offset");
}
if (string.IsNullOrWhiteSpace(timezone))
{
throw new ArgumentNullException("timezone");
}
this.gmt_offset = string.Copy(gmt_offset);
this.timezone = string.Copy(timezone);
this.bid = bid;
}
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using RestSharp;
using Untappd.Net.Request;
namespace Untappd.Net.Responses.Actions
{
public class ToastUntoast : IAction
{
public Method RequestMethod { get; private set; }
public IDictionary<string, object> BodyParameters { get; private set; }
public string CheckinId { get; private set; }
public string EndPoint { get { return string.Format(" /v4/checkin/toast/{0}", CheckinId); } }
/// <summary>
///
/// </summary>
/// <param name="checkinId"></param>
/// <exception cref="ArgumentNullException"></exception>
public ToastUntoast(string checkinId)
{
if (string.IsNullOrWhiteSpace(checkinId))
{
throw new ArgumentNullException("checkinId");
}
CheckinId = string.Copy(checkinId);
BodyParameters = new Dictionary<string, object>();
RequestMethod = Method.POST;
}
}
}

View File

@@ -47,6 +47,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Authentication\AuthenticationHelper.cs" />
<Compile Include="Request\IAction.cs" />
<Compile Include="Request\RepositoryGet.cs" />
<Compile Include="Request\RepositoryPost.cs" />
<Compile Include="SingleObjectArrayConverter.cs" />
<Compile Include="Client\AuthenticatedUntappdCredentials.cs" />
<Compile Include="Client\IAuthenticatedUntappdCredentials.cs" />