|
@@ -21,22 +21,16 @@ import org.json.JSONArray
|
|
|
import retrofit2.Call
|
|
|
import retrofit2.Callback
|
|
|
import retrofit2.Response
|
|
|
-import retrofit2.Retrofit
|
|
|
import java.io.File
|
|
|
import kotlin.coroutines.resume
|
|
|
import kotlin.coroutines.suspendCoroutine
|
|
|
|
|
|
object CloudApi {
|
|
|
|
|
|
- private val retrofit: Retrofit by lazyOf(RetrofitUtil.getGsonBuilder().baseUrl(WayneApiConfig.HOST_BASE).build())
|
|
|
- private val retrofitAuthBase: Retrofit by lazyOf(RetrofitUtil.getAuthBuilder().baseUrl(WayneApiConfig.HOST_BASE).build())
|
|
|
- private val retrofitAuthTrx: Retrofit by lazyOf(RetrofitUtil.getAuthBuilder().baseUrl(WayneApiConfig.HOST_TRX).build())
|
|
|
- private val retrofitAuthConfig: Retrofit by lazyOf(RetrofitUtil.getAuthBuilder().baseUrl(WayneApiConfig.HOST_CONFIG).build())
|
|
|
-
|
|
|
- private val serviceLogin: ServiceLogin by lazyOf(retrofit.create(ServiceLogin::class.java))
|
|
|
- private val serviceBase: ServiceBase by lazyOf(retrofitAuthBase.create(ServiceBase::class.java))
|
|
|
- private val serviceTrx: ServiceTrx by lazyOf(retrofitAuthTrx.create(ServiceTrx::class.java))
|
|
|
- private val serviceConfig: ServiceConfig by lazyOf(retrofitAuthConfig.create(ServiceConfig::class.java))
|
|
|
+ private var serviceLogin = makeLoginService()
|
|
|
+ private var serviceBase = makeBaseService()
|
|
|
+ private var serviceTrx = makeTrxService()
|
|
|
+ private var serviceConfig = makeConfigService()
|
|
|
|
|
|
suspend fun login(accountName: String, password: String): DFSResult<*> {
|
|
|
val resultLogin = loginOnly(accountName, password)
|
|
@@ -49,6 +43,25 @@ object CloudApi {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ suspend fun barcodeName(id: Int) = suspendCoroutine<DFSResult<ResultBarcode>> {
|
|
|
+ serviceBase.barcodeInfo(id).enqueue(object : Callback<ResultBarcode> {
|
|
|
+ override fun onResponse(call: Call<ResultBarcode>, response: Response<ResultBarcode>) {
|
|
|
+ val code = response.code()
|
|
|
+ val body = response.body()
|
|
|
+ if (code == 200 && body != null) {
|
|
|
+ it.resume(DFSResult.success(body))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ it.resume(DFSResult.fail(R.string.req_fail))
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onFailure(call: Call<ResultBarcode>, t: Throwable) {
|
|
|
+ DFSLog.e(t)
|
|
|
+ it.resume(DFSResult.fail(t.message.toString()))
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
private suspend fun loginOnly(accountName: String, password: String) = suspendCoroutine<DFSResult<ResultLogin>> {
|
|
|
serviceLogin.login(accountName, password).enqueue(object : Callback<ResultLogin> {
|
|
|
|
|
@@ -192,22 +205,38 @@ object CloudApi {
|
|
|
return DFSResult.fail(R.string.req_fail)
|
|
|
}
|
|
|
|
|
|
- suspend fun barcodeName(id: Int) = suspendCoroutine<DFSResult<ResultBarcode>> {
|
|
|
- serviceBase.barcodeInfo(id).enqueue(object : Callback<ResultBarcode> {
|
|
|
- override fun onResponse(call: Call<ResultBarcode>, response: Response<ResultBarcode>) {
|
|
|
- val code = response.code()
|
|
|
- val body = response.body()
|
|
|
- if (code == 200 && body != null) {
|
|
|
- it.resume(DFSResult.success(body))
|
|
|
- return
|
|
|
- }
|
|
|
- it.resume(DFSResult.fail(R.string.req_fail))
|
|
|
- }
|
|
|
+ private fun makeLoginService(): ServiceLogin {
|
|
|
+ return RetrofitUtil.getGsonBuilder()
|
|
|
+ .baseUrl(WayneApiConfig.HOST_BASE)
|
|
|
+ .build()
|
|
|
+ .create(ServiceLogin::class.java)
|
|
|
+ }
|
|
|
|
|
|
- override fun onFailure(call: Call<ResultBarcode>, t: Throwable) {
|
|
|
- DFSLog.e(t)
|
|
|
- it.resume(DFSResult.fail(t.message.toString()))
|
|
|
- }
|
|
|
- })
|
|
|
+ private fun makeBaseService(): ServiceBase {
|
|
|
+ return RetrofitUtil.getAuthBuilder()
|
|
|
+ .baseUrl(WayneApiConfig.HOST_BASE)
|
|
|
+ .build()
|
|
|
+ .create(ServiceBase::class.java)
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun makeTrxService(): ServiceTrx {
|
|
|
+ return RetrofitUtil.getAuthBuilder()
|
|
|
+ .baseUrl(WayneApiConfig.HOST_TRX)
|
|
|
+ .build()
|
|
|
+ .create(ServiceTrx::class.java)
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun makeConfigService(): ServiceConfig {
|
|
|
+ return RetrofitUtil.getAuthBuilder()
|
|
|
+ .baseUrl(WayneApiConfig.HOST_CONFIG)
|
|
|
+ .build()
|
|
|
+ .create(ServiceConfig::class.java)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun regenerateAllService() {
|
|
|
+ serviceLogin = makeLoginService()
|
|
|
+ serviceBase = makeBaseService()
|
|
|
+ serviceTrx = makeTrxService()
|
|
|
+ serviceConfig = makeConfigService()
|
|
|
}
|
|
|
}
|