Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different types for posted and returned data #2

Open
muliyul opened this issue Mar 17, 2017 · 3 comments
Open

Different types for posted and returned data #2

muliyul opened this issue Mar 17, 2017 · 3 comments

Comments

@muliyul
Copy link

muliyul commented Mar 17, 2017

This is a suggestion mixed with a question.
I would like to POST and parse the returned request. Something along the lines:

Map<String, Object> body = new HashMap<>();
body.put("email", "[email protected]");
body.put("password", "s0m3P4s5w0rd");
Rest.one("auth")
       .put(body)
       .post(User.class)
       .request();

However this is currently impossible because the body and the parsed response are coupled. A decouple would be nice.

Is it possible to achieve what I'm seeking with volley-requests?

@jsotuyod
Copy link
Member

This is an interesting point. We discussed this for a very long time internally before deciding on the current approach.

We ended up deciding on keeping it unified because:

  1. it implies a smaller API (single arg / method call allows to setup everything)
  2. we couldn't think of any scenario where splitting this is needed and not down to an API design problem

I'm guessing here, but I assume that your User class has an email field, and maybe even a password field.

If that's the case, you could rewrite your code as:

User body = new User("[email protected]", "s0m3P4s5w0rd");
Rest.one("auth")
       .put(body)
       .request();

All null fields on User will be left out on the put request body, and the posted json would be the same as the one produced by your map.

Is there any reason why this wouldn't work on your scenario? Maybe we have a real issue here, and I'm not seeing it.

@muliyul
Copy link
Author

muliyul commented Mar 17, 2017

Say my authentication requires {email, password} fields to be present, however my user class contains them in another format like:

class User {
    private String password;
    private List<Identity> identities;
    class Identity {
        enum Type { Facebook, Google }
        Type type;
        String email;
    }
}

@jsotuyod
Copy link
Member

Interesting... I wonder how we could extend the DSL without overcomplicating this...

Overloading put / post / etc. would be an option, but would require to add too many methods...

Maybe doing something such as:

Rest.one("auth")
       .put(body)
       .as(User.class)
       .request();

The problem would be the order of the verb call (post / put / get) and the as would be meaningful, as one overrides the other.... Maybe just adding a requestAs(Class<?> clazz) method would be better to force order, but the type data would be lost from the builder....

On the other hand, got get requests, this unfolding makes no sense at all...

Maybe overloading the verb methods is the way forward... I certainly need to think this over. Your input is welcomed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants