Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Allow custom request timeouts to be passed to OkHttp client (#86) #134

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ import tbdex.sdk.protocol.models.Offering
import tbdex.sdk.protocol.serialization.Json
import tbdex.sdk.protocol.serialization.Json.jsonMapper
import web5.sdk.dids.Did
import java.time.Duration

/**
* A client of the tbDEX HTTP interface for communicating with a PFI.
*
* An optional request timeout may be specified. Even if set to Duration.ZERO (default), the default OkHttp timeouts
* will be enforced: 10 seconds to connect, 10 seconds to write the request and 10 seconds to read the response.
*/
object TbdexHttpClient {
private val client = OkHttpClient()
class TbdexHttpClient(timeout: Duration) {
private val client = OkHttpClient.Builder().callTimeout(timeout).build()
private val jsonMediaType = "application/json; charset=utf-8".toMediaType()
private const val JSON_HEADER = "application/json"
private val JSON_HEADER = "application/json"

constructor() : this(Duration.ZERO)

/**
* Fetches offerings from a PFI.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class E2ETest {
@Test
@Disabled("Must be run alongside tbdex-mock-pfi. See README for details")
fun `tests e2e flow`() {
val client = TbdexHttpClient
val client = TbdexHttpClient()

val keyManager = InMemoryKeyManager()
val pfiDid = DidDht.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class TbdexHttpClientTest {
val mockResponseString = Json.jsonMapper.writeValueAsString(mapOf("data" to mockOfferings))
server.enqueue(MockResponse().setBody(mockResponseString).setResponseCode(HttpURLConnection.HTTP_OK))

val response = TbdexHttpClient.getOfferings(pfiDid.uri, null)
val response = TbdexHttpClient().getOfferings(pfiDid.uri, null)

assertEquals(1, response.size)
}
Expand All @@ -80,7 +80,7 @@ class TbdexHttpClientTest {
val mockResponseString = Json.jsonMapper.writeValueAsString(errorDetails)
server.enqueue(MockResponse().setBody(mockResponseString).setResponseCode(HttpURLConnection.HTTP_BAD_REQUEST))

assertThrows<TbdexResponseException> { TbdexHttpClient.getOfferings(pfiDid.uri, null) }
assertThrows<TbdexResponseException> { TbdexHttpClient().getOfferings(pfiDid.uri, null) }
}

@Test
Expand All @@ -89,7 +89,7 @@ class TbdexHttpClientTest {
server.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_ACCEPTED))

val rfq = TestData.getRfq(pfiDid.uri, TypeId.generate("offering"))
assertDoesNotThrow { TbdexHttpClient.sendMessage(rfq) }
assertDoesNotThrow { TbdexHttpClient().sendMessage(rfq) }
}

@Test
Expand All @@ -112,7 +112,7 @@ class TbdexHttpClientTest {
server.enqueue(MockResponse().setBody(mockResponseString).setResponseCode(HttpURLConnection.HTTP_BAD_REQUEST))

val rfq = TestData.getRfq(pfiDid.uri, TypeId.generate("offering"))
val exception = assertThrows<TbdexResponseException> { TbdexHttpClient.sendMessage(rfq) }
val exception = assertThrows<TbdexResponseException> { TbdexHttpClient().sendMessage(rfq) }
assertEquals(1, exception.errors?.size)
assertEquals("400", exception.errors?.get(0)?.status)
}
Expand All @@ -124,7 +124,7 @@ class TbdexHttpClientTest {
val mockResponseString = Json.jsonMapper.writeValueAsString(mapOf("data" to exchange))
server.enqueue(MockResponse().setBody(mockResponseString).setResponseCode(HttpURLConnection.HTTP_OK))

val response = TbdexHttpClient.getExchange(pfiDid.uri, alice, "exchange_1234")
val response = TbdexHttpClient().getExchange(pfiDid.uri, alice, "exchange_1234")

assertEquals(offeringId, (response[0] as Rfq).data.offeringId)
}
Expand All @@ -149,7 +149,7 @@ class TbdexHttpClientTest {
val mockResponseString = Json.jsonMapper.writeValueAsString(errorDetails)
server.enqueue(MockResponse().setBody(mockResponseString).setResponseCode(HttpURLConnection.HTTP_BAD_REQUEST))

assertThrows<TbdexResponseException> { TbdexHttpClient.getExchange(pfiDid.uri, alice, "exchange_1234") }
assertThrows<TbdexResponseException> { TbdexHttpClient().getExchange(pfiDid.uri, alice, "exchange_1234") }
}

@Test
Expand All @@ -159,7 +159,7 @@ class TbdexHttpClientTest {
val mockResponseString = Json.jsonMapper.writeValueAsString(mapOf("data" to exchanges))
server.enqueue(MockResponse().setBody(mockResponseString).setResponseCode(HttpURLConnection.HTTP_OK))

val response = TbdexHttpClient.getExchanges(pfiDid.uri, alice)
val response = TbdexHttpClient().getExchanges(pfiDid.uri, alice)

assertEquals(offeringId, (response[0][0] as Rfq).data.offeringId)
}
Expand Down Expand Up @@ -196,7 +196,7 @@ class TbdexHttpClientTest {
val mockResponseString = Json.jsonMapper.writeValueAsString(errorDetails)
server.enqueue(MockResponse().setBody(mockResponseString).setResponseCode(HttpURLConnection.HTTP_BAD_REQUEST))

assertThrows<TbdexResponseException> { TbdexHttpClient.getExchanges(pfiDid.uri, alice) }
assertThrows<TbdexResponseException> { TbdexHttpClient().getExchanges(pfiDid.uri, alice) }
}

@AfterEach
Expand Down
Loading