Skip to content

Commit

Permalink
network: Use a default httpclient on BaseRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
sirambd committed Jul 9, 2024
1 parent cfaae03 commit 550f9f0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import io.ktor.http.URLBuilder
import io.ktor.http.Url
import kotlinx.serialization.json.Json

internal open class BaseRequest(protected val json: Json) {
internal open class BaseRequest(protected val json: Json, protected val httpClient: HttpClient) {

protected fun createUrl(path: String, vararg queries: Pair<String, String>): Url {
val baseUrl = Url(UrlConstants.baseUrl + path)
Expand All @@ -40,20 +40,20 @@ internal open class BaseRequest(protected val json: Json) {
}.build()
}

protected suspend inline fun <reified R> get(client: HttpClient, url: Url): R {
return client.get(url) {}.decode<R>()
protected suspend inline fun <reified R> get(url: Url, httpClient: HttpClient = this.httpClient): R {
return httpClient.get(url) {}.decode<R>()
}

protected suspend inline fun <reified R> post(client: HttpClient, url: Url, data: Any?): R {
return client.post(url) { setBody(data) }.decode<R>()
protected suspend inline fun <reified R> post(url: Url, data: Any?, httpClient: HttpClient = this.httpClient): R {
return httpClient.post(url) { setBody(data) }.decode<R>()
}

protected suspend inline fun <reified R> put(client: HttpClient, url: Url, data: Any?): R {
return client.put(url) { setBody(data) }.decode<R>()
protected suspend inline fun <reified R> put(url: Url, data: Any?, httpClient: HttpClient = this.httpClient): R {
return httpClient.put(url) { setBody(data) }.decode<R>()
}

protected suspend inline fun <reified R> delete(client: HttpClient, url: Url): R {
return client.delete(url) {}.decode<R>()
protected suspend inline fun <reified R> delete(url: Url, httpClient: HttpClient = this.httpClient): R {
return httpClient.delete(url) {}.decode<R>()
}

protected suspend inline fun <reified R> HttpResponse.decode(): R {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ import com.infomaniak.multiplatform_swisstransfer.network.utils.UrlConstants
import io.ktor.client.HttpClient
import kotlinx.serialization.json.Json

internal class TransferRequest internal constructor(json: Json, private val httpClient: HttpClient) : BaseRequest(json) {
internal class TransferRequest constructor(json: Json, httpClient: HttpClient) : BaseRequest(json, httpClient) {

suspend fun getTransfer(linkUUID: String): ApiResponse<TransferApi> {
val url = createUrl("${UrlConstants.links}/$linkUUID")
return get(httpClient, url)
return get(url = createUrl("${UrlConstants.links}/$linkUUID"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package com.infomaniak.multiplatform_swisstransfer.network.requests
import io.ktor.client.HttpClient
import kotlinx.serialization.json.Json

internal class UploadRequest internal constructor(json: Json, private val httpClient: HttpClient) : BaseRequest(json) {
internal class UploadRequest internal constructor(json: Json, httpClient: HttpClient) : BaseRequest(json, httpClient) {

// TODO: implement method here
}

0 comments on commit 550f9f0

Please sign in to comment.