Skip to content

Commit

Permalink
GetUsers needs to support querystrings
Browse files Browse the repository at this point in the history
Mainly for pagination and filters
  • Loading branch information
jozsurf committed May 13, 2015
1 parent 2f1928c commit ebf3dd9
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions Freshdesk/FreshdeskService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Freshdesk
{
Expand Down Expand Up @@ -217,12 +216,16 @@ protected virtual string DoMultipartFormRequest(Uri uri, object body, IEnumerabl
return GetResponseAsString(response);
}

protected virtual Uri UriForPath(string path)
{
UriBuilder uriBuilder = new UriBuilder(this.ApiUri);
uriBuilder.Path = path;
return uriBuilder.Uri;
}
protected virtual Uri UriForPath(string path, string query = null)
{
UriBuilder uriBuilder = new UriBuilder(this.ApiUri);
uriBuilder.Path = path;
if (!string.IsNullOrEmpty(query))
{
uriBuilder.Query = query;
}
return uriBuilder.Uri;
}

private static Dictionary<string, string> GetStringsContent(object instance)
{
Expand Down Expand Up @@ -395,10 +398,25 @@ public void UpdateUser(UpdateUserRequest updateUserRequest, long id)
/// Get users
/// </summary>
/// <returns></returns>
public IEnumerable<GetUserRequest> GetUsers()
{
return DoRequest<IEnumerable<GetUserRequest>>(UriForPath("/contacts.json"));
}
public IEnumerable<GetUserRequest> GetUsers()
{
var users = new List<GetUserRequest>();
var page = 1;
while (true)
{
var paginatedUsers = DoRequest<IEnumerable<GetUserRequest>>(UriForPath("/contacts.json", string.Format("page={0}", page))).ToList();
if (paginatedUsers.Any())
{
users.AddRange(paginatedUsers);
page++;
}
else
{
break;
}
}
return users;
}


#endregion
Expand Down

0 comments on commit ebf3dd9

Please sign in to comment.