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

Commit

Permalink
update send message endpoints and extract out (#66)
Browse files Browse the repository at this point in the history
* update send message endpoints and extract out

* update http endpoint paths
  • Loading branch information
kirahsapong authored Mar 20, 2024
1 parent 497ac58 commit a46f188
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions Sources/tbDEX/HttpClient/tbDEXHttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,72 @@ public enum tbDEXHttpClient {
throw Error(reason: "Error while fetching offerings: \(error)")
}
}

/// Sends an RFQ and options to the PFI to initiate an exchange
/// - Parameters:
/// - rfq: The RFQ message that will be sent to the PFI
/// - Throws: if message verification fails
/// - Throws: if recipient DID resolution fails
/// - Throws: if recipient DID does not have a PFI service entry
public static func createExchange(rfq: RFQ) async throws {
try await sendMessage(message: rfq, messageEndpoint: "/exchanges")
}

/// Sends the Order message to the PFI
/// - Parameters:
/// - order: The Order message that will be sent to the PFI
/// - Throws: if message verification fails
/// - Throws: if recipient DID resolution fails
/// - Throws: if recipient DID does not have a PFI service entry
public static func submitOrder(order: Order) async throws {
let exchangeID = order.metadata.exchangeID
try await sendMessage(message: order, messageEndpoint: "/exchanges/\(exchangeID)")
}

/// Sends the Close message to the PFI
/// - Parameters:
/// - order: The Close message that will be sent to the PFI
/// - Throws: if message verification fails
/// - Throws: if recipient DID resolution fails
/// - Throws: if recipient DID does not have a PFI service entry
public static func submitClose(close: Close) async throws {
let exchangeID = close.metadata.exchangeID
try await sendMessage(message: close, messageEndpoint: "/exchanges/\(exchangeID)")
}

/// Sends a message to a PFI
/// - Parameter message: The message to send
public static func sendMessage<D: MessageData>(
message: Message<D>
/// - Parameters:
/// - message: The message to send
/// - messageEndpoint: The endpoint for the message with a leading slash. eg. "/exchanges"
private static func sendMessage<D: MessageData>(
message: Message<D>,
messageEndpoint: String
) async throws {
guard try await message.verify() else {
throw Error(reason: "Message signature is invalid")
}

let pfiDidUri = message.metadata.to
let exchangeID = message.metadata.exchangeID
let kind = message.metadata.kind

guard let pfiServiceEndpoint = await getPFIServiceEndpoint(pfiDIDURI: pfiDidUri) else {
throw Error(reason: "DID does not have service of type PFI")
}
guard let url = URL(string: "\(pfiServiceEndpoint)/exchanges/\(exchangeID)/\(kind.rawValue)") else {
guard let url = URL(string: "\(pfiServiceEndpoint)\(messageEndpoint)") else {
throw Error(reason: "Could not create URL from PFI service endpoint")
}

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")

if case .rfq = message.metadata.kind {
// Sending an RFQ means creating an exchange, so we POST
request.httpMethod = "POST"
// RFQs are special, and wrap their message in an `rfq` object.
request.httpBody = try tbDEXJSONEncoder().encode(["rfq": message])
} else {
// Adding messages to an exchange requires a PUT
request.httpMethod = "PUT"
// All other messages encode their messages directly to the http body
request.httpBody = try tbDEXJSONEncoder().encode(message)
}
Expand Down

0 comments on commit a46f188

Please sign in to comment.