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

Commit

Permalink
adding submitrfq
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyoonie9 committed Dec 14, 2023
1 parent 8120389 commit bb59e47
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import tbdex.sdk.httpserver.models.OfferingsApi
import tbdex.sdk.httpserver.models.SubmitCallback
import tbdex.sdk.protocol.models.MessageKind
import tbdex.sdk.protocol.models.Offering
import tbdex.server.tbdex.sdk.httpserver.handlers.submitRfq
import tbdex.sdk.httpserver.handlers.submitRfq

fun main() {
val serverConfig = TbdexHttpServerConfig(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,76 @@
package tbdex.server.tbdex.sdk.httpserver.handlers
package tbdex.sdk.httpserver.handlers

import io.ktor.http.HttpStatusCode
import io.ktor.server.application.ApplicationCall
import io.ktor.server.request.receiveText
import io.ktor.server.response.respond
import tbdex.sdk.httpclient.models.ErrorDetail
import tbdex.sdk.httpserver.models.CallbackError
import tbdex.sdk.httpserver.models.ExchangesApi
import tbdex.sdk.httpserver.models.OfferingsApi
import tbdex.sdk.httpserver.models.SubmitCallback
import tbdex.sdk.protocol.models.Message
import tbdex.sdk.protocol.models.MessageKind
import tbdex.sdk.protocol.models.Rfq

// validate the message and then invoke submitCallback

// todo: decide on the correct error response format
suspend fun submitRfq(
call: ApplicationCall,
offeringsApi: OfferingsApi,
exchangesApi: ExchangesApi,
callback: SubmitCallback?
) {
val message: Rfq?

try {
val message = Message.parse(call.receiveText()) as Rfq
val offering = offeringsApi.getOffering(message.data.offeringId.toString())
message = Message.parse(call.receiveText()) as Rfq
} catch (e: Exception) {
val errorResponse = ErrorDetail(detail = "Parsing of TBDex message failed: ${e.message}")
call.respond(HttpStatusCode.BadRequest, errorResponse)
return
}

callback?.invoke(call, MessageKind.rfq, offering)
call.respond(HttpStatusCode.Accepted)
val existingRfq = exchangesApi.getRfq(message.metadata.exchangeId.toString())
if (existingRfq != null) {
val errorResponse = ErrorDetail(detail = "RFQ already exists.")
call.respond(HttpStatusCode.Conflict, errorResponse)
return
}

val offering = offeringsApi.getOffering(message.data.offeringId.toString())
if (offering == null) {
val errorResponse = ErrorDetail(detail = "Offering with id ${message.data.offeringId} does not exist.")
call.respond(HttpStatusCode.BadRequest, errorResponse)
return
}

try {
message.verifyOfferingRequirements(offering)
} catch (e: Exception) {
val errorResponse = ErrorDetail(detail = "Parsing of TBDex message failed: ${e.message}")
val errorResponse = ErrorDetail(detail = "Failed to verify offering requirements: ${e.message}")
call.respond(HttpStatusCode.BadRequest, errorResponse)
return
}

if (callback == null) {
call.respond(HttpStatusCode.Accepted)
return
}

try {
callback.invoke(call, MessageKind.rfq, offering)
} catch (e: Exception) {
if (e is CallbackError) {
// todo: e.details is a list of ErrorDetail. do we want to include all the errordetails?
call.respond(e.statusCode, e.details!!.first())
return
} else {
val errorResponse = ErrorDetail(detail = "umm idk")
call.respond(HttpStatusCode.InternalServerError, errorResponse)
return
}
}

call.respond(HttpStatusCode.Accepted)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import tbdex.sdk.httpclient.models.ErrorDetail
import tbdex.sdk.protocol.models.MessageKind
import tbdex.sdk.protocol.models.Offering

//typealias GetCallback<T> = (RequestContext, Filters[T]) -> Any
//typealias SubmitCallback<T, O> = (RequestContext, MessageKindClasses[T], SubmitCallbackOpts[O]) -> Unit

typealias GetCallback = (ApplicationCall, Filter) -> Any
typealias SubmitCallback = (ApplicationCall, MessageKind, Offering?) -> Unit

Expand All @@ -19,7 +16,8 @@ class GetOfferingsFilter(
) : Filter

class GetExchangesFilter(
val exchangeIds: List<String>
val exchangeIds: List<String>,
val from: String
) : Filter

sealed interface Filter
Expand Down

0 comments on commit bb59e47

Please sign in to comment.