3

Traits work well with Retrofit as long as there is no extra method implemented. Depending on return type RetrofitError: TwitchApi.someMethod: HTTP method annotation is required (e.g., @GET, @POST, etc.). or java.lang.IllegalArgumentException: TwitchApi.someMethod: Must have either a return type or Callback as last argument. is thrown.

Is there a way to make retrofit ignore a method that is not annotated with any of retrofit.http.GET / PUT / ... ?

public trait SomeApi {

    GET("/endpoint")
    public fun getSomething(Query("user") user: String): Observable<SomeResponse>

    class object {
        public fun create(): SomeApi {
            val restAdapter = RestAdapter.Builder().setEndpoint("http://localhost").build()
            return restAdapter.create<TwitchApi>(javaClass<SomeApi >())
        }
    }

    public fun someMethodThatBreaksRetrofit(user: String) : Int {
        return processResponse(getSomething(user))
    }
}
atok
  • 5,880
  • 3
  • 33
  • 62

1 Answers1

9

You shouldn't do like that at all. Fortunately in Kotlin you can use extension functions so

interface SomeApi {
    GET("/endpoint")
    fun getSomething(Query("user") user: String): Observable<SomeResponse>
}

fun SomeApi.someMethod(user : String) : Observable<Int>
    = processResponse(getSomething(user))
atok
  • 5,880
  • 3
  • 33
  • 62
Sergey Mashkov
  • 4,630
  • 1
  • 27
  • 24