From d4ce7ccded9135978292938c56ccb315555df98c Mon Sep 17 00:00:00 2001 From: Tommy Parnell Date: Fri, 1 May 2015 05:14:23 -0400 Subject: [PATCH] started on actions --- .../Request/TestRepository.cs | 2 +- src/Untappd.Net/Request/IAction.cs | 16 ++++ src/Untappd.Net/Request/Repository.cs | 80 +--------------- src/Untappd.Net/Request/RepositoryGet.cs | 79 ++++++++++++++++ src/Untappd.Net/Request/RepositoryPost.cs | 19 ++++ src/Untappd.Net/Responses/Actions/CheckIn.cs | 91 +++++++++++++++++++ .../Responses/Actions/ToastUntoast.cs | 30 ++++++ src/Untappd.Net/Untappd.Net.csproj | 3 + 8 files changed, 244 insertions(+), 76 deletions(-) create mode 100644 src/Untappd.Net/Request/IAction.cs create mode 100644 src/Untappd.Net/Request/RepositoryGet.cs create mode 100644 src/Untappd.Net/Request/RepositoryPost.cs create mode 100644 src/Untappd.Net/Responses/Actions/CheckIn.cs create mode 100644 src/Untappd.Net/Responses/Actions/ToastUntoast.cs diff --git a/src/Untappd.Net.UnitTests/Request/TestRepository.cs b/src/Untappd.Net.UnitTests/Request/TestRepository.cs index 9a5b815..9b0a87b 100644 --- a/src/Untappd.Net.UnitTests/Request/TestRepository.cs +++ b/src/Untappd.Net.UnitTests/Request/TestRepository.cs @@ -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); } } diff --git a/src/Untappd.Net/Request/IAction.cs b/src/Untappd.Net/Request/IAction.cs new file mode 100644 index 0000000..e353c26 --- /dev/null +++ b/src/Untappd.Net/Request/IAction.cs @@ -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 BodyParameters { get; } + } +} diff --git a/src/Untappd.Net/Request/Repository.cs b/src/Untappd.Net/Request/Repository.cs index d398ff8..961bef1 100644 --- a/src/Untappd.Net/Request/Repository.cs +++ b/src/Untappd.Net/Request/Repository.cs @@ -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 bodyParameters = null) + internal void ConfigureRequest(string endPoint, IDictionary bodyParameters = null , Method webMethod = Method.GET) { Request.Resource = endPoint; Request.Method = webMethod; @@ -38,83 +38,13 @@ namespace Untappd.Net.Request } - /// - /// Get the things! - /// - /// What you want to request - /// Pass in a credentials object - /// 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 - /// Any additional params you wish to add to the request - /// - public TResult Get (IUnAuthenticatedUntappdCredentials credentials, string urlParameter, IDictionary bodyParameters = null) - where TResult : IUnAuthenticatedRequest,new() + private TResult ExecuteRequest() { - 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(); - } - - /// - /// Get the things! Async! - /// - /// - /// - /// - /// - /// - public Task GetAsync(IUnAuthenticatedUntappdCredentials credentials, string urlParameter, IDictionary 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(); - } - - /// - /// Get the things! authenticated! - /// - /// - /// Pass in a credentials object - /// 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 - /// Any additional params you wish to add to the request - /// - public TResult Get(IAuthenticatedUntappdCredentials credentials, string urlParameter = "", IDictionary 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(); - } - - /// - /// Get the things Authenticated! Async!! - /// - /// - /// - /// - /// - /// - public Task GetAsync(IAuthenticatedUntappdCredentials credentials, string urlParameter = "", IDictionary 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(); - } - - private TResult DoRestRequest() - { - var response = Client.Execute(this.Request); + var response = Client.Execute(Request); return JsonConvert.DeserializeObject(response.Content); } - private async Task DoRestRequestAsync() + private async Task ExecuteRequestAsync() { var response = await Client.ExecuteTaskAsync(Request); return JsonConvert.DeserializeObject(response.Content); diff --git a/src/Untappd.Net/Request/RepositoryGet.cs b/src/Untappd.Net/Request/RepositoryGet.cs new file mode 100644 index 0000000..e945116 --- /dev/null +++ b/src/Untappd.Net/Request/RepositoryGet.cs @@ -0,0 +1,79 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Untappd.Net.Client; + +namespace Untappd.Net.Request +{ + public partial class Repository + { + /// + /// Get the things! + /// + /// What you want to request + /// Pass in a credentials object + /// 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 + /// Any additional params you wish to add to the request + /// + public TResult Get(IUnAuthenticatedUntappdCredentials credentials, string urlParameter, IDictionary 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(); + } + + /// + /// Get the things! Async! + /// + /// + /// + /// + /// + /// + public Task GetAsync(IUnAuthenticatedUntappdCredentials credentials, string urlParameter, IDictionary 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(); + } + + /// + /// Get the things! authenticated! + /// + /// + /// Pass in a credentials object + /// 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 + /// Any additional params you wish to add to the request + /// + public TResult Get(IAuthenticatedUntappdCredentials credentials, string urlParameter = "", IDictionary bodyParameters = null) + where TResult : IAuthenticatedRequest, new() + { + var result = new TResult(); + ConfigureRequest(result.EndPoint(urlParameter), bodyParameters); + Request.AddParameter("access_token", credentials.AccessToken); + return ExecuteRequest(); + } + + /// + /// Get the things Authenticated! Async!! + /// + /// + /// + /// + /// + /// + public Task GetAsync(IAuthenticatedUntappdCredentials credentials, string urlParameter = "", IDictionary bodyParameters = null) + where TResult : IAuthenticatedRequest, new() + { + var result = new TResult(); + ConfigureRequest(result.EndPoint(urlParameter), bodyParameters); + Request.AddParameter("access_token", credentials.AccessToken); + return ExecuteRequestAsync(); + } + } +} diff --git a/src/Untappd.Net/Request/RepositoryPost.cs b/src/Untappd.Net/Request/RepositoryPost.cs new file mode 100644 index 0000000..a23a5d3 --- /dev/null +++ b/src/Untappd.Net/Request/RepositoryPost.cs @@ -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(); + } + } +} diff --git a/src/Untappd.Net/Responses/Actions/CheckIn.cs b/src/Untappd.Net/Responses/Actions/CheckIn.cs new file mode 100644 index 0000000..c648956 --- /dev/null +++ b/src/Untappd.Net/Responses/Actions/CheckIn.cs @@ -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 BodyParameters + { + get + { + var dict = new Dictionary(); + 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; + } + } +} diff --git a/src/Untappd.Net/Responses/Actions/ToastUntoast.cs b/src/Untappd.Net/Responses/Actions/ToastUntoast.cs new file mode 100644 index 0000000..39d43d5 --- /dev/null +++ b/src/Untappd.Net/Responses/Actions/ToastUntoast.cs @@ -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 BodyParameters { get; private set; } + public string CheckinId { get; private set; } + public string EndPoint { get { return string.Format(" /v4/checkin/toast/{0}", CheckinId); } } + /// + /// + /// + /// + /// + public ToastUntoast(string checkinId) + { + if (string.IsNullOrWhiteSpace(checkinId)) + { + throw new ArgumentNullException("checkinId"); + } + CheckinId = string.Copy(checkinId); + BodyParameters = new Dictionary(); + RequestMethod = Method.POST; + } + } +} diff --git a/src/Untappd.Net/Untappd.Net.csproj b/src/Untappd.Net/Untappd.Net.csproj index f25d01e..27f251b 100644 --- a/src/Untappd.Net/Untappd.Net.csproj +++ b/src/Untappd.Net/Untappd.Net.csproj @@ -47,6 +47,9 @@ + + +