mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-25 16:22:35 +03:00
refactor libs.versions.toml
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
dependencies {
|
||||
implementation(rootProject.libs.springFramework.context)
|
||||
implementation(rootProject.libs.ktor.client.cio)
|
||||
implementation(rootProject.libs.ktor.client.core)
|
||||
implementation(rootProject.libs.io.ktor.ktor.client.cio)
|
||||
implementation(rootProject.libs.io.ktor.ktor.client.core)
|
||||
implementation(rootProject.libs.org.springframework.spring.context)
|
||||
|
||||
testImplementation(rootProject.libs.ktor.client.mock)
|
||||
testImplementation(rootProject.libs.io.ktor.ktor.client.mock)
|
||||
}
|
||||
|
||||
@@ -5,43 +5,57 @@ import com.github.dannecron.demo.edgeintegration.client.neko.dto.ImagesResponse
|
||||
import com.github.dannecron.demo.edgeintegration.client.neko.exceptions.RequestException
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.HttpClientEngine
|
||||
import io.ktor.client.plugins.defaultRequest
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import io.ktor.http.path
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
class ClientImpl(
|
||||
engine: HttpClientEngine,
|
||||
private val baseUrl: String,
|
||||
): Client {
|
||||
private val httpClient = HttpClient(engine)
|
||||
private val httpClient = HttpClient(engine) {
|
||||
defaultRequest {
|
||||
url(baseUrl)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getCategories() = runBlocking {
|
||||
httpClient.get(urlString = baseUrl) {
|
||||
httpClient.get {
|
||||
url {
|
||||
path("/api/v2/endpoints")
|
||||
}
|
||||
}
|
||||
.takeIf { it.status.value in 200..209 }
|
||||
?.let {
|
||||
response -> Json.decodeFromString<Map<String, CategoryFormat>>(response.bodyAsText()).keys
|
||||
.let { response ->
|
||||
val responseBody = response.bodyAsText()
|
||||
if (response.status.value in 200..209) {
|
||||
Json.decodeFromString<Map<String, CategoryFormat>>(responseBody).keys
|
||||
} else {
|
||||
throw RequestException(
|
||||
"get categories error. Status: ${response.status.value}, response: $responseBody"
|
||||
)
|
||||
}
|
||||
}
|
||||
?: throw RequestException("get categories error")
|
||||
}
|
||||
|
||||
override fun getImages(category: String, amount: Int) = runBlocking {
|
||||
httpClient.get(urlString = baseUrl) {
|
||||
httpClient.get {
|
||||
url {
|
||||
path("/api/v2/$category")
|
||||
parameters.append("amount", amount.toString())
|
||||
}
|
||||
}
|
||||
.takeIf { it.status.value in 200..209 }
|
||||
?.let {
|
||||
response -> Json.decodeFromString<ImagesResponse>(response.bodyAsText())
|
||||
.let { response ->
|
||||
val responseBody = response.bodyAsText()
|
||||
if (response.status.value in 200..209) {
|
||||
Json.decodeFromString<ImagesResponse>(responseBody)
|
||||
} else {
|
||||
throw RequestException(
|
||||
"get images error. Status: ${response.status.value}, response: $responseBody"
|
||||
)
|
||||
}
|
||||
}
|
||||
?: throw RequestException("get images error")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user