|
@@ -12,6 +12,7 @@ package com.drake.net
|
|
|
import android.content.Context
|
|
|
import com.bumptech.glide.Glide
|
|
|
import com.drake.net.error.ResponseException
|
|
|
+import com.yanzhenjie.kalle.Canceler
|
|
|
import com.yanzhenjie.kalle.Kalle
|
|
|
import com.yanzhenjie.kalle.RequestMethod
|
|
|
import com.yanzhenjie.kalle.Url
|
|
@@ -21,6 +22,7 @@ import com.yanzhenjie.kalle.simple.SimpleUrlRequest
|
|
|
import com.yanzhenjie.kalle.simple.cache.CacheMode
|
|
|
import kotlinx.coroutines.*
|
|
|
import java.io.File
|
|
|
+import java.net.SocketException
|
|
|
|
|
|
// <editor-fold desc="异步请求">
|
|
|
|
|
@@ -35,86 +37,260 @@ import java.io.File
|
|
|
*/
|
|
|
|
|
|
inline fun <reified M> CoroutineScope.Get(
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleUrlRequest.Api.() -> Unit)? = null
|
|
|
-): Deferred<M> = async(Dispatchers.IO) {
|
|
|
- _submitUrl<M>(RequestMethod.GET, path, tag, cache, absolutePath, block)
|
|
|
+ path: String,
|
|
|
+ tag: Any? = null,
|
|
|
+ cache: CacheMode = CacheMode.HTTP,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ noinline block: SimpleUrlRequest.Api.() -> Unit = {}): Deferred<M> = async(Dispatchers.IO) {
|
|
|
+ if (!isActive) throw CancellationException()
|
|
|
+
|
|
|
+ val uid = coroutineContext[CoroutineExceptionHandler]
|
|
|
+ coroutineContext[Job]?.invokeOnCompletion {
|
|
|
+ if (it != null) Canceler.cancel(uid) else Canceler.removeCancel(uid)
|
|
|
+ }
|
|
|
+
|
|
|
+ val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
+
|
|
|
+ val request = SimpleUrlRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.GET)
|
|
|
+ .tag(tag)
|
|
|
+ .uid(uid)
|
|
|
+ .cacheKey(path)
|
|
|
+ .cacheMode(cache)
|
|
|
+
|
|
|
+ val response = try {
|
|
|
+ request.apply(block)
|
|
|
+ .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
|
|
|
+ } catch (e: SocketException) {
|
|
|
+ throw CancellationException()
|
|
|
+ }
|
|
|
+
|
|
|
+ if (response.isSucceed) response.success!! else throw response.failure!!
|
|
|
}
|
|
|
|
|
|
|
|
|
inline fun <reified M> CoroutineScope.Post(
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleBodyRequest.Api.() -> Unit)? = null
|
|
|
-): Deferred<M> = async(Dispatchers.IO) {
|
|
|
- _submitBody<M>(RequestMethod.POST, path, tag, cache, absolutePath, block)
|
|
|
-}
|
|
|
+ path: String,
|
|
|
+ tag: Any? = null,
|
|
|
+ cache: CacheMode = CacheMode.HTTP,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ noinline block: SimpleBodyRequest.Api.() -> Unit = {}): Deferred<M> =
|
|
|
+ async(Dispatchers.IO) {
|
|
|
+ if (!isActive) throw CancellationException()
|
|
|
+
|
|
|
+ val uid = coroutineContext[CoroutineExceptionHandler]
|
|
|
+ coroutineContext[Job]?.invokeOnCompletion {
|
|
|
+ if (it != null) Canceler.cancel(uid) else Canceler.removeCancel(uid)
|
|
|
+ }
|
|
|
+
|
|
|
+ val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
+
|
|
|
+ val request = SimpleBodyRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.POST)
|
|
|
+ .tag(tag)
|
|
|
+ .uid(uid)
|
|
|
+ .cacheKey(path)
|
|
|
+ .cacheMode(cache)
|
|
|
+
|
|
|
+ val response = try {
|
|
|
+ request.apply(block)
|
|
|
+ .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
|
|
|
+ } catch (e: SocketException) {
|
|
|
+ throw CancellationException()
|
|
|
+ }
|
|
|
+
|
|
|
+ if (response.isSucceed) response.success!! else throw response.failure!!
|
|
|
+ }
|
|
|
|
|
|
inline fun <reified M> CoroutineScope.Head(
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleUrlRequest.Api.() -> Unit)? = null
|
|
|
-): Deferred<M> = async(Dispatchers.IO) {
|
|
|
- _submitUrl<M>(RequestMethod.HEAD, path, tag, cache, absolutePath, block)
|
|
|
+ path: String,
|
|
|
+ tag: Any? = null,
|
|
|
+ cache: CacheMode = CacheMode.HTTP,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ noinline block: SimpleUrlRequest.Api.() -> Unit = {}): Deferred<M> = async(Dispatchers.IO) {
|
|
|
+ if (!isActive) throw CancellationException()
|
|
|
+
|
|
|
+ val uid = coroutineContext[CoroutineExceptionHandler]
|
|
|
+ coroutineContext[Job]?.invokeOnCompletion {
|
|
|
+ if (it != null) Canceler.cancel(uid) else Canceler.removeCancel(uid)
|
|
|
+ }
|
|
|
+
|
|
|
+ val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
+
|
|
|
+ val request = SimpleUrlRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.HEAD)
|
|
|
+ .tag(tag)
|
|
|
+ .uid(uid)
|
|
|
+ .cacheKey(path)
|
|
|
+ .cacheMode(cache)
|
|
|
+
|
|
|
+ val response = try {
|
|
|
+ request.apply(block)
|
|
|
+ .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
|
|
|
+ } catch (e: SocketException) {
|
|
|
+ throw CancellationException()
|
|
|
+ }
|
|
|
+
|
|
|
+ if (response.isSucceed) response.success!! else throw response.failure!!
|
|
|
}
|
|
|
|
|
|
inline fun <reified M> CoroutineScope.Options(
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleUrlRequest.Api.() -> Unit)? = null
|
|
|
-): Deferred<M> = async(Dispatchers.IO) {
|
|
|
- _submitUrl<M>(RequestMethod.OPTIONS, path, tag, cache, absolutePath, block)
|
|
|
+ path: String,
|
|
|
+ tag: Any? = null,
|
|
|
+ cache: CacheMode = CacheMode.HTTP,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ noinline block: SimpleUrlRequest.Api.() -> Unit = {}): Deferred<M> = async(Dispatchers.IO) {
|
|
|
+ if (!isActive) throw CancellationException()
|
|
|
+
|
|
|
+ val uid = coroutineContext[CoroutineExceptionHandler]
|
|
|
+ coroutineContext[Job]?.invokeOnCompletion {
|
|
|
+ if (it != null) Canceler.cancel(uid) else Canceler.removeCancel(uid)
|
|
|
+ }
|
|
|
+
|
|
|
+ val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
+
|
|
|
+ val request = SimpleUrlRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.OPTIONS)
|
|
|
+ .tag(tag)
|
|
|
+ .uid(uid)
|
|
|
+ .cacheKey(path)
|
|
|
+ .cacheMode(cache)
|
|
|
+
|
|
|
+ val response = try {
|
|
|
+ request.apply(block)
|
|
|
+ .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
|
|
|
+ } catch (e: SocketException) {
|
|
|
+ throw CancellationException()
|
|
|
+ }
|
|
|
+
|
|
|
+ if (response.isSucceed) response.success!! else throw response.failure!!
|
|
|
}
|
|
|
|
|
|
|
|
|
inline fun <reified M> CoroutineScope.Trace(
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleUrlRequest.Api.() -> Unit)? = null
|
|
|
-): Deferred<M> = async(Dispatchers.IO) {
|
|
|
- _submitUrl<M>(RequestMethod.TRACE, path, tag, cache, absolutePath, block)
|
|
|
+ path: String,
|
|
|
+ tag: Any? = null,
|
|
|
+ cache: CacheMode = CacheMode.HTTP,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ noinline block: SimpleUrlRequest.Api.() -> Unit = {}): Deferred<M> = async(Dispatchers.IO) {
|
|
|
+ if (!isActive) throw CancellationException()
|
|
|
+
|
|
|
+ val uid = coroutineContext[CoroutineExceptionHandler]
|
|
|
+ coroutineContext[Job]?.invokeOnCompletion {
|
|
|
+ if (it != null) Canceler.cancel(uid) else Canceler.removeCancel(uid)
|
|
|
+ }
|
|
|
+
|
|
|
+ val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
+
|
|
|
+ val request = SimpleUrlRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.TRACE)
|
|
|
+ .tag(tag)
|
|
|
+ .uid(uid)
|
|
|
+ .cacheKey(path)
|
|
|
+ .cacheMode(cache)
|
|
|
+
|
|
|
+ val response = try {
|
|
|
+ request.apply(block)
|
|
|
+ .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
|
|
|
+ } catch (e: SocketException) {
|
|
|
+ throw CancellationException()
|
|
|
+ }
|
|
|
+
|
|
|
+ if (response.isSucceed) response.success!! else throw response.failure!!
|
|
|
}
|
|
|
|
|
|
inline fun <reified M> CoroutineScope.Delete(
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleBodyRequest.Api.() -> Unit)? = null
|
|
|
-): Deferred<M> = async(Dispatchers.IO) {
|
|
|
- _submitBody<M>(RequestMethod.DELETE, path, tag, cache, absolutePath, block)
|
|
|
+ path: String,
|
|
|
+ tag: Any? = null,
|
|
|
+ cache: CacheMode = CacheMode.HTTP,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ noinline block: SimpleBodyRequest.Api.() -> Unit = {}
|
|
|
+ ): Deferred<M> = async(Dispatchers.IO) {
|
|
|
+ if (!isActive) throw CancellationException()
|
|
|
+
|
|
|
+ val uid = coroutineContext[CoroutineExceptionHandler]
|
|
|
+ coroutineContext[Job]?.invokeOnCompletion {
|
|
|
+ if (it != null) Canceler.cancel(uid) else Canceler.removeCancel(uid)
|
|
|
+ }
|
|
|
+
|
|
|
+ val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
+
|
|
|
+ val request = SimpleBodyRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.DELETE)
|
|
|
+ .tag(tag)
|
|
|
+ .uid(uid)
|
|
|
+ .cacheKey(path)
|
|
|
+ .cacheMode(cache)
|
|
|
+
|
|
|
+ val response = try {
|
|
|
+ request.apply(block)
|
|
|
+ .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
|
|
|
+ } catch (e: SocketException) {
|
|
|
+ throw CancellationException()
|
|
|
+ }
|
|
|
+
|
|
|
+ if (response.isSucceed) response.success!! else throw response.failure!!
|
|
|
}
|
|
|
|
|
|
inline fun <reified M> CoroutineScope.Put(
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleBodyRequest.Api.() -> Unit)? = null
|
|
|
-): Deferred<M> = async(Dispatchers.IO) {
|
|
|
- _submitBody<M>(RequestMethod.PUT, path, tag, cache, absolutePath, block)
|
|
|
-}
|
|
|
+ path: String,
|
|
|
+ tag: Any? = null,
|
|
|
+ cache: CacheMode = CacheMode.HTTP,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ noinline block: SimpleBodyRequest.Api.() -> Unit = {}): Deferred<M> =
|
|
|
+ async(Dispatchers.IO) {
|
|
|
+ if (!isActive) throw CancellationException()
|
|
|
+
|
|
|
+ val uid = coroutineContext[CoroutineExceptionHandler]
|
|
|
+ coroutineContext[Job]?.invokeOnCompletion {
|
|
|
+ if (it != null) Canceler.cancel(uid) else Canceler.removeCancel(uid)
|
|
|
+ }
|
|
|
+
|
|
|
+ val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
+
|
|
|
+ val request = SimpleBodyRequest.newApi(Url.newBuilder(realPath).build(),
|
|
|
+ RequestMethod.PUT)
|
|
|
+ .tag(tag)
|
|
|
+ .uid(uid)
|
|
|
+ .cacheKey(path)
|
|
|
+ .cacheMode(cache)
|
|
|
+
|
|
|
+ val response = try {
|
|
|
+ request.apply(block)
|
|
|
+ .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
|
|
|
+ } catch (e: SocketException) {
|
|
|
+ throw CancellationException()
|
|
|
+ }
|
|
|
+
|
|
|
+ if (response.isSucceed) response.success!! else throw response.failure!!
|
|
|
+ }
|
|
|
|
|
|
inline fun <reified M> CoroutineScope.Patch(
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleBodyRequest.Api.() -> Unit)? = null
|
|
|
-): Deferred<M> = async(Dispatchers.IO) {
|
|
|
- _submitBody<M>(RequestMethod.PATCH, path, tag, cache, absolutePath, block)
|
|
|
-}
|
|
|
+ path: String,
|
|
|
+ tag: Any? = null,
|
|
|
+ cache: CacheMode = CacheMode.HTTP,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ noinline block: SimpleBodyRequest.Api.() -> Unit = {}): Deferred<M> =
|
|
|
+ async(Dispatchers.IO) {
|
|
|
+ if (!isActive) throw CancellationException()
|
|
|
+
|
|
|
+ val uid = coroutineContext[CoroutineExceptionHandler]
|
|
|
+ coroutineContext[Job]?.invokeOnCompletion {
|
|
|
+ if (it != null) Canceler.cancel(uid) else Canceler.removeCancel(uid)
|
|
|
+ }
|
|
|
+
|
|
|
+ val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
+
|
|
|
+ val request =
|
|
|
+ SimpleBodyRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.PATCH)
|
|
|
+ .tag(tag)
|
|
|
+ .uid(uid)
|
|
|
+ .cacheKey(path)
|
|
|
+ .cacheMode(cache)
|
|
|
+
|
|
|
+ val response = try {
|
|
|
+ request.apply(block)
|
|
|
+ .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
|
|
|
+ } catch (e: SocketException) {
|
|
|
+ throw CancellationException()
|
|
|
+ }
|
|
|
+
|
|
|
+ if (response.isSucceed) response.success!! else throw response.failure!!
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -128,32 +304,34 @@ inline fun <reified M> CoroutineScope.Patch(
|
|
|
* @param block 请求参数
|
|
|
*/
|
|
|
fun CoroutineScope.Download(
|
|
|
- path: String,
|
|
|
- method: RequestMethod = RequestMethod.GET,
|
|
|
- dir: String = NetConfig.app.externalCacheDir!!.absolutePath,
|
|
|
- tag: Any? = null,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- block: (UrlDownload.Api.() -> Unit)? = null
|
|
|
-): Deferred<String> = async(Dispatchers.IO) {
|
|
|
+ path: String,
|
|
|
+ method: RequestMethod = RequestMethod.GET,
|
|
|
+ dir: String = NetConfig.app.externalCacheDir!!.absolutePath,
|
|
|
+ tag: Any? = null,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ block: UrlDownload.Api.() -> Unit = {}): Deferred<String> = async(Dispatchers.IO) {
|
|
|
+
|
|
|
+ if (!isActive) throw CancellationException()
|
|
|
+
|
|
|
+ val uid = coroutineContext[CoroutineExceptionHandler]
|
|
|
+ coroutineContext[Job]?.invokeOnCompletion {
|
|
|
+ if (it != null && it !is CancellationException) Canceler.cancel(uid) else Canceler.removeCancel(uid)
|
|
|
+ }
|
|
|
|
|
|
val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
|
|
|
- val download =
|
|
|
- UrlDownload.newApi(Url.newBuilder(realPath).build(), method).directory(dir).tag(tag)
|
|
|
+ val download = UrlDownload.newApi(Url.newBuilder(realPath).build(), method).directory(dir).tag(tag).uid(uid)
|
|
|
|
|
|
- if (isActive) {
|
|
|
- if (block == null) {
|
|
|
- download.perform()
|
|
|
- } else {
|
|
|
- download.apply(block).perform()
|
|
|
- }
|
|
|
- } else {
|
|
|
+ try {
|
|
|
+ download.apply(block).perform()
|
|
|
+ } catch (e: SocketException) {
|
|
|
throw CancellationException()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 下载图片, 图片宽高要求要么同时指定要么同时不指定
|
|
|
+ * 要求依赖 Glide
|
|
|
*
|
|
|
* @receiver Context
|
|
|
* @param url String
|
|
@@ -161,183 +339,228 @@ fun CoroutineScope.Download(
|
|
|
* @param height Int 图片高度
|
|
|
* @return Observable<File>
|
|
|
*/
|
|
|
-fun CoroutineScope.DownloadImg(
|
|
|
- url: String,
|
|
|
- with: Int = -1,
|
|
|
- height: Int = -1
|
|
|
-): Deferred<File> = async(Dispatchers.IO) {
|
|
|
+fun CoroutineScope.DownloadImg(url: String, with: Int = -1, height: Int = -1): Deferred<File> =
|
|
|
+ async(Dispatchers.IO) {
|
|
|
|
|
|
- val download = Glide.with(NetConfig.app).download(url)
|
|
|
+ val download = Glide.with(NetConfig.app).download(url)
|
|
|
|
|
|
- val futureTarget = if (with == -1 && height == -1) {
|
|
|
- download.submit()
|
|
|
- } else {
|
|
|
- download.submit(with, height)
|
|
|
- }
|
|
|
+ val futureTarget = if (with == -1 && height == -1) {
|
|
|
+ download.submit()
|
|
|
+ } else {
|
|
|
+ download.submit(with, height)
|
|
|
+ }
|
|
|
|
|
|
- futureTarget.get()
|
|
|
-}
|
|
|
+ futureTarget.get()
|
|
|
+ }
|
|
|
|
|
|
+// </editor-fold>
|
|
|
+
|
|
|
+// <editor-fold desc="同步请求">
|
|
|
|
|
|
-inline fun <reified M> _submitBody(
|
|
|
- method: RequestMethod,
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleBodyRequest.Api.() -> Unit)? = null
|
|
|
-): M {
|
|
|
+inline fun <reified M> syncGet(
|
|
|
+ path: String,
|
|
|
+ tag: Any? = null,
|
|
|
+ cache: CacheMode = CacheMode.HTTP,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ noinline block: SimpleUrlRequest.Api.() -> Unit = {}): M {
|
|
|
|
|
|
val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
|
|
|
- val request = SimpleBodyRequest.newApi(Url.newBuilder(realPath).build(), method)
|
|
|
- .tag(tag)
|
|
|
- .cacheKey(path)
|
|
|
- .cacheMode(cache)
|
|
|
+ val request = SimpleUrlRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.GET)
|
|
|
+ .tag(tag)
|
|
|
+ .cacheKey(path)
|
|
|
+ .cacheMode(cache)
|
|
|
|
|
|
- val response = if (block == null) {
|
|
|
- request.perform(M::class.java, ResponseException::class.java)
|
|
|
- } else {
|
|
|
- request.apply(block).perform<M, String>(M::class.java, ResponseException::class.java)
|
|
|
- }
|
|
|
+ val response = request.apply(block)
|
|
|
+ .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
|
|
|
|
|
|
return if (response.isSucceed) {
|
|
|
response.success!!
|
|
|
} else {
|
|
|
- throw response.failure as ResponseException
|
|
|
+ throw response.failure!!
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-inline fun <reified M> _submitUrl(
|
|
|
- method: RequestMethod,
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleUrlRequest.Api.() -> Unit)? = null
|
|
|
-): M {
|
|
|
+inline fun <reified M> syncPost(
|
|
|
+ path: String,
|
|
|
+ tag: Any? = null,
|
|
|
+ cache: CacheMode = CacheMode.HTTP,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ noinline block: SimpleBodyRequest.Api.() -> Unit = {}): M {
|
|
|
|
|
|
val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
|
|
|
- val request = SimpleUrlRequest.newApi(Url.newBuilder(realPath).build(), method)
|
|
|
- .tag(tag)
|
|
|
- .cacheKey(path)
|
|
|
- .cacheMode(cache)
|
|
|
+ val request = SimpleBodyRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.POST)
|
|
|
+ .tag(tag)
|
|
|
+ .cacheKey(path)
|
|
|
+ .cacheMode(cache)
|
|
|
|
|
|
- val response = if (block == null) {
|
|
|
- request.perform(M::class.java, ResponseException::class.java)
|
|
|
- } else {
|
|
|
- request.apply(block).perform<M, String>(M::class.java, ResponseException::class.java)
|
|
|
- }
|
|
|
+ val response = request.apply(block)
|
|
|
+ .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
|
|
|
|
|
|
return if (response.isSucceed) {
|
|
|
response.success!!
|
|
|
} else {
|
|
|
- throw response.failure as ResponseException
|
|
|
+ throw response.failure!!
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// </editor-fold>
|
|
|
+inline fun <reified M> syncHead(
|
|
|
+ path: String,
|
|
|
+ tag: Any? = null,
|
|
|
+ cache: CacheMode = CacheMode.HTTP,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ noinline block: SimpleUrlRequest.Api.() -> Unit = {}): M {
|
|
|
|
|
|
-// <editor-fold desc="同步请求">
|
|
|
+ val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
|
|
|
-inline fun <reified M> syncGet(
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleUrlRequest.Api.() -> Unit)? = null
|
|
|
-): M {
|
|
|
- return _submitUrl<M>(RequestMethod.GET, path, tag, cache, absolutePath, block)
|
|
|
-}
|
|
|
+ val request = SimpleUrlRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.HEAD)
|
|
|
+ .tag(tag)
|
|
|
+ .cacheKey(path)
|
|
|
+ .cacheMode(cache)
|
|
|
|
|
|
-inline fun <reified M> syncPost(
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleBodyRequest.Api.() -> Unit)? = null
|
|
|
-): M {
|
|
|
- return _submitBody<M>(RequestMethod.POST, path, tag, cache, absolutePath, block)
|
|
|
-}
|
|
|
+ val response = request.apply(block)
|
|
|
+ .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
|
|
|
|
|
|
-inline fun <reified M> syncHead(
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleUrlRequest.Api.() -> Unit)? = null
|
|
|
-): M {
|
|
|
- return _submitUrl<M>(RequestMethod.HEAD, path, tag, cache, absolutePath, block)
|
|
|
+ return if (response.isSucceed) {
|
|
|
+ response.success!!
|
|
|
+ } else {
|
|
|
+ throw response.failure!!
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
inline fun <reified M> syncOptions(
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleUrlRequest.Api.() -> Unit)? = null
|
|
|
-): M {
|
|
|
- return _submitUrl<M>(RequestMethod.OPTIONS, path, tag, cache, absolutePath, block)
|
|
|
+ path: String,
|
|
|
+ tag: Any? = null,
|
|
|
+ cache: CacheMode = CacheMode.HTTP,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ noinline block: SimpleUrlRequest.Api.() -> Unit = {}): M {
|
|
|
+
|
|
|
+ val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
+
|
|
|
+ val request = SimpleUrlRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.OPTIONS)
|
|
|
+ .tag(tag)
|
|
|
+ .cacheKey(path)
|
|
|
+ .cacheMode(cache)
|
|
|
+
|
|
|
+ val response = request.apply(block)
|
|
|
+ .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
|
|
|
+
|
|
|
+ return if (response.isSucceed) {
|
|
|
+ response.success!!
|
|
|
+ } else {
|
|
|
+ throw response.failure!!
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
inline fun <reified M> syncTrace(
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleUrlRequest.Api.() -> Unit)? = null
|
|
|
-): M {
|
|
|
- return _submitUrl<M>(RequestMethod.TRACE, path, tag, cache, absolutePath, block)
|
|
|
+ path: String,
|
|
|
+ tag: Any? = null,
|
|
|
+ cache: CacheMode = CacheMode.HTTP,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ noinline block: SimpleUrlRequest.Api.() -> Unit = {}): M {
|
|
|
+
|
|
|
+ val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
+
|
|
|
+ val request = SimpleUrlRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.TRACE)
|
|
|
+ .tag(tag)
|
|
|
+ .cacheKey(path)
|
|
|
+ .cacheMode(cache)
|
|
|
+
|
|
|
+ val response = request.apply(block)
|
|
|
+ .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
|
|
|
+
|
|
|
+ return if (response.isSucceed) {
|
|
|
+ response.success!!
|
|
|
+ } else {
|
|
|
+ throw response.failure!!
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
inline fun <reified M> syncDelete(
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleBodyRequest.Api.() -> Unit)? = null
|
|
|
-): M {
|
|
|
- return _submitBody<M>(RequestMethod.DELETE, path, tag, cache, absolutePath, block)
|
|
|
+ path: String,
|
|
|
+ tag: Any? = null,
|
|
|
+ cache: CacheMode = CacheMode.HTTP,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ noinline block: SimpleBodyRequest.Api.() -> Unit = {}): M {
|
|
|
+
|
|
|
+ val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
+
|
|
|
+ val request = SimpleBodyRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.DELETE)
|
|
|
+ .tag(tag)
|
|
|
+ .cacheKey(path)
|
|
|
+ .cacheMode(cache)
|
|
|
+
|
|
|
+ val response = request.apply(block)
|
|
|
+ .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
|
|
|
+
|
|
|
+ return if (response.isSucceed) {
|
|
|
+ response.success!!
|
|
|
+ } else {
|
|
|
+ throw response.failure!!
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
inline fun <reified M> syncPut(
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleBodyRequest.Api.() -> Unit)? = null
|
|
|
-): M {
|
|
|
- return _submitBody<M>(RequestMethod.PUT, path, tag, cache, absolutePath, block)
|
|
|
+ path: String,
|
|
|
+ tag: Any? = null,
|
|
|
+ cache: CacheMode = CacheMode.HTTP,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ noinline block: SimpleBodyRequest.Api.() -> Unit = {}): M {
|
|
|
+
|
|
|
+ val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
+
|
|
|
+ val request = SimpleBodyRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.PUT)
|
|
|
+ .tag(tag)
|
|
|
+ .cacheKey(path)
|
|
|
+ .cacheMode(cache)
|
|
|
+
|
|
|
+ val response = request.apply(block)
|
|
|
+ .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
|
|
|
+
|
|
|
+ return if (response.isSucceed) {
|
|
|
+ response.success!!
|
|
|
+ } else {
|
|
|
+ throw response.failure!!
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
inline fun <reified M> syncPatch(
|
|
|
- path: String,
|
|
|
- tag: Any? = null,
|
|
|
- cache: CacheMode = CacheMode.HTTP,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- noinline block: (SimpleBodyRequest.Api.() -> Unit)? = null
|
|
|
-): M {
|
|
|
- return _submitBody<M>(RequestMethod.PATCH, path, tag, cache, absolutePath, block)
|
|
|
+ path: String,
|
|
|
+ tag: Any? = null,
|
|
|
+ cache: CacheMode = CacheMode.HTTP,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ noinline block: SimpleBodyRequest.Api.() -> Unit = {}): M {
|
|
|
+
|
|
|
+ val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
+
|
|
|
+ val request = SimpleBodyRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.PATCH)
|
|
|
+ .tag(tag)
|
|
|
+ .cacheKey(path)
|
|
|
+ .cacheMode(cache)
|
|
|
+
|
|
|
+ val response = request.apply(block)
|
|
|
+ .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
|
|
|
+
|
|
|
+ return if (response.isSucceed) {
|
|
|
+ response.success!!
|
|
|
+ } else {
|
|
|
+ throw response.failure!!
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
fun syncDownload(
|
|
|
- path: String,
|
|
|
- directory: String = NetConfig.app.externalCacheDir!!.absolutePath,
|
|
|
- tag: Any? = null,
|
|
|
- absolutePath: Boolean = false,
|
|
|
- block: (UrlDownload.Api.() -> Unit)? = null
|
|
|
-): String {
|
|
|
+ path: String,
|
|
|
+ directory: String = NetConfig.app.externalCacheDir!!.absolutePath,
|
|
|
+ tag: Any? = null,
|
|
|
+ absolutePath: Boolean = false,
|
|
|
+ block: UrlDownload.Api.() -> Unit = {}): String {
|
|
|
|
|
|
val realPath = if (absolutePath) path else (NetConfig.host + path)
|
|
|
|
|
|
val download = Kalle.Download.get(realPath).directory(directory).tag(tag)
|
|
|
|
|
|
- return if (block == null) {
|
|
|
- download.perform()
|
|
|
- } else {
|
|
|
- download.apply(block).perform()
|
|
|
- }
|
|
|
+ return download.apply(block).perform()
|
|
|
}
|
|
|
|
|
|
fun Context.syncDownloadImg(url: String, with: Int = 0, height: Int = 0): File {
|
|
@@ -358,3 +581,5 @@ fun Context.syncDownloadImg(url: String, with: Int = 0, height: Int = 0): File {
|
|
|
// </editor-fold>
|
|
|
|
|
|
|
|
|
+
|
|
|
+
|