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

Commit

Permalink
Split client sendMessage into separate methods (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
Diane Huxley authored Mar 5, 2024
1 parent 1689d87 commit e7301f1
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 64 deletions.
106 changes: 66 additions & 40 deletions httpclient/src/main/kotlin/tbdex/sdk/httpclient/TbdexHttpClient.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tbdex.sdk.httpclient

import com.fasterxml.jackson.module.kotlin.convertValue
import de.fxlae.typeid.TypeId
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
Expand All @@ -16,9 +15,11 @@ import tbdex.sdk.httpclient.models.GetExchangesFilter
import tbdex.sdk.httpclient.models.GetOfferingsFilter
import tbdex.sdk.httpclient.models.TbdexResponseException
import tbdex.sdk.protocol.Validator
import tbdex.sdk.protocol.models.Close
import tbdex.sdk.protocol.models.Message
import tbdex.sdk.protocol.models.MessageKind
import tbdex.sdk.protocol.models.Offering
import tbdex.sdk.protocol.models.Order
import tbdex.sdk.protocol.models.Rfq
import tbdex.sdk.protocol.serialization.Json
import tbdex.sdk.protocol.serialization.Json.jsonMapper
Expand Down Expand Up @@ -72,75 +73,100 @@ object TbdexHttpClient {
}
}

private fun sendMessage(pfiDid: String, path: String, requestBody: RequestBody) {

val pfiServiceEndpoint = getPfiServiceEndpoint(pfiDid)
val url = pfiServiceEndpoint + path

val request = buildRequest(url, requestBody)

println("Attempting to send rfq message to: ${request.url}")

executeRequest(request)
}

/**
* Sends a message to the PFI. You can also use this message to create an exchange without a replyTo URL.
* Send RFQ message to the PFI.
*
* @param message The message to send.
* @param rfq The RFQ to send
* @param replyTo The callback URL for PFI to send messages to.
*
* @throws TbdexResponseException for response errors.
*/
fun sendMessage(message: Message) {
validateMessage(message)
fun createExchange(rfq: Rfq) {
validateMessage(rfq)

val pfiDid = message.metadata.to
val exchangeId = message.metadata.exchangeId
val kind = message.metadata.kind
val pfiDid = rfq.metadata.to
val exchangeId = rfq.metadata.exchangeId

val pfiServiceEndpoint = getPfiServiceEndpoint(pfiDid)

val body: RequestBody
val url: String
if (kind == MessageKind.rfq) {
body = Json.stringify(CreateExchangeRequest(message as Rfq)).toRequestBody(jsonMediaType)
url = "$pfiServiceEndpoint/exchanges/$exchangeId"
} else {
body = Json.stringify(message).toRequestBody(jsonMediaType)
url = "$pfiServiceEndpoint/exchanges/$exchangeId/$kind"
}

val request = buildRequest(url, body)
val path = "/exchanges/$exchangeId"

println("Attempting to send $kind message to: ${request.url}")
val body: RequestBody = Json.stringify(CreateExchangeRequest(rfq))
.toRequestBody(jsonMediaType)

executeRequest(request)
this.sendMessage(pfiDid, path, body)
}

/**
* Send RFQ message and include a replyTo URL for the PFI to send a callback to.
*
* @param message The message to send (is of type RFQ)
* @param rfq The RFQ to send
* @param replyTo The callback URL for PFI to send messages to.
*
* @throws TbdexResponseException for response errors.
*
*/
fun sendMessage(message: Rfq, replyTo: String) {
validateMessage(message)
fun createExchange(rfq: Rfq, replyTo: String) {
validateMessage(rfq)

val pfiDid = message.metadata.to
val exchangeId = message.metadata.exchangeId
val pfiDid = rfq.metadata.to
val exchangeId = rfq.metadata.exchangeId

val pfiServiceEndpoint = getPfiServiceEndpoint(pfiDid)
val url = "$pfiServiceEndpoint/exchanges/$exchangeId"
val path = "/exchanges/$exchangeId"

val body: RequestBody = Json.stringify(CreateExchangeRequest(message, replyTo))
val body: RequestBody = Json.stringify(CreateExchangeRequest(rfq, replyTo))
.toRequestBody(jsonMediaType)

val request = buildRequest(url, body)
this.sendMessage(pfiDid, path, body)
}

println("Attempting to send Rfq message to: ${request.url}")
/**
* Send Order message to the PFI.
*
* @param order The Order to send
*
* @throws TbdexResponseException for response errors.
*/
fun submitOrder(order: Order) {
validateMessage(order)

executeRequest(request)
val pfiDid = order.metadata.to
val exchangeId = order.metadata.exchangeId
val path = "/exchanges/$exchangeId/order"

val body: RequestBody = Json.stringify(order)
.toRequestBody(jsonMediaType)

this.sendMessage(pfiDid, path, body)
}

/**
* Aliased method for sendMessage(Rfq, String) to create an exchange by sending an RFQ with a replyTo URL.
* Send Order message to the PFI.
*
* @param message The message to send (is of type RFQ)
* @param replyTo The callback URL for PFI to send messages to.
* @param order The Order to send
*
* @throws TbdexResponseException for response errors.
*/
fun createExchange(message: Rfq, replyTo: String) {
sendMessage(message, replyTo)
fun submitClose(close: Close) {
validateMessage(close)

val pfiDid = close.metadata.to
val exchangeId = close.metadata.exchangeId
val path = "/exchanges/$exchangeId/close"

val body: RequestBody = Json.stringify(close)
.toRequestBody(jsonMediaType)

this.sendMessage(pfiDid, path, body)
}

/**
Expand Down
3 changes: 1 addition & 2 deletions httpclient/src/test/kotlin/tbdex/sdk/httpclient/E2ETest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tbdex.sdk.httpclient

import com.nimbusds.jose.jwk.JWK
import de.fxlae.typeid.TypeId
import foundation.identity.did.Service
import org.junit.jupiter.api.Disabled
import tbdex.sdk.httpclient.models.GetExchangesFilter
Expand Down Expand Up @@ -127,7 +126,7 @@ class E2ETest {
println("Sending order against Quote with exchangeId of ${order.metadata.exchangeId}")

try {
client.sendMessage(order)
client.submitOrder(order)
} catch (e: TbdexResponseException) {
throw AssertionError(
"Error returned from sending Order. " +
Expand Down
Loading

0 comments on commit e7301f1

Please sign in to comment.