0

I've declared an API call in an interface and was wondering if it is possible to put constraints on some of the parameters. The API I'm accessing has these constraints as well and would like to enforce them in my program.

@GET("/recipes/search")
Call<RecipeResponse> getRecipes(
        @Query("cuisine") String cuisine,
        @Query("diet") String diet,
        @Query("excludeIngredients") String excludeIngredients,
        @Query("intolerances") String intolerances,
        @Query("number") Integer number,
        @Query("offset") Integer offset,
        @Query("query") String query,
        @Query("type") String type
);

How can I do this?

I know that it is possible to do this with POST request, and passing along an object via the RequestBody through the @Body annotation. Can I do this with a GET request too, where information is passed via the query string?

Thanks!

Kevin
  • 1
  • 1

1 Answers1

0

I think I ended up finding a solution. I've made a class SearchRecipeRequest in which I declare all possible parameters as class variables. In the setters I do the data validation such as checking for null on parameters that are required, or min/max value constraints on integers as specified by the endpoint. I then made a SearchRecipeRequestBuilder class to build such an object like so to make it easier to deal with all those possible parameters:

public class SearchRecipeRequestBuilder {

    private String  _cuisine = null,
                    _diet = null,
                    _excludeIngredients = null,
                    _intolerances = null,
                    _query = null,
                    _type = null;

    private Integer _number = null,
                    _offset = null;

    public SearchRecipeRequestBuilder() {}

    public SearchRecipeRequest buildRequest() {
        return new SearchRecipeRequest(_cuisine, _diet, _excludeIngredients, _intolerances, _number, _offset, _query, _type);
    }

    public SearchRecipeRequestBuilder cuisine(String cuisine) {
        _cuisine = cuisine;
        return this;
    }

    public SearchRecipeRequestBuilder diet(String diet) {
        _diet = diet;
        return this;
    }

    public SearchRecipeRequestBuilder excludeIngredients(String excludeIngredients) {
        _excludeIngredients = excludeIngredients;
        return this;
    }

    public SearchRecipeRequestBuilder intolerances(String intolerances) {
        _intolerances = intolerances;
        return this;
    }

    public SearchRecipeRequestBuilder query(String query) {
        _query = query;
        return this;
    }

    public SearchRecipeRequestBuilder type(String type) {
        _type = type;
        return this;
    }

    public SearchRecipeRequestBuilder number(Integer number) {
        _number = number;
        return this;
    }

    public SearchRecipeRequestBuilder offset(Integer offset) {
        _offset = offset;
        return this;
    }
}

Which allows me to build the request like so:

    SearchRecipeRequest request = new SearchRecipeRequestBuilder()
            .query("burger")
            .buildRequest();

I then pass along that object to a different function that knows how to use the request object to pass it along to the API.

That's how I'm doing it right now, if someone has a better way I'd love to hear it. :)

I got the idea to use the Builder pattern from a different StackOverflow question: Managing constructors with many parameters in Java.

Kevin
  • 1
  • 1