-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18711b9
commit 48648e0
Showing
6 changed files
with
265 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
~ Copyright lt 2023 | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
~ you may not use this file except in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
--> | ||
|
||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.lt.lazy_people_http"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET"/> | ||
|
||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,112 @@ | ||
# LazyPeopleHttp | ||
|
||
Lazy people http, Retrofit of kmp all targets | ||
|
||
Inspired by: https://github.com/ltttttttttttt/retrofit | ||
If you want to use a more functional library, you can use: https://github.com/Foso/Ktorfit | ||
|
||
## How to use | ||
|
||
Step 1.add dependencies: | ||
|
||
version | ||
= [](https://repo1.maven.org/maven2/io/github/ltttttttttttt/LazyPeopleHttp/) | ||
|
||
* If it is a single platform, add it to build.gradle.kts in the app module directory | ||
|
||
```kotlin | ||
plugins { | ||
... | ||
id("com.google.devtools.ksp") version "1.7.10-1.0.6"//this,The left 1.7.10 corresponds to your the Kotlin version,more version: https://github.com/google/ksp/releases | ||
} | ||
|
||
//The fourth step of configuring ksp to generate directory reference links: https://github.com/ltttttttttttt/Buff/blob/main/README.md | ||
|
||
dependencies { | ||
... | ||
implementation("io.github.ltttttttttttt:LazyPeopleHttp-lib:$version")//this,such as 1.0.0 | ||
ksp("io.github.ltttttttttttt:LazyPeopleHttp:$version")//this,such as 1.0.0 | ||
} | ||
``` | ||
|
||
* If it is multi-platform, add it to build.gradle.kts in the common module directory | ||
|
||
```kotlin | ||
plugins { | ||
... | ||
id("com.google.devtools.ksp") version "1.7.10-1.0.6" | ||
} | ||
|
||
... | ||
val commonMain by getting { | ||
//Configure the ksp generation directory | ||
kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin") | ||
dependencies { | ||
... | ||
api("io.github.ltttttttttttt:LazyPeopleHttp-lib:$version")//this,such as 1.0.0 | ||
} | ||
} | ||
|
||
... | ||
dependencies { | ||
add("kspCommonMainMetadata", "io.github.ltttttttttttt:LazyPeopleHttp:$version") | ||
} | ||
``` | ||
|
||
Step 2.interface declaration: | ||
|
||
```kotlin | ||
@LazyPeopleHttpService | ||
interface HttpFunctions { | ||
//Standard post request statement | ||
@POST("post/postB") | ||
fun postB(@Field("name") t: String): Call<NetBean<UserBean>> | ||
|
||
//The lazy people post request statement will treat the method name as a url, and its _ will be converted to / | ||
fun post_postC(name: String): Call<NetBean<String>> | ||
|
||
//suspend post request statement | ||
suspend fun post_postA(t: String): NetBean<String> | ||
|
||
//Standard get request statement | ||
@GET("get/getA") | ||
fun getA(@Query("t") t2: String): Call<NetBean<String>> | ||
|
||
//lazy people get request statement | ||
fun get_getB(name: String): Call<NetBean<UserBean>> | ||
|
||
//suspend get request statement | ||
suspend fun suspendGetB(name: String): NetBean<UserBean> | ||
|
||
//Add static request headers | ||
@Header("aaa", "bbb") | ||
fun post_checkHeader(): Call<NetBean<String?>> | ||
|
||
//Configure dynamic urls | ||
@GET("get/getD/{type}") | ||
fun getD(@Url("type") url: String): Call<NetBean<String?>> | ||
|
||
//Specific functions can be declared, and no additional methods will be generated at this time | ||
fun ccc(): Int = 0 | ||
} | ||
``` | ||
|
||
Step 3.interface use: | ||
|
||
```kotlin | ||
//Configure the client of ktor, the json of serialization | ||
private val client = HttpClient { | ||
defaultRequest { | ||
//Configure baseUrl | ||
url("http://127.0.0.1:666/") | ||
} | ||
} | ||
private val json = Json { ignoreUnknownKeys = true } | ||
private val config = LazyPeopleHttpConfig(client, json) | ||
//Create an implementation class for the request interface | ||
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,112 @@ | ||
# LazyPeopleHttp | ||
|
||
懒人http客户端,相当于kotlin全平台的Retrofit | ||
相对于Retrofit,有更高的运行时性能,且支持多平台 | ||
|
||
灵感来源于: https://github.com/ltttttttttttt/retrofit | ||
如果你想使用更多功能的库,可以使用: https://github.com/Foso/Ktorfit | ||
|
||
## 使用方式 | ||
|
||
Step 1.添加依赖: | ||
|
||
version | ||
= [](https://repo1.maven.org/maven2/io/github/ltttttttttttt/LazyPeopleHttp/) | ||
|
||
* 如果是单平台,在app模块目录内的build.gradle.kts内添加 | ||
|
||
```kotlin | ||
plugins { | ||
... | ||
id("com.google.devtools.ksp") version "1.7.10-1.0.6"//this,前面的1.7.10对应你的kotlin版本,更多版本参考: https://github.com/google/ksp/releases | ||
} | ||
|
||
//配置ksp生成目录参考链接的第四步: https://github.com/ltttttttttttt/Buff/blob/main/README_CN.md?plain=1 | ||
|
||
dependencies { | ||
... | ||
implementation("io.github.ltttttttttttt:LazyPeopleHttp-lib:$version")//this,比如1.0.0 | ||
ksp("io.github.ltttttttttttt:LazyPeopleHttp:$version")//this,比如1.0.0 | ||
} | ||
``` | ||
|
||
* 如果是多平台,在common模块目录内的build.gradle.kts内添加 | ||
|
||
```kotlin | ||
plugins { | ||
... | ||
id("com.google.devtools.ksp") version "1.7.10-1.0.6"//this,前面的1.7.10对应你的kotlin版本,更多版本参考: https://github.com/google/ksp/releases | ||
} | ||
|
||
... | ||
val commonMain by getting { | ||
//配置ksp生成目录 | ||
kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin") | ||
dependencies { | ||
... | ||
api("io.github.ltttttttttttt:LazyPeopleHttp-lib:$version")//this,比如1.0.0 | ||
} | ||
} | ||
|
||
... | ||
dependencies { | ||
add("kspCommonMainMetadata", "io.github.ltttttttttttt:LazyPeopleHttp:$version") | ||
} | ||
``` | ||
|
||
Step 2.接口声明: | ||
|
||
```kotlin | ||
@LazyPeopleHttpService | ||
interface HttpFunctions { | ||
//标准post请求声明 | ||
@POST("post/postB") | ||
fun postB(@Field("name") t: String): Call<NetBean<UserBean>> | ||
|
||
//懒人post请求声明,会把方法名当做url,其下划线会转换为斜杠 | ||
fun post_postC(name: String): Call<NetBean<String>> | ||
|
||
//suspend post请求声明 | ||
suspend fun post_postA(t: String): NetBean<String> | ||
|
||
//标准get请求声明 | ||
@GET("get/getA") | ||
fun getA(@Query("t") t2: String): Call<NetBean<String>> | ||
|
||
//懒人get请求声明 | ||
fun get_getB(name: String): Call<NetBean<UserBean>> | ||
|
||
//suspend get请求声明 | ||
suspend fun suspendGetB(name: String): NetBean<UserBean> | ||
|
||
//添加静态的请求头 | ||
@Header("aaa", "bbb") | ||
fun post_checkHeader(): Call<NetBean<String?>> | ||
|
||
//配置动态的url | ||
@GET("get/getD/{type}") | ||
fun getD(@Url("type") url: String): Call<NetBean<String?>> | ||
|
||
//可以声明具体函数,此时不会生成额外的方法 | ||
fun ccc(): Int = 0 | ||
} | ||
``` | ||
|
||
Step 3.接口使用: | ||
|
||
```kotlin | ||
//配置ktor的client,serialization的json | ||
private val client = HttpClient { | ||
defaultRequest { | ||
//配置baseUrl | ||
url("http://127.0.0.1:666/") | ||
} | ||
} | ||
private val json = Json { ignoreUnknownKeys = true } | ||
private val config = LazyPeopleHttpConfig(client, json) | ||
//创建请求接口的实现类 | ||
private val hf = HttpFunctions::class.createService(config) | ||
|
||
//使用接口的实现类 | ||
hf.postB("123").enqueue()//回调异步请求 | ||
hf.suspendGetB("111")//协程异步请求 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters