Retrofit: Provide Response Code Enums

Created on 6 Feb 2018  路  6Comments  路  Source: square/retrofit

What kind of issue is this?

  • Feature Request. Start by telling us what problem you鈥檙e trying to solve. Often a solution
    already exists! Don鈥檛 send pull requests to implement new features without first getting our
    support. Sometimes we leave features out on purpose to keep the project small.

I believe this library will benefit from having the Response object return an Enum on its method code(), like many other HTTP libraries and Frameworks do., so instead of:

if (response.code() == 200) { 
    // print "OK".
} else if (response.code() == 403) {
    // print "Forbbiden"
}

In which we have to memorize all the HTTP codes, and maybe mix some, we can have:

if (response.code() == HttpStatus.OK_200) { 
    // print "OK".
} else if (response.code() == HttpStatus.Forbidden_403) {
    // print "Forbbiden"
}

I think this will help the users to code quicker with the library (autocomplete can help with Enums) and beginners on REST and HTTP codes will be less prone to errors and mixing up what code does what.

Most helpful comment

All 6 comments

The list of HTTP response codes are not finite and Java enums do not provide a mechanism for representing values that are not known at library compile time. If you are not comfortable with the raw code values, the isSuccessful() method will handle performing the appropriate range check.

This is similar to #2627 which suggested constants.

javax.ws.rs uses enums:

https://docs.oracle.com/javaee/7/api/javax/ws/rs/core/Response.Status.html

So I thought Retrofit could follow the same idea.

Doing so can't be done until Retrofit 3 which we currently have no plans for any time soon. I still don't see a lot of provided value in an enum here.

I think the code becomes more readable, maybe you guys deal with HTTP codes for a long time and don't see the difference, but for beginners all those numbers on the middle of the logic can get a bit hard to read smoothly.

Just my $0.2

@swankjesse this looks interesting, thanks!

Was this page helpful?
0 / 5 - 0 ratings