需要HTTP方法注释Retrofit2

时间:2019-10-19 11:00:04

标签: android kotlin retrofit2 kodein

我对存储库模式的用法有一个抽象,我无法进行改造。

我将从翻新服务转到用例。

我有AuthenticationRetrofitService

interface AuthenticationRetrofitService {

    @GET(LOGIN_PATH)
    suspend fun doLogin(@Header("Authorization") basicAuth: String) : Either<Throwable, Monitor>

    @GET(LOGOUT_PATH)
    suspend fun doLogout() : Either<Throwable,Unit>

    companion object {
        private const val LOGIN_PATH = "login/"
        private const val LOGOUT_PATH = "logout/"
    }
}

然后我有AuthenticationRetrofitApi,它实现了AuthenticationApi

class AuthenticationRetrofitApi(private val service: AuthenticationRetrofitService) : AuthenticationApi {

    override suspend fun doLogin(basicAuth: String) = service.doLogin(basicAuth)

    override suspend fun doLogout() = service.doLogout()

}

那么这就是AuthenticationApi

interface AuthenticationApi {

    suspend fun doLogin(basicAuth: String) : Either<Throwable, Monitor>
    suspend fun doLogout() : Either<Throwable, Unit>
}

然后我有AuthenticationRepository

interface AuthenticationRepository {

    suspend fun doLogin(basicAuth: String): Either<Throwable, Monitor>
    suspend fun doLogout(): Either<Throwable, Unit>
}

还有AuthenticationRepositoryImpl

class AuthenticationRepositoryImpl (private val api: AuthenticationApi) : AuthenticationRepository {
    override suspend fun doLogin(basicAuth: String) =
        api.doLogin(basicAuth = basicAuth)
            .fold({
                Either.Left(Throwable())
            },
                {
                    Either.Right(it)
                }
            )

    override suspend fun doLogout() = api.doLogout().fold({Either.Left(Throwable())},{Either.Right(Unit)})

}

在用例中,我称AuthenticationRepository是我的问题,我不知道如何链接它们,因为如果我运行该应用程序,则会出现此错误。

  

java.lang.IllegalArgumentException:需要HTTP方法注释(例如@ GET,@ POST等)。

然后我正在使用Kodein进行依赖项注入,但是我不知道bindprovide会是什么错误? 因为:

AuthenticationRetrofitApi未使用 AuthenticationRepositoryImpl未使用

我缺少什么?

编辑

这就是我将Kodein用于改造模块的方式

val retrofitModule = Kodein.Module("retrofitModule") {

    bind<OkHttpClient>() with singleton {
        OkHttpClient().newBuilder().build()
    }
    bind<Retrofit>() with singleton {
        Retrofit.Builder()
            .baseUrl("https://127.0.0.1/api/")
            .client(instance())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

//I'm not sure if I have to bind this
   bind<AuthenticationRepository>() with singleton {
        instance<Retrofit>().create(AuthenticationRepository::class.java)
    }
}

0 个答案:

没有答案