GithubHelp home page GithubHelp logo

czy1121 / httpapi Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 2.0 117 KB

使用 Coroutine + Retrofit 打造的最简单HTTP请求库

License: Apache License 2.0

Kotlin 100.00%
kotlin jetpack retrofit2 http coroutine

httpapi's Introduction

httpapi

使用 Coroutine + Retrofit 打造的最简单HTTP请求库,支持 kotlin.Result

Gradle

repositories {
    maven { url "https://gitee.com/ezy/repo/raw/cosmo/"}
}
dependencies {
    implementation "me.reezy.cosmo:httpapi:0.8.0"
}

使用

data class HttpBin(
    val origin: String,
    val url: String,
)

interface TestService {
    @GET("https://httpbin.org/get")
    suspend fun suspendHttpResult(): Result<HttpBin>

    @GET("https://httpbin.org/get")
    fun call(): Call<HttpBin>
}

初始化

private val okhttp by lazy {
    OkHttpClient.Builder().cache(Cache(File(cacheDir, "okhttp"), 100 * 1024 * 1024)).build()
}

private val moshi by lazy {
    Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
}

private val retrofit by lazy { 
    Retrofit.Builder().client(okhttp)
        .baseUrl("http://httpbin.org")
        .addConverterFactory(MoshiConverterFactory.create(moshi).asLenient())
        .addCallAdapterFactory(ResultCallAdapterFactory {
            it.printStackTrace()
        })
        .build() 
}


// 提供 Retrofit 实例
Api.setRetrofitProvider {
    retrofit
}

// 设置全局错误处理
Api.setErrorHandler {
    it.printStackTrace()
}

普通请求

// 普通请求
http<TestService>().call().onSuccess(this) {
    Log.e("OoO", "call => $it")
}.onFailure {
    Log.e("OoO", "call onFailure => $it")
}.onFinally {
    Log.e("OoO", "call onFinally")
}

在协程上下文中发起请求

// 在协程上下文中发起请求
lifecycleScope.launch {

    // 通过回调处理结果
    http<TestService>().suspendKotlinResult().onSuccess {
        Log.e("OoO", "suspendKotlinResult => $it")
    }.onFailure {
        Log.e("OoO", "suspendKotlinResult onFailure => $it")
    }

    // 直接返回结果
    val result = http<TestService>().suspendKotlinResult().getOrNull() ?: return@launch
}

LICENSE

The Component is open-sourced software licensed under the Apache license.

httpapi's People

Contributors

czy1121 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

caostgrace cybkw

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.