Skip to content

Commit

Permalink
Merge pull request #4 from ltttttttttttt/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ltttttttttttt authored Jun 1, 2023
2 parents 351cc29 + 3711a36 commit 9ef11a9
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions LazyPeopleHttp-lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ kotlin {
api("io.ktor:ktor-client-core:$ktorVersion")
//kt的跨平台json解析
api("org.jetbrains.kotlinx:kotlinx-serialization-json:$serializationJsonVersion")
//compose runtime
compileOnly("org.jetbrains.compose.runtime:runtime:1.4.0")
}
}
val commonTest by getting {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.lt.lazy_people_http.call

import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import io.ktor.client.request.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
Expand All @@ -26,4 +28,21 @@ interface Call<T> {
* 自定义一些配置
*/
fun config(block: HttpRequestBuilder.() -> Unit): Call<T>

/**
* 将[Call]转化为[State]
*/
fun toState(onFailure: ((call: Call<T>, t: Throwable?) -> Unit)? = null): State<T?> {
val state = mutableStateOf<T?>(null)
enqueue(object : Callback<T> {
override fun onResponse(call: Call<T>, response: T) {
state.value = response
}

override fun onFailure(call: Call<T>, t: Throwable?) {
onFailure?.invoke(call, t)
}
})
return state
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlinx.coroutines.withContext

/**
* creator: lt 2023/3/10 lt.dygzs@qq.com
* effect : 使用GlobalScope作用域的协程进行http请求
* effect : 进行http请求
* warning:
*/
class RealCall<T>(
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
suspend fun getUser(userId: Int): User
or
fun getUser(userId: Int): Call<User>

//In Compose used:
val user by remember { hf.getUser(0).toState() }
Text("UserName=${user?.name}")
```

Lazy people http, A type-safe HTTP client for JVM(Android, Desktop), iOS, js web.
Expand Down Expand Up @@ -124,6 +128,7 @@ private val hf = HttpFunctions::class.createService(config)
//Implementation class using the interface
hf.postB("123").enqueue()//callback asynchronous request
hf.suspendGetB("111")//Coroutine asynchronous request
val data by remember { hf.get().toState() }//Return responsive State, suitable for Compose
```

Step 4.Custom configuration:
Expand Down
5 changes: 5 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
suspend fun getUser(userId: Int): User
fun getUser(userId: Int): Call<User>

//如果使用Compose可以这样用:
val user by remember { hf.getUser(0).toState() }
Text("UserName=${user?.name}")
```

懒人http客户端, 类型安全的HTTP客户端, 适用于: JVM(Android, Desktop), iOS, js web.
Expand Down Expand Up @@ -124,6 +128,7 @@ private val hf = HttpFunctions::class.createService(config)
//使用接口的实现类
hf.postB("123").enqueue()//回调异步请求
hf.suspendGetB("111")//协程异步请求
val data by remember { hf.get().toState() }//返回响应式的State,适用于Compose
```

Step 4.自定义配置:
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ const val ktorVersion = "2.2.4"//ktor版本
const val serializationJsonVersion = "1.5.0"//json序列号版本
const val kspVersion = "$kotlinVersion-1.0.10"//ksp版本

const val mVersion = "1.0.8"//此库的版本
const val mVersion = "1.0.9"//此库的版本
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ interface HttpFunctions {
fun getD(@Url("type") url: String): Call<NetBean<String?>>

@GET("{url}")
fun get(@Url("url") url: String): Call<MData>
fun get(@Url("url") url: String="http://t.weather.sojson.com/api/weather/city/101030100"): Call<MData>

@GET("get/getB")
suspend fun suspendGetB(name: String): NetBean<UserBean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import com.lt.lazy_people_http.call.Call
import com.lt.lazy_people_http.call.Callback
Expand Down Expand Up @@ -69,6 +70,9 @@ fun App() {
}) {
Text(text4)
}

val data by remember { hf.get().toState() }
Text("Call#toState()=${data?.cityInfo?.city}")
}
}

Expand All @@ -95,8 +99,7 @@ fun testAll() {
assert(hf.getD("success").await().code == 200)
assert(hf.getD("fail").await().code == 400)
assert(
hf.get("http://t.weather.sojson.com/api/weather/city/101030100")
.await().cityInfo?.city == "天津市"
hf.get().await().cityInfo?.city == "天津市"
)
assert(hf.suspendGetB("2").data.name == "2")
assert(hf.post_postA("123").data == "123")
Expand Down

0 comments on commit 9ef11a9

Please sign in to comment.