diff --git a/src/Untappd.Net.UnitTests/Responses/Actions/TestCheckInAction.cs b/src/Untappd.Net.UnitTests/Responses/Actions/TestCheckInAction.cs new file mode 100644 index 0000000..09df9b1 --- /dev/null +++ b/src/Untappd.Net.UnitTests/Responses/Actions/TestCheckInAction.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NUnit.Framework; +using Untappd.Net.Responses.Actions; + +namespace Untappd.Net.UnitTests.Responses.Actions +{ + [TestFixture] + public class TestCheckInAction + { + [Test] + public void TestAccessors() + { + Assert.Throws(() => { new CheckIn(string.Empty, "timezone", 1); }); + Assert.Throws(() => { new CheckIn("1", string.Empty, 1); }); + var checkin = new CheckIn("offset", "timezone", 1); + Assert.IsNotNullOrEmpty(checkin.RequestMethod.ToString()); + Assert.Throws(() => { checkin.Rating = -1; }); + Assert.Throws(() => { checkin.Rating = 6; }); + Assert.Throws(() => { checkin.Shout = new String('d', 141); }); + checkin.Rating = 3; + Assert.AreEqual(3, checkin.Rating); + var t = "tst"; + checkin.Shout = t; + Assert.IsNotNullOrEmpty(checkin.EndPoint); + Assert.AreEqual(checkin.Shout, t); + } + + [Test] + public void TestDictionaryGeneration() + { + var checkin = new CheckIn("offset", "timezone", 1); + Assert.AreEqual(checkin.BodyParameters["gmt_offset"], "offset"); + Assert.AreEqual(checkin.BodyParameters["timezone"], "timezone"); + Assert.AreEqual(checkin.BodyParameters["bid"], 1); + + Assert.IsFalse(checkin.BodyParameters.ContainsKey("geolat")); + checkin.Geolat = 4; + Assert.IsTrue(checkin.BodyParameters.ContainsKey("geolat")); + Assert.AreEqual(checkin.BodyParameters["geolat"], 4); + + Assert.IsFalse(checkin.BodyParameters.ContainsKey("geolng")); + checkin.Geolng = 4; + Assert.IsTrue(checkin.BodyParameters.ContainsKey("geolng")); + Assert.AreEqual(checkin.BodyParameters["geolng"], 4); + + Assert.IsFalse(checkin.BodyParameters.ContainsKey("shout")); + checkin.Shout = "shout"; + Assert.IsTrue(checkin.BodyParameters.ContainsKey("shout")); + Assert.AreEqual(checkin.BodyParameters["shout"], "shout"); + + Assert.IsFalse(checkin.BodyParameters.ContainsKey("rating")); + checkin.Rating = 2; + Assert.IsTrue(checkin.BodyParameters.ContainsKey("rating")); + Assert.AreEqual(checkin.BodyParameters["rating"], 2); + + + + } + + } +} diff --git a/src/Untappd.Net.UnitTests/Responses/Actions/TestSimpleActions.cs b/src/Untappd.Net.UnitTests/Responses/Actions/TestSimpleActions.cs new file mode 100644 index 0000000..9aa1429 --- /dev/null +++ b/src/Untappd.Net.UnitTests/Responses/Actions/TestSimpleActions.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NUnit.Framework; +using Untappd.Net.Responses.Actions; + +namespace Untappd.Net.UnitTests.Responses.Actions +{ + [TestFixture] + public class TestSimpleActions + { + [Test] + public void TestFriends() + { + var accept = new AcceptFriend("targetid"); + Assert.IsNotNullOrEmpty(accept.RequestMethod.ToString()); + Assert.IsTrue(accept.EndPoint.Contains("targetid")); + Assert.IsNotNull(accept.BodyParameters); + + var add = new AddFriend("targetid"); + Assert.IsNotNullOrEmpty(add.RequestMethod.ToString()); + Assert.IsTrue(add.EndPoint.Contains("targetid")); + Assert.IsNotNull(add.BodyParameters); + + var toast = new ToastUntoast("targetid"); + Assert.IsNotNullOrEmpty(toast.RequestMethod.ToString()); + Assert.IsTrue(toast.EndPoint.Contains("targetid")); + Assert.IsNotNull(toast.BodyParameters); + + var remove = new RemoveFriend("targetid"); + Assert.IsNotNullOrEmpty(remove.RequestMethod.ToString()); + Assert.IsTrue(remove.EndPoint.Contains("targetid")); + Assert.IsNotNull(remove.BodyParameters); + + var removeWish = new RemoveFromWishList(1); + Assert.IsNotNullOrEmpty(removeWish.RequestMethod.ToString()); + Assert.IsNotNullOrEmpty(removeWish.EndPoint); + Assert.AreEqual(removeWish.BodyParameters["bid"], 1); + + + var addWish = new AddToWishList(1); + Assert.IsNotNullOrEmpty(addWish.RequestMethod.ToString()); + Assert.IsNotNullOrEmpty(addWish.EndPoint); + Assert.AreEqual(addWish.BodyParameters["bid"], 1); + + var comment = new AddComment("checkin", "shout"); + Assert.IsNotNullOrEmpty(comment.RequestMethod.ToString()); + Assert.IsTrue(comment.EndPoint.Contains("checkin")); + Assert.AreEqual(comment.BodyParameters["shout"], "shout"); + } + + [Test] + public void TestPendingFriends() + { + var pending = new PendingFriends(); + Assert.IsNotNullOrEmpty(pending.RequestMethod.ToString()); + Assert.IsNotNullOrEmpty(pending.EndPoint); + Assert.IsNotNull(pending.BodyParameters); + Assert.IsFalse(pending.BodyParameters.ContainsKey("limit")); + pending.Limit = 1; + Assert.IsTrue(pending.BodyParameters.ContainsKey("limit")); + Assert.IsFalse(pending.BodyParameters.ContainsKey("offset")); + pending.Offset = 1; + Assert.IsTrue(pending.BodyParameters.ContainsKey("offset")); + } + + [Test] + public void TestArgumentNull() + { + Assert.Throws(() => { new AcceptFriend(string.Empty); }); + Assert.Throws(() => { new AddFriend(string.Empty); }); + Assert.Throws(() => { new RemoveFriend(string.Empty); }); + Assert.Throws(() => { new ToastUntoast(string.Empty); }); + Assert.Throws(() => { new AddComment(string.Empty, "ds"); }); + Assert.Throws(() => { new AddComment("ds", string.Empty); }); + Assert.Throws(() => { new AddComment("ds", new String('d', 141)); }); + } + } +} diff --git a/src/Untappd.Net.UnitTests/Untappd.Net.UnitTests.csproj b/src/Untappd.Net.UnitTests/Untappd.Net.UnitTests.csproj index 9bb0afb..d5a937d 100644 --- a/src/Untappd.Net.UnitTests/Untappd.Net.UnitTests.csproj +++ b/src/Untappd.Net.UnitTests/Untappd.Net.UnitTests.csproj @@ -69,6 +69,8 @@ + + diff --git a/src/Untappd.Net/Request/RepositoryPost.cs b/src/Untappd.Net/Request/RepositoryPost.cs index 3e341c7..4359d79 100644 --- a/src/Untappd.Net/Request/RepositoryPost.cs +++ b/src/Untappd.Net/Request/RepositoryPost.cs @@ -4,6 +4,12 @@ namespace Untappd.Net.Request { public partial class Repository { + /// + /// do a post with actions + /// + /// + /// + /// returns dynamic since often the return doesn't matter public dynamic Post(IAuthenticatedUntappdCredentials credentials, IAction action) { ConfigureRequest(action.EndPoint, action.BodyParameters); diff --git a/src/Untappd.Net/Responses/Actions/AcceptFriend.cs b/src/Untappd.Net/Responses/Actions/AcceptFriend.cs new file mode 100644 index 0000000..7003de0 --- /dev/null +++ b/src/Untappd.Net/Responses/Actions/AcceptFriend.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using RestSharp; +using Untappd.Net.Request; + +namespace Untappd.Net.Responses.Actions +{ + public class AcceptFriend : IAction + { + public Method RequestMethod { get { return Method.GET; } } + public string EndPoint { get; private set; } + public IDictionary BodyParameters { get { return new Dictionary(); } } + public AcceptFriend(string target_id) + { + if (string.IsNullOrWhiteSpace(target_id)) + { + throw new ArgumentNullException("target_id"); + } + EndPoint = string.Format("v4/friend/accept/{0}", target_id); + } + } +} diff --git a/src/Untappd.Net/Responses/Actions/AddComment.cs b/src/Untappd.Net/Responses/Actions/AddComment.cs new file mode 100644 index 0000000..7c088f4 --- /dev/null +++ b/src/Untappd.Net/Responses/Actions/AddComment.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using RestSharp; +using Untappd.Net.Request; + +namespace Untappd.Net.Responses.Actions +{ + public class AddComment : IAction + { + public Method RequestMethod { get {return Method.POST;} } + public string EndPoint { get; private set; } + public IDictionary BodyParameters { get; private set; } + public AddComment(string checkinId, string shout) + { + if (string.IsNullOrWhiteSpace(checkinId)) + { + throw new ArgumentNullException("checkinId"); + } + if (string.IsNullOrWhiteSpace(shout)) + { + throw new ArgumentNullException("shout"); + } + if (shout.Length > 140) + { + throw new ArgumentOutOfRangeException("shout", shout, "Shout cannot be more than 140 characters"); + } + EndPoint = string.Format("v4/checkin/addcomment/{0}", checkinId); + BodyParameters = new Dictionary {{shout, shout}}; + + } + } +} diff --git a/src/Untappd.Net/Responses/Actions/AddFriend.cs b/src/Untappd.Net/Responses/Actions/AddFriend.cs new file mode 100644 index 0000000..c67fca6 --- /dev/null +++ b/src/Untappd.Net/Responses/Actions/AddFriend.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using RestSharp; +using Untappd.Net.Request; + +namespace Untappd.Net.Responses.Actions +{ + public class AddFriend : IAction + { + public Method RequestMethod { get{return Method.GET;} } + public string EndPoint { get; private set; } + public IDictionary BodyParameters { get{ return new Dictionary();} } + public AddFriend(string target_id) + { + if (string.IsNullOrWhiteSpace(target_id)) + { + throw new ArgumentNullException("target_id"); + } + EndPoint = string.Format("v4/friend/request/{0}", target_id); + } + } +} diff --git a/src/Untappd.Net/Responses/Actions/AddToWishList.cs b/src/Untappd.Net/Responses/Actions/AddToWishList.cs new file mode 100644 index 0000000..991fb3f --- /dev/null +++ b/src/Untappd.Net/Responses/Actions/AddToWishList.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; +using RestSharp; +using Untappd.Net.Request; + +namespace Untappd.Net.Responses.Actions +{ + public class AddToWishList : IAction + { + public Method RequestMethod { get {return Method.GET;} } + public string EndPoint { get { return "v4/user/wishlist/add"; } } + public IDictionary BodyParameters { get; private set; } + public AddToWishList(int beerId) + { + BodyParameters = new Dictionary() { { "bid", beerId } }; + + } + } +} diff --git a/src/Untappd.Net/Responses/Actions/CheckIn.cs b/src/Untappd.Net/Responses/Actions/CheckIn.cs index 4a25c65..9682366 100644 --- a/src/Untappd.Net/Responses/Actions/CheckIn.cs +++ b/src/Untappd.Net/Responses/Actions/CheckIn.cs @@ -18,37 +18,37 @@ namespace Untappd.Net.Responses.Actions { var dict = new Dictionary { - {"gmt_offset", gmt_offset}, - {"timezone", timezone}, - {"bid", bid} + {"gmt_offset", GmtOffset}, + {"timezone", Timezone}, + {"bid", Bid} }; - if (geolat.HasValue) + if (Geolat.HasValue) { - dict.Add("geolat", geolat.Value); + dict.Add("geolat", Geolat.Value); } - if (geolng.HasValue) + if (Geolng.HasValue) { - dict.Add("geolng", geolng.Value); + dict.Add("geolng", Geolng.Value); } - if (!string.IsNullOrWhiteSpace(shout) && shout.Length <= 140) + if (!string.IsNullOrWhiteSpace(Shout) && Shout.Length <= 140) { - dict.Add("shout", shout); + dict.Add("shout", Shout); } - if (rating > 0 && rating < 6) + if (Rating > 0 && Rating < 6) { - dict.Add("rating", rating); + 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 GmtOffset { 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 + public string Shout { get { return _shout; } set @@ -57,11 +57,11 @@ namespace Untappd.Net.Responses.Actions { throw new ArgumentOutOfRangeException("value", value,"Shout can be no more than 140 characters"); } - _shout = value; + _shout = string.Copy(value); } } - public short rating + public short Rating { get { return _rating; } set @@ -75,19 +75,19 @@ namespace Untappd.Net.Responses.Actions } - public CheckIn(string gmt_offset, string timezone, int bid) + public CheckIn(string gmtOffset, string timezone, int bid) { - if (string.IsNullOrWhiteSpace(gmt_offset)) + if (string.IsNullOrWhiteSpace(gmtOffset)) { - throw new ArgumentNullException("gmt_offset"); + throw new ArgumentNullException("gmtOffset"); } if (string.IsNullOrWhiteSpace(timezone)) { throw new ArgumentNullException("timezone"); } - this.gmt_offset = string.Copy(gmt_offset); - this.timezone = string.Copy(timezone); - this.bid = bid; + GmtOffset = string.Copy(gmtOffset); + Timezone = string.Copy(timezone); + Bid = bid; } } } diff --git a/src/Untappd.Net/Responses/Actions/PendingFriends.cs b/src/Untappd.Net/Responses/Actions/PendingFriends.cs index efcd8ba..aec06d3 100644 --- a/src/Untappd.Net/Responses/Actions/PendingFriends.cs +++ b/src/Untappd.Net/Responses/Actions/PendingFriends.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; using RestSharp; using Untappd.Net.Request; @@ -10,7 +6,7 @@ namespace Untappd.Net.Responses.Actions { public class PendingFriends : IAction { - public Method RequestMethod { get; private set; } + public Method RequestMethod { get {return Method.GET;} } public string EndPoint { get { return "v4/user/pending"; }} public IDictionary BodyParameters @@ -32,9 +28,5 @@ namespace Untappd.Net.Responses.Actions public int? Offset { get; set; } public int? Limit { get; set; } - public PendingFriends() - { - - } } } diff --git a/src/Untappd.Net/Responses/Actions/RemoveFriend.cs b/src/Untappd.Net/Responses/Actions/RemoveFriend.cs new file mode 100644 index 0000000..0463da6 --- /dev/null +++ b/src/Untappd.Net/Responses/Actions/RemoveFriend.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using RestSharp; +using Untappd.Net.Request; + +namespace Untappd.Net.Responses.Actions +{ + public class RemoveFriend : IAction + { + public Method RequestMethod { get { return Method.GET; } } + public string EndPoint { get; private set; } + public IDictionary BodyParameters { get { return new Dictionary(); } } + public RemoveFriend(string target_id) + { + if (string.IsNullOrWhiteSpace(target_id)) + { + throw new ArgumentNullException("target_id"); + } + EndPoint = string.Format("v4/friend/reject/{0}", target_id); + } + } +} diff --git a/src/Untappd.Net/Responses/Actions/RemoveFromWishList.cs b/src/Untappd.Net/Responses/Actions/RemoveFromWishList.cs new file mode 100644 index 0000000..9623913 --- /dev/null +++ b/src/Untappd.Net/Responses/Actions/RemoveFromWishList.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RestSharp; +using Untappd.Net.Request; + +namespace Untappd.Net.Responses.Actions +{ + public class RemoveFromWishList : IAction + { + public Method RequestMethod { get {return Method.GET;} } + public string EndPoint { get { return "v4/user/wishlist/delete"; } } + public IDictionary BodyParameters { get; private set; } + public RemoveFromWishList(int beerId) + { + BodyParameters = new Dictionary() { { "bid", beerId } }; + + } + } +} diff --git a/src/Untappd.Net/Responses/Actions/ToastUntoast.cs b/src/Untappd.Net/Responses/Actions/ToastUntoast.cs index 19d5640..3fee33c 100644 --- a/src/Untappd.Net/Responses/Actions/ToastUntoast.cs +++ b/src/Untappd.Net/Responses/Actions/ToastUntoast.cs @@ -7,10 +7,9 @@ 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 Method RequestMethod { get { return Method.POST; } } + public IDictionary BodyParameters { get { return new Dictionary(); } } + public string EndPoint { get; private set; } /// /// /// @@ -22,9 +21,7 @@ namespace Untappd.Net.Responses.Actions { throw new ArgumentNullException("checkinId"); } - CheckinId = string.Copy(checkinId); - BodyParameters = new Dictionary(); - RequestMethod = Method.POST; + EndPoint = string.Format("v4/checkin/toast/{0}", checkinId); } } } diff --git a/src/Untappd.Net/Untappd.Net.csproj b/src/Untappd.Net/Untappd.Net.csproj index f79e080..a57e78c 100644 --- a/src/Untappd.Net/Untappd.Net.csproj +++ b/src/Untappd.Net/Untappd.Net.csproj @@ -50,8 +50,14 @@ + + + + + +