|
@@ -28,18 +28,20 @@ import java.io.File
|
|
|
* @param path String 网络路径, 非绝对路径会加上HOST为前缀
|
|
|
* @see NetConfig.host
|
|
|
* @param isAbsolutePath Boolean Path是否是绝对路径
|
|
|
+ * @param tag 可以传递对象给Request请求
|
|
|
* @param block SimpleUrlRequest.Api.() -> UnitUtils
|
|
|
* @return Observable<M> 结果会在主线程
|
|
|
*/
|
|
|
inline fun <reified M> get(
|
|
|
path: String,
|
|
|
+ tag: Any? = null,
|
|
|
isAbsolutePath: Boolean = false,
|
|
|
noinline block: (SimpleUrlRequest.Api.() -> Unit)? = null
|
|
|
): Observable<M> {
|
|
|
|
|
|
return Observable.create<M> {
|
|
|
try {
|
|
|
- val get = Kalle.get(if (isAbsolutePath) path else (NetConfig.host + path))
|
|
|
+ val get = Kalle.get(if (isAbsolutePath) path else (NetConfig.host + path)).tag(tag)
|
|
|
val response = if (block == null) {
|
|
|
get.perform(M::class.java, String::class.java)
|
|
|
} else {
|
|
@@ -69,19 +71,21 @@ inline fun <reified M> get(
|
|
|
* Post提交
|
|
|
* @param path String 网络路径, 非绝对路径会加上HOST为前缀
|
|
|
* @see NetConfig.host
|
|
|
+ * @param tag 可以传递对象给Request请求
|
|
|
* @param isAbsolutePath Boolean 是否是绝对路径
|
|
|
* @param block SimpleBodyRequest.Api.() -> UnitUtils
|
|
|
* @return Observable<M> 结果会在主线程
|
|
|
*/
|
|
|
inline fun <reified M> post(
|
|
|
path: String,
|
|
|
+ tag: Any? = null,
|
|
|
isAbsolutePath: Boolean = false,
|
|
|
noinline block: (SimpleBodyRequest.Api.() -> Unit)? = null
|
|
|
): Observable<M> {
|
|
|
|
|
|
return Observable.create<M> {
|
|
|
try {
|
|
|
- val post = Kalle.post(if (isAbsolutePath) path else (NetConfig.host + path))
|
|
|
+ val post = Kalle.post(if (isAbsolutePath) path else (NetConfig.host + path)).tag(tag)
|
|
|
val response = if (block == null) {
|
|
|
post.perform<M, String>(M::class.java, String::class.java)
|
|
|
} else {
|
|
@@ -114,6 +118,7 @@ inline fun <reified M> post(
|
|
|
* @param path String 网络路径, 非绝对路径会加上HOST为前缀
|
|
|
* @see NetConfig.host
|
|
|
* @param directory String 下载文件存放目录 {默认存在android/data/packageName/cache目录}
|
|
|
+ * @param tag 可以传递对象给Request请求
|
|
|
* @param isAbsolutePath Boolean 下载链接是否是绝对路径
|
|
|
* @param block 请求参数
|
|
|
* @return Observable<String> 结果会在主线程
|
|
@@ -121,6 +126,7 @@ inline fun <reified M> post(
|
|
|
fun download(
|
|
|
path: String,
|
|
|
directory: String = NetConfig.app.externalCacheDir!!.absolutePath,
|
|
|
+ tag: Any? = null,
|
|
|
isAbsolutePath: Boolean = false,
|
|
|
block: (UrlDownload.Api.() -> Unit)? = null
|
|
|
): Observable<String> {
|
|
@@ -129,7 +135,7 @@ fun download(
|
|
|
try {
|
|
|
val realPath = if (isAbsolutePath) path else (NetConfig.host + path)
|
|
|
|
|
|
- val download = Kalle.Download.get(realPath).directory(directory)
|
|
|
+ val download = Kalle.Download.get(realPath).directory(directory).tag(tag)
|
|
|
|
|
|
val filePath = if (block == null) {
|
|
|
download.perform()
|
|
@@ -180,9 +186,7 @@ fun Context.downloadImg(url: String, with: Int = -1, height: Int = -1): Observab
|
|
|
it.onComplete()
|
|
|
}
|
|
|
} catch (e: Exception) {
|
|
|
- if (!it.isDisposed) {
|
|
|
- it.onError(e)
|
|
|
- }
|
|
|
+ if (!it.isDisposed) it.onError(e)
|
|
|
}
|
|
|
|
|
|
}.subscribeOn(Schedulers.computation()).observeOn(AndroidSchedulers.mainThread())
|
|
@@ -194,13 +198,14 @@ fun Context.downloadImg(url: String, with: Int = -1, height: Int = -1): Observab
|
|
|
|
|
|
inline fun <reified M> syncGet(
|
|
|
path: String,
|
|
|
+ tag: Any? = null,
|
|
|
isAbsolutePath: Boolean = false,
|
|
|
noinline block: (SimpleUrlRequest.Api.() -> Unit)? = null
|
|
|
): Observable<M> {
|
|
|
|
|
|
return Observable.create<M> {
|
|
|
try {
|
|
|
- val get = Kalle.get(if (isAbsolutePath) path else (NetConfig.host + path))
|
|
|
+ val get = Kalle.get(if (isAbsolutePath) path else (NetConfig.host + path)).tag(tag)
|
|
|
val response = if (block == null) {
|
|
|
get.perform(M::class.java, String::class.java)
|
|
|
} else {
|
|
@@ -212,9 +217,7 @@ inline fun <reified M> syncGet(
|
|
|
it.onNext(response.succeed())
|
|
|
it.onComplete()
|
|
|
} else {
|
|
|
- it.onError(
|
|
|
- ResponseException(response.failed(), response.code())
|
|
|
- )
|
|
|
+ it.onError(ResponseException(response.failed(), response.code()))
|
|
|
}
|
|
|
}
|
|
|
} catch (e: java.lang.Exception) {
|
|
@@ -227,13 +230,14 @@ inline fun <reified M> syncGet(
|
|
|
|
|
|
inline fun <reified M> syncPost(
|
|
|
path: String,
|
|
|
+ tag: Any? = null,
|
|
|
isAbsolutePath: Boolean = false,
|
|
|
noinline block: (SimpleBodyRequest.Api.() -> Unit)? = null
|
|
|
): Observable<M> {
|
|
|
|
|
|
return Observable.create<M> {
|
|
|
try {
|
|
|
- val post = Kalle.post(if (isAbsolutePath) path else (NetConfig.host + path))
|
|
|
+ val post = Kalle.post(if (isAbsolutePath) path else (NetConfig.host + path)).tag(tag)
|
|
|
val response = if (block == null) {
|
|
|
post.perform<M, String>(M::class.java, String::class.java)
|
|
|
} else {
|
|
@@ -261,6 +265,7 @@ inline fun <reified M> syncPost(
|
|
|
fun syncDownload(
|
|
|
path: String,
|
|
|
directory: String = NetConfig.app.externalCacheDir!!.absolutePath,
|
|
|
+ tag: Any? = null,
|
|
|
isAbsolutePath: Boolean = false,
|
|
|
block: (UrlDownload.Api.() -> Unit)? = null
|
|
|
): Observable<String> {
|
|
@@ -269,7 +274,7 @@ fun syncDownload(
|
|
|
try {
|
|
|
val realPath = if (isAbsolutePath) path else (NetConfig.host + path)
|
|
|
|
|
|
- val download = Kalle.Download.get(realPath).directory(directory)
|
|
|
+ val download = Kalle.Download.get(realPath).directory(directory).tag(tag)
|
|
|
|
|
|
val filePath = if (block == null) {
|
|
|
download.perform()
|