This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
54 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 50 additions & 7 deletions
57
httpserver/src/main/kotlin/tbdex/sdk/httpserver/handlers/SubmitRfq.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule tbdex
updated
23 files