gcoleman0828 gcoleman0828 - 3 months ago 19
reST (reStructuredText) Question

.Net WebAPI URI convention for advanced searching /filtering

I am relatively new to REST and WebAPI by Microsoft. We are implementing a hub REST service that will house several types of object gets and sets. Being the lead on the project, I am being tasked with coming up with the proper Uri design we are going with. I was wondering what thoughts were on war is better. Yes I specifically phased that without using the word "standard".

Here are the options my team and I are currently entertaining:

Http://servername/API/REST/Ldap/AD/employees?username=jsmith
Http://servername/API/REST/Ldap/AD/employee/UserName?searchTerm=jsmith (this seems RPC to me)
Http://servername/API/REST/Ldap/AD/employees/getusername?searchterm?jsmith


We are also creating a Soap version hence the rest in the Uri.

Thanks for the input

Answer

To Oppositional's comment, this is what I put together some time ago.

https://groups.google.com/d/msg/servicestack/uoMzASmvxho/CtqpZdju7NcJ

public class QueryBase
{
    public string Query { get; set; }
    public int Limit { get; set; }
    public int Offset { get; set; }
}

[Route("/v1/users")]
public class User : IReturn<List<User>>
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
}

public class RequestFilterAttribute : Attribute, IHasRequestFilter
{
    #region IHasRequestFilter Members

    public IHasRequestFilter Copy()
    {
        return this;
    }

    public int Priority
    {
        get { return -100; }
    }

public void RequestFilter(IHttpRequest req, IHttpResponse res, object requestDto)
{
    var query = req.QueryString["q"] ?? req.QueryString["query"];
    var limit = req.QueryString["limit"];
    var offset = req.QueryString["offset"];

    var user = requestDto as QueryBase;
    if (user == null) { return; }
    user.Query = query;
    user.Limit = limit.IsEmpty() ? int.MaxValue : int.Parse(limit);
    user.Offset = offset.IsEmpty() ? 0 : int.Parse(offset);
    }

#endregion
}

[Route("/v1/users/search", "GET")]
[RequestFilter]
public class SearchUser : QueryBase, IReturn<PagedResult<User>> { }

public class UsersService : Service
{
    public static List<User> UserRepository = new List<User>
    {
        new User{ Id="1", FirstName = "Michael", LastName = "A", Email = "michaelEmail" },
        new User{ Id="2", FirstName = "Robert", LastName = "B", Email = "RobertEmail" },
        new User{ Id="3", FirstName = "Khris", LastName = "C", Email = "KhrisEmail" },
        new User{ Id="4", FirstName = "John", LastName = "D", Email = "JohnEmail" },
        new User{ Id="4", FirstName = "Lisa", LastName = "E", Email = "LisaEmail" }
    };

    public PagedResult<User> Get(SearchUser request)
{
    var query = request.Query;
    var users = request.Query.IsNullOrEmpty()
                    ? UserRepository.ToList()
                    : UserRepository.Where(x => x.FirstName.Contains(query) || x.LastName.Contains(query) || x.Email.Contains(query)).ToList();

    var totalItems = users.Count;
    var totalPages = (int)Math.Ceiling((decimal)totalItems / (decimal)request.Limit);
    var currentPage = request.Offset;
    users = users.Skip(request.Offset * request.Limit).Take(request.Limit).ToList();
    var itemCount = users.Count;

    return new PagedResult<User>
        {
            TotalItems = totalItems,
            TotalPages = totalPages,
            ItemCount = itemCount,
            Items = users,
            CurrentPage = currentPage
        };
}
}
Comments