I am looking for ways to improve my API wrapper for Pocket. So far, my code is quite compact and nice (I think), but I feel like there must be a better way to do this.
This is the part where I think a lot of code redundancy due to different parameters that need to be serialized to Json and later send to server.
public async Task<List<ItemRetrieved>> GetAllUnreadAsync(int sinceInUnixTime = 0)
{
var requestUrl = MakeRequestUri("v3/get");
var request = new HttpRequest(HttpMethod.Post, requestUrl);
request.AddBody(new
{
consumer_key = _consumerKey,
access_token = _accessToken,
state = "unread",
sort = "newest",
since = sinceInUnixTime
});
var response = await SendAsync<ItemRetrievedWrapper>(request);
return response.List.Values.ToList();
}
public async Task<List<ItemRetrieved>> GetFavoriteAsync(int sinceInUnixTime = 0)
{
var requestUrl = MakeRequestUri("v3/get");
var request = new HttpRequest(HttpMethod.Post, requestUrl);
request.AddBody(new
{
consumer_key = _consumerKey,
access_token = _accessToken,
favorite = 1,
sort = "newest",
since = sinceInUnixTime
});
var response = await SendAsync<ItemRetrievedWrapper>(request);
return response.List.Values.ToList();
}
I have few other methods that look like this as well but the only different is the anonymous object passed inside the AddBody
. The anonymous object is the parameters that need to be set. AddBody
will serialize the object into Json.
My idea currently is to create a separate class/object for the parameters and use method chaining to build up the parameters before passing it into AddBody
.
Maybe looks something like this.
var requestParams = new RequestParams();
requestParams.SortBy("newest").Since(2312321312).IsFavorite(true);
request.AddBody(requestParams);
Are there any other good ways I can refactor my code?
If you need to see the big picture, you can take a look here at my Github https://github.com/shulhi/PocketNet
Thank you.