Giter VIP home page Giter VIP logo

http-url-extractor's Introduction

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"

http-url-extractor's People

Contributors

xiazunyang avatar

Watchers

 avatar

Forkers

vovien

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.