2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

Well, you should refactor your code so that you don't repeat yourself:

private async Task<List<ItemRetrieved>> GetValuesAsync(object parametersAsAnonymousObject)
{
    var requestUrl = MakeRequestUri("v3/get");

    var request = new HttpRequest(HttpMethod.Post, requestUrl);
    request.AddBody(parametersAsAnonymousObject);

    var response = await SendAsync<ItemRetrievedWrapper>(request);
    return response.List.Values.ToList();
}

public Task<List<ItemRetrieved>> GetAllUnreadAsync(int sinceInUnixTime = 0)
{
    return GetValuesAsync(new
        {
            consumer_key = _consumerKey,
            access_token = _accessToken,
            state = "unread",
            sort = "newest",
            since = sinceInUnixTime
        });
}

public Task<List<ItemRetrieved>> GetFavoriteAsync(int sinceInUnixTime = 0)
{
    return GetValuesAsync(new
        {
            consumer_key = _consumerKey,
            access_token = _accessToken,
            favorite = 1,
            sort = "newest",
            since = sinceInUnixTime
        });
}

This sill repeats consumer_key and access_token, but I think getting rid of that repetition would be too complicated, so it's not worth it (unless you have big number of these methods).

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.