/http-url-extractor

当你使用Retrofit,但是你不想通过拼接字符串来创建HttpUrl的时候,请看这里。

Primary LanguageKotlin

http-url-extractor

当你们使用Retrofit,并且你们的后台服务提供了多个端口的服务,在某些时候,你可能会需要拿到一个Retrofit Api接口具体的HttpUrl地址;面对着接口上的注解们,你陷入了深思:我TM是真的不想拼接字符串。这时,这个项目的价值就体现出来了!

原理

通过动态代理,模拟Retrofit创建API实例,解析接口、方法以及参数相关的注解,由用户自行决定调用的方法,并提供对应的参数,最终处理成正确的HttpUrl

安装

当前最新版本:

  1. 在根模块的build.gradle的适当位置添加以下代码:

    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
  2. 在业务模块的build.gradle文件中添加以下代码:

    dependencies {
        implementation 'cn.numeron:http-url-extractor:latest_version'
    }

使用

//Retrofit Api接口
interface TestApi {

    @GET("/get")
    suspend fun get(@Query("aaa") value: String): Response<Unit?>

    @POST("post/{position}/{archive}")
    suspend fun post(
        @Path("position") position: Int,
        @Path("archive") archive: String?
    ): Response<Unit?>

}
//使用`baseUrl`或`Retrofit`实例即可初始化,调用`extract`方法即可获取到对应方法的`url`
val httpUrlExtractor = HttpUrlExtractor("http://192.168.3.213:6680/")
    /* 可通过此方法支持自定义注解的解析
    .addAnnotationParser<YourAnnotation> { builder, annotation, parameter ->
        //此Lambda内有ExtractorScope的作用域,可以访问或修改builder的url和path等属性。
        TODO("执行你的解析操作,并设置为builder设置解析结果")
    }
    */

val getUrl = httpUrlExtractor.extract<TestApi> {
    //执行想要获取到HttpUrl的方法
    get("bbb")
}
//getUrl = "http://192.168.3.213:6680/get?aaa=bbb"

val postUrl = httpUrlExtractor.extract<TestApi> {
    post(123, "money")
}
//postUrl = "http://192.168.3.213:6680/post/123/money"