Skip to content

Commit

Permalink
Throw exception on non-200 statuses (#113)
Browse files Browse the repository at this point in the history
* Throw exception on non-200 statuses

* Fix test
  • Loading branch information
Ifropc authored Aug 3, 2023
1 parent 9270283 commit e672c84
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 28 deletions.
1 change: 1 addition & 0 deletions wallet-sdk/src/main/kotlin/org/stellar/walletsdk/Wallet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ data class ApplicationConfiguration(
HttpClient(OkHttp) {
install(ContentNegotiation) { json(defaultJson) }
defaultRequest { url { protocol = URLProtocol.HTTPS } }
expectSuccess = true
defaultClientConfig()
}
}
Expand Down
18 changes: 3 additions & 15 deletions wallet-sdk/src/main/kotlin/org/stellar/walletsdk/customer/Sep12.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import io.ktor.http.*
import org.stellar.walletsdk.auth.AuthToken
import org.stellar.walletsdk.exception.CustomerNotFoundException
import org.stellar.walletsdk.exception.CustomerUpdateException
import org.stellar.walletsdk.exception.ErrorOnDeletingCustomerException
import org.stellar.walletsdk.exception.UnauthorizedCustomerDeletionException
import org.stellar.walletsdk.util.Util.authDelete
import org.stellar.walletsdk.util.Util.authGet
import org.stellar.walletsdk.util.Util.putJson
Expand Down Expand Up @@ -68,7 +66,7 @@ internal constructor(
urlBuilder.appendPathSegments("customer")
val urlString = urlBuilder.buildString()

return httpClient.putJson<Map<String, String>, AddCustomerResponse>(urlString, customer, token)
return httpClient.putJson(urlString, customer, token)
}

/**
Expand Down Expand Up @@ -103,7 +101,7 @@ internal constructor(
urlBuilder.appendPathSegments("customer")
val urlString = urlBuilder.buildString()

return httpClient.putJson<Map<String, String>, AddCustomerResponse>(urlString, customer, token)
return httpClient.putJson(urlString, customer, token)
}

/**
Expand All @@ -118,16 +116,6 @@ internal constructor(
urlBuilder.appendPathSegments(customerAccount)
val urlString = urlBuilder.buildString()

val statusCode = httpClient.authDelete(urlString, memo, token)

if (statusCode == HttpStatusCode.Unauthorized || statusCode == HttpStatusCode.Forbidden) {
throw UnauthorizedCustomerDeletionException(customerAccount)
}
if (statusCode == HttpStatusCode.NotFound) {
throw CustomerNotFoundException("Customer not found")
}
if (statusCode != HttpStatusCode.OK) {
throw ErrorOnDeletingCustomerException(customerAccount)
}
httpClient.authDelete(urlString, memo, token)
}
}
20 changes: 9 additions & 11 deletions wallet-sdk/src/main/kotlin/org/stellar/walletsdk/util/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,16 @@ internal object Util {
url: String,
memo: String?,
authToken: AuthToken?,
): HttpStatusCode {
val response =
this.delete(url) {
if (authToken != null) {
headers { append(HttpHeaders.Authorization, "Bearer $authToken") }
}
if (memo != null) {
contentType(ContentType.Application.Json)
setBody(mapOf("memo" to memo))
}
) {
this.delete(url) {
if (authToken != null) {
headers { append(HttpHeaders.Authorization, "Bearer $authToken") }
}
return response.status
if (memo != null) {
contentType(ContentType.Application.Json)
setBody(mapOf("memo" to memo))
}
}
}

private inline fun <reified T> String.fromJsonOrError(): T {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.stellar.walletsdk

import io.ktor.client.plugins.*
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull
Expand All @@ -10,7 +11,6 @@ import org.junit.jupiter.api.assertDoesNotThrow
import org.stellar.walletsdk.anchor.auth
import org.stellar.walletsdk.anchor.customer
import org.stellar.walletsdk.customer.Sep12Status
import org.stellar.walletsdk.exception.CustomerExceptions
import org.stellar.walletsdk.horizon.SigningKeyPair

class CustomerManagementTest {
Expand Down Expand Up @@ -77,9 +77,12 @@ class CustomerManagementTest {
)

assertDoesNotThrow { runBlocking { customer.delete(testCustomerAccount) } }
assertFailsWith<CustomerExceptions> {
assertFailsWith<ClientRequestException> {
runBlocking { customer.getByIdAndType(addCustomerResponse.id, testCustomerType) }
}
assertFailsWith<ClientRequestException> {
runBlocking { customer.delete(testCustomerAccount) }
}
}
}
}

0 comments on commit e672c84

Please sign in to comment.