lfoust avatar lfoust committed a93d34d

Started implementing 3.5 client methods

Comments (0)

Files changed (7)

trunk/source/Stacky.Net35/Options/AnswerOptions.cs

-using System;
-
-namespace Stacky
-{
-    public class OptionsBase
-    {
-        public int? Page = null;
-        public int? PageSize = null;
-        public string Filter = null;
-    }
-
-    public class Options<TSort, TMaxMin>
-        where TSort : struct
-        where TMaxMin : struct
-    {
-        public TSort? SortBy = null;
-        public SortDirection? SortDirection = null;
-        public int? Page = null;
-        public int? PageSize = null;
-        public UnixDateTime? FromDate = null;
-        public UnixDateTime? ToDate = null;
-        public TMaxMin? Min = null;
-        public TMaxMin? Max = null;
-        public string Filter = null;
-    }
-
-    public class Options<TSort> : Options<TSort, UnixDateTime>
-        where TSort : struct
-    {
-    }
-
-    public class AnswerOptions
-    {
-        public AnswerSort SortBy = AnswerSort.Creation;
-        public SortDirection SortDirection = SortDirection.Descending;
-        public int? Page = null;
-        public int? PageSize = null;
-        public bool IncludeBody = false;
-        public bool IncludeComments = false;
-        public DateTime? FromDate = null;
-        public DateTime? ToDate = null;
-        public int? Min = null;
-        public int? Max = null;
-    }
-}

trunk/source/Stacky.Net35/Options/Options.cs

+namespace Stacky
+{
+    using System;
+
+    //int? page = null, int? pageSize = null, DateTime? fromDate = null, DateTime? toDate = null, string filter = null
+    public class Options
+    {
+        public int? Page = null;
+        public int? PageSize = null;
+        public DateTime? FromDate = null;
+        public DateTime? ToDate = null;
+        public string Filter = null;
+    }
+
+    public class Options<TSort, TMaxMin> : Options
+        where TSort : struct
+        where TMaxMin : struct
+    {
+        public TSort? SortBy = null;
+        public SortDirection? SortDirection = null;
+        public TMaxMin? Min = null;
+        public TMaxMin? Max = null;
+    }
+
+    public class Options<TSort> : Options<TSort, DateTime>
+        where TSort : struct
+    {
+    }
+}

trunk/source/Stacky.Net35/Stacky.Net35.csproj

     <Compile Include="..\Stacky\UrlClientAsync.cs">
       <Link>UrlClientAsync.cs</Link>
     </Compile>
-    <Compile Include="Options\AnswerOptions.cs" />
+    <Compile Include="Options\Options.cs" />
     <Compile Include="Options\BadgeByUserOptions.cs" />
     <Compile Include="Options\CommentOptions.cs" />
     <Compile Include="Options\CommentsByPostOptions.cs" />
     <Compile Include="StackyClient\CommentMethods.cs" />
     <Compile Include="StackyClient\QuestionMethods.cs" />
     <Compile Include="StackyClient\RevisionMethods.cs" />
+    <Compile Include="StackyClient\StackyClientWithOptions.cs" />
     <Compile Include="StackyClient\StatsMethods.cs" />
     <Compile Include="StackyClient\TagMethods.cs" />
     <Compile Include="StackyClient\UserMethods.cs" />

trunk/source/Stacky.Net35/StackyClient/BadgeMethods.cs

-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
+using System.Linq;
 
 namespace Stacky
 {
     public partial class StackyClient
     {
-        public virtual IEnumerable<Badge> GetBadges()
+        public virtual IPagedList<Badge> GetBadges()
         {
-            return GetBadges("badges", null);
+            return GetBadges(null);
         }
 
-        private IEnumerable<Badge> GetBadges(string method, string[] urlArguments)
+        public virtual IPagedList<Badge> GetBadges(Options<BadgeSort, BadgeMinMax> options)
         {
-            return MakeRequest<Badge>(method, urlArguments, new
-            {
-                site = this.SiteUrlName
-            }).Items;
+            return Execute<Badge, BadgeMinMax>("badges", null, options);
         }
 
-        public virtual IEnumerable<User> GetUsersByBadge(int badgeId)
+        public virtual Badge GetBadge(int id)
         {
-            return GetUsersByBadge(badgeId, new BadgeByUserOptions());
+            return GetBadge(id, null);
         }
 
-        public virtual IEnumerable<User> GetUsersByBadge(int badgeId, BadgeByUserOptions options)
+        public virtual Badge GetBadge(int id, string filter)
         {
-            return GetUsersByBadge(badgeId.ToArray(), options);
+            var options = new Options<BadgeSort, BadgeMinMax>
+            {
+                Filter = filter
+            };
+            return GetBadges(id.ToArray(), options).FirstOrDefault();
         }
 
-        public virtual IPagedList<User> GetUsersByBadge(IEnumerable<int> badgeIds)
+        public virtual IPagedList<Badge> GetBadges(IEnumerable<int> ids, Options<BadgeSort, BadgeMinMax> options)
         {
-            return GetUsersByBadge(badgeIds, new BadgeByUserOptions());
+            return Execute<Badge, BadgeSort, BadgeMinMax>("badges", new string[] { ids.Vectorize() }, options);
         }
 
-        public virtual IPagedList<User> GetUsersByBadge(IEnumerable<int> badgeIds, BadgeByUserOptions options)
+        public virtual IPagedList<Badge> GetNamedBadges()
         {
-            var response = MakeRequest<User>("badges", new string[] { badgeIds.Vectorize() }, new
-            {
-                site = this.SiteUrlName,
-                page = options.Page ?? null,
-                pagesize = options.PageSize ?? null,
-                fromdate = GetDateValue(options.FromDate),
-                todate = GetDateValue(options.ToDate),
-            });
-            return new PagedList<User>(response);
+            return GetNamedBadges(null);
         }
 
-        public virtual IEnumerable<Badge> GetTagBasedBadges()
+        public virtual IPagedList<Badge> GetNamedBadges(Options<BadgeSort, BadgeMinMax> options)
         {
-            return GetBadges("badges", new string[] { "tags" });
+            return Execute<Badge, BadgeMinMax>("badges", new string[] { "name" }, options);
+        }
+
+        public virtual IPagedList<Badge> GetRecentlyAwardedBadges(Options options)
+        {
+            return Execute<Badge, BadgeMinMax>("badges", new string[] { "recipients" }, options);
+        }
+
+        public virtual IPagedList<Badge> GetRecentlyAwardedBadges(int id, Options options)
+        {
+            return GetRecentlyAwardedBadges(id.ToArray(), options);
+        }
+
+        public virtual IPagedList<Badge> GetRecentlyAwardedBadges(IEnumerable<int> ids, Options options)
+        {
+            return Execute<Badge, BadgeMinMax>("badges", new string[] { ids.Vectorize(), "recipients" }, options);
+        }
+
+        public virtual IPagedList<Badge> GetTagBasedBadges(Options<BadgeSort, BadgeMinMax> options)
+        {
+            return Execute<Badge, BadgeMinMax>("badges", new string[] { "tags" }, options);
         }
     }
 }

trunk/source/Stacky.Net35/StackyClient/StackyClientWithOptions.cs

+#if !SILVERLIGHT
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Stacky
+{
+    public partial class StackyClient : StackyClientBase
+    {
+        protected IPagedList<TEntity> Execute<TEntity>(string methodName, string[] urlArguments, Options options)
+            where TEntity : Entity, new()
+        {
+            var response = MakeRequest<TEntity>(methodName, urlArguments, new
+            {
+                site = this.SiteUrlName,
+                page = options.Page ?? null,
+                pagesize = options.PageSize ?? null,
+                fromdate = GetDateValue(options.FromDate),
+                todate = GetDateValue(options.ToDate),
+                filter = options.Filter
+            });
+            return new PagedList<TEntity>(response);
+        }
+
+        protected IPagedList<TEntity> Execute<TEntity, TSort, TMinMax>(string methodName, string[] urlArguments, Options<TSort, TMinMax> options)
+            where TEntity : Entity, new()
+            where TSort : struct
+            where TMinMax : struct
+        {
+            var response = MakeRequest<TEntity>(methodName, urlArguments, new
+            {
+                site = this.SiteUrlName,
+                page = options.Page ?? null,
+                pagesize = options.PageSize ?? null,
+                fromdate = GetDateValue(options.FromDate),
+                todate = GetDateValue(options.ToDate),
+                sort = GetEnumValue(options.SortBy),
+                order = GetSortDirection(options.SortDirection),
+                min = options.Min,
+                max = options.Max,
+                filter = options.Filter
+            });
+            return new PagedList<TEntity>(response);
+        }
+
+        protected IPagedList<TEntity> Execute<TEntity, TSort>(string methodName, string[] urlArguments, Options<TSort> options)
+            where TEntity : Entity, new()
+            where TSort : struct
+        {
+            var response = MakeRequest<TEntity>(methodName, urlArguments, new
+            {
+                site = this.SiteUrlName,
+                page = options.Page ?? null,
+                pagesize = options.PageSize ?? null,
+                fromdate = GetDateValue(options.FromDate),
+                todate = GetDateValue(options.ToDate),
+                sort = GetEnumValue(options.SortBy),
+                order = GetSortDirection(options.SortDirection),
+                min = GetDateValue(options.Min),
+                max = GetDateValue(options.Max),
+                filter = options.Filter
+            });
+            return new PagedList<TEntity>(response);
+        }
+    }
+}
+#endif

trunk/source/Stacky.Net35/StackyClientAsync/AnswerMethods.cs

     public partial class StackyClientAsync
 #endif
     {
-        public virtual void GetUsersAnswers(int userId, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError)
-        {
-            GetUsersAnswers(userId, onSuccess, onError, new AnswerOptions());
-        }
-
-        public virtual void GetUsersAnswers(int userId, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError, AnswerOptions options)
-        {
-            GetUsersAnswers(userId.ToArray(), onSuccess, onError, options);
-        }
-
-        public virtual void GetUsersAnswers(IEnumerable<int> userIds, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError)
-        {
-            GetUsersAnswers(userIds, onSuccess, onError, new AnswerOptions());
-        }
-
-        public virtual void GetUsersAnswers(IEnumerable<int> userIds, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError, AnswerOptions options)
-        {
-            GetAnswers("users", new string[] { userIds.Vectorize(), "answers" }, onSuccess, onError, options);
-        }
-
-        public virtual void GetQuestionAnswers(int questionId, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError)
-        {
-            GetQuestionAnswers(questionId, onSuccess, onError, new AnswerOptions());
-        }
-
-        public virtual void GetQuestionAnswers(int questionId, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError, AnswerOptions options)
-        {
-            GetUsersAnswers(questionId.ToArray(), onSuccess, onError, options);
-        }
-
-        public virtual void GetQuestionAnswers(IEnumerable<int> questionIds, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError)
-        {
-            GetQuestionAnswers(questionIds, onSuccess, onError, new AnswerOptions());
-        }
-
-        public virtual void GetQuestionAnswers(IEnumerable<int> questionIds, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError, AnswerOptions options)
-        {
-            GetAnswers("questions", new string[] { questionIds.Vectorize(), "answers" }, onSuccess, onError, options);
-        }
-
-        private void GetAnswers(string method, string[] urlParameters, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError, AnswerOptions options)
-        {
-            MakeRequest<Answer>(method, urlParameters, new
-            {
-                site = this.SiteUrlName,
-                page = options.Page ?? null,
-                pagesize = options.PageSize ?? null,
-                body = options.IncludeBody ? (bool?)true : null,
-                comments = options.IncludeComments ? (bool?)true : null,
-                sort = options.SortBy.ToString().ToLower(),
-                order = GetSortDirection(options.SortDirection),
-                min = options.Min ?? null,
-                max = options.Max ?? null,
-                fromdate = GetDateValue(options.FromDate),
-                todate = GetDateValue(options.ToDate),
-            }, (items) => onSuccess(new PagedList<Answer>(items)), onError);
-        }
     }
 }

trunk/source/Tests/Stacky.Net35.IntegrationTests/BadgeTests.cs

             Assert.IsNotNull(badges);
         }
 
-        [TestMethod]
-        public void Badge_GetUsersByBadge()
-        {
-            var users = Client.GetUsersByBadge(1464);
-            Assert.IsNotNull(users);
-        }
+        //[TestMethod]
+        //public void Badge_GetUsersByBadge()
+        //{
+        //    var users = Client.GetUsersByBadge(1464);
+        //    Assert.IsNotNull(users);
+        //}
 
-        [TestMethod]
-        public void Badge_GetTagBasedBadges()
-        {
-            var badges = Client.GetTagBasedBadges();
-            Assert.IsNotNull(badges);
-        }
+        //[TestMethod]
+        //public void Badge_GetTagBasedBadges()
+        //{
+        //    var badges = Client.GetTagBasedBadges();
+        //    Assert.IsNotNull(badges);
+        //}
     }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.