|
|
|
@ -5,6 +5,7 @@ import com.google.gson.JsonParser
|
|
|
|
|
import info.nightscout.sdk.exceptions.DateHeaderOutOfToleranceException
|
|
|
|
|
import info.nightscout.sdk.exceptions.InvalidAccessTokenException
|
|
|
|
|
import info.nightscout.sdk.exceptions.InvalidFormatNightscoutException
|
|
|
|
|
import info.nightscout.sdk.exceptions.InvalidParameterNightscoutException
|
|
|
|
|
import info.nightscout.sdk.exceptions.UnknownResponseNightscoutException
|
|
|
|
|
import info.nightscout.sdk.exceptions.UnsuccessfullNightscoutException
|
|
|
|
|
import info.nightscout.sdk.interfaces.NSAndroidClient
|
|
|
|
@ -105,20 +106,22 @@ class NSAndroidClientImpl(
|
|
|
|
|
val response = api.lastModified()
|
|
|
|
|
if (response.isSuccessful) {
|
|
|
|
|
return@callWrapper response.body()?.result ?: throw UnsuccessfullNightscoutException()
|
|
|
|
|
} else {
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getSgvs(): NSAndroidClient.ReadResponse<List<NSSgvV3>> = callWrapper(dispatcher) {
|
|
|
|
|
|
|
|
|
|
val response = api.getSgvs()
|
|
|
|
|
if (response.isSuccessful) {
|
|
|
|
|
return@callWrapper NSAndroidClient.ReadResponse(code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = 0, values = response.body()?.result?.map(RemoteEntry::toSgv).toNotNull())
|
|
|
|
|
} else {
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getSgvsModifiedSince(from: Long, limit: Long): NSAndroidClient.ReadResponse<List<NSSgvV3>> = callWrapper(dispatcher) {
|
|
|
|
|
|
|
|
|
@ -127,20 +130,22 @@ class NSAndroidClientImpl(
|
|
|
|
|
val eTagString = response.headers()["ETag"]
|
|
|
|
|
val eTag = eTagString?.substring(3, eTagString.length - 1)?.toLong()
|
|
|
|
|
return@callWrapper NSAndroidClient.ReadResponse(code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = eTag, values = response.body()?.result?.map(RemoteEntry::toSgv).toNotNull())
|
|
|
|
|
} else {
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getSgvsNewerThan(from: Long, limit: Long): NSAndroidClient.ReadResponse<List<NSSgvV3>> = callWrapper(dispatcher) {
|
|
|
|
|
|
|
|
|
|
val response = api.getSgvsNewerThan(from, limit)
|
|
|
|
|
if (response.isSuccessful) {
|
|
|
|
|
return@callWrapper NSAndroidClient.ReadResponse(code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = 0, values = response.body()?.result?.map(RemoteEntry::toSgv).toNotNull())
|
|
|
|
|
} else {
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun createSvg(nsSgvV3: NSSgvV3): CreateUpdateResponse = callWrapper(dispatcher) {
|
|
|
|
|
|
|
|
|
@ -176,7 +181,10 @@ class NSAndroidClientImpl(
|
|
|
|
|
identifier = null,
|
|
|
|
|
errorResponse = errorResponse
|
|
|
|
|
)
|
|
|
|
|
} else throw UnknownResponseNightscoutException()
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun updateSvg(nsSgvV3: NSSgvV3): CreateUpdateResponse = callWrapper(dispatcher) {
|
|
|
|
@ -205,20 +213,22 @@ class NSAndroidClientImpl(
|
|
|
|
|
deduplicatedIdentifier = null,
|
|
|
|
|
lastModified = null
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getTreatmentsNewerThan(createdAt: String, limit: Long): NSAndroidClient.ReadResponse<List<NSTreatment>> = callWrapper(dispatcher) {
|
|
|
|
|
|
|
|
|
|
val response = api.getTreatmentsNewerThan(createdAt, limit)
|
|
|
|
|
if (response.isSuccessful) {
|
|
|
|
|
return@callWrapper NSAndroidClient.ReadResponse(code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = 0, values = response.body()?.result?.map(RemoteTreatment::toTreatment).toNotNull())
|
|
|
|
|
} else {
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getTreatmentsModifiedSince(from: Long, limit: Long): NSAndroidClient.ReadResponse<List<NSTreatment>> = callWrapper(dispatcher) {
|
|
|
|
|
|
|
|
|
@ -226,22 +236,26 @@ class NSAndroidClientImpl(
|
|
|
|
|
if (response.isSuccessful) {
|
|
|
|
|
val eTagString = response.headers()["ETag"]
|
|
|
|
|
val eTag = eTagString?.substring(3, eTagString.length - 1)?.toLong()
|
|
|
|
|
return@callWrapper NSAndroidClient.ReadResponse(code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = eTag, values = response.body()?.result?.map
|
|
|
|
|
(RemoteTreatment::toTreatment).toNotNull())
|
|
|
|
|
} else {
|
|
|
|
|
return@callWrapper NSAndroidClient.ReadResponse(
|
|
|
|
|
code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = eTag, values = response.body()?.result?.map
|
|
|
|
|
(RemoteTreatment::toTreatment).toNotNull()
|
|
|
|
|
)
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getDeviceStatusModifiedSince(from: Long): List<NSDeviceStatus> = callWrapper(dispatcher) {
|
|
|
|
|
|
|
|
|
|
val response = api.getDeviceStatusModifiedSince(from)
|
|
|
|
|
if (response.isSuccessful) {
|
|
|
|
|
return@callWrapper response.body()?.result?.map(RemoteDeviceStatus::toNSDeviceStatus).toNotNull()
|
|
|
|
|
} else {
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun createDeviceStatus(nsDeviceStatus: NSDeviceStatus): CreateUpdateResponse = callWrapper(dispatcher) {
|
|
|
|
|
|
|
|
|
@ -266,10 +280,11 @@ class NSAndroidClientImpl(
|
|
|
|
|
lastModified = response.body()?.result?.lastModified
|
|
|
|
|
)
|
|
|
|
|
} else throw UnknownResponseNightscoutException()
|
|
|
|
|
} else {
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun createTreatment(nsTreatment: NSTreatment): CreateUpdateResponse = callWrapper(dispatcher) {
|
|
|
|
|
|
|
|
|
@ -304,7 +319,10 @@ class NSAndroidClientImpl(
|
|
|
|
|
identifier = null,
|
|
|
|
|
errorResponse = errorResponse
|
|
|
|
|
)
|
|
|
|
|
} else throw UnknownResponseNightscoutException()
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun updateTreatment(nsTreatment: NSTreatment): CreateUpdateResponse = callWrapper(dispatcher) {
|
|
|
|
@ -333,20 +351,22 @@ class NSAndroidClientImpl(
|
|
|
|
|
deduplicatedIdentifier = null,
|
|
|
|
|
lastModified = null
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getFoods(limit: Long): NSAndroidClient.ReadResponse<List<NSFood>> = callWrapper(dispatcher) {
|
|
|
|
|
|
|
|
|
|
val response = api.getFoods(limit)
|
|
|
|
|
if (response.isSuccessful) {
|
|
|
|
|
return@callWrapper NSAndroidClient.ReadResponse(code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = 0, values = response.body()?.result?.map(RemoteFood::toNSFood).toNotNull())
|
|
|
|
|
} else {
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
override suspend fun getFoodsModifiedSince(from: Long, limit: Long): NSAndroidClient.ReadResponse<List<NSFood>> = callWrapper(dispatcher) {
|
|
|
|
@ -412,10 +432,11 @@ class NSAndroidClientImpl(
|
|
|
|
|
deduplicatedIdentifier = null,
|
|
|
|
|
lastModified = null
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun createProfileStore(remoteProfileStore: JSONObject): CreateUpdateResponse = callWrapper(dispatcher) {
|
|
|
|
|
remoteProfileStore.put("app", "AAPS")
|
|
|
|
@ -438,10 +459,11 @@ class NSAndroidClientImpl(
|
|
|
|
|
lastModified = response.body()?.result?.lastModified
|
|
|
|
|
)
|
|
|
|
|
} else throw UnsuccessfullNightscoutException()
|
|
|
|
|
} else {
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun getLastProfileStore(): NSAndroidClient.ReadResponse<List<JSONObject>> = callWrapper(dispatcher) {
|
|
|
|
|
|
|
|
|
@ -450,10 +472,11 @@ class NSAndroidClientImpl(
|
|
|
|
|
val eTagString = response.headers()["ETag"]
|
|
|
|
|
val eTag = eTagString?.substring(3, eTagString.length - 1)?.toLong()
|
|
|
|
|
return@callWrapper NSAndroidClient.ReadResponse(code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = eTag, values = response.body()?.result.toNotNull())
|
|
|
|
|
} else {
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override suspend fun getProfileModifiedSince(from: Long): NSAndroidClient.ReadResponse<List<JSONObject>> = callWrapper(dispatcher) {
|
|
|
|
@ -463,10 +486,11 @@ class NSAndroidClientImpl(
|
|
|
|
|
val eTagString = response.headers()["ETag"]
|
|
|
|
|
val eTag = eTagString?.substring(3, eTagString.length - 1)?.toLong()
|
|
|
|
|
return@callWrapper NSAndroidClient.ReadResponse(code = response.raw().networkResponse?.code ?: response.code(), lastServerModified = eTag, values = response.body()?.result.toNotNull())
|
|
|
|
|
} else {
|
|
|
|
|
} else if (response.code() in 400..499)
|
|
|
|
|
throw InvalidParameterNightscoutException(response.errorBody()?.string() ?: response.message())
|
|
|
|
|
else
|
|
|
|
|
throw UnsuccessfullNightscoutException()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private suspend fun <T> callWrapper(dispatcher: CoroutineDispatcher, block: suspend () -> T): T =
|
|
|
|
|