From d442574bed7c3c8020ee0d54b0a1964f3d7230bf Mon Sep 17 00:00:00 2001 From: Daniel Rochetti Date: Tue, 12 Mar 2024 16:16:08 -0700 Subject: [PATCH] fix(queue): codable input request url --- Sources/FalClient/Client+Request.swift | 3 ++- Sources/FalClient/Queue+Codable.swift | 23 +++++++++++------ Sources/FalClient/Queue.swift | 34 +++++++++----------------- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/Sources/FalClient/Client+Request.swift b/Sources/FalClient/Client+Request.swift index 30dc95b..1917894 100644 --- a/Sources/FalClient/Client+Request.swift +++ b/Sources/FalClient/Client+Request.swift @@ -24,6 +24,7 @@ extension Client { url = urlComponents.url ?? url } + let targetUrl = url if let requestProxy = config.requestProxy { guard let proxyUrl = URL(string: requestProxy) else { throw FalError.invalidUrl(url: requestProxy) @@ -45,7 +46,7 @@ extension Client { // setup the request proxy if available if config.requestProxy != nil { - request.setValue(urlString, forHTTPHeaderField: "x-fal-target-url") + request.setValue(targetUrl.absoluteString, forHTTPHeaderField: "x-fal-target-url") } if input != nil, options.httpMethod != .get { diff --git a/Sources/FalClient/Queue+Codable.swift b/Sources/FalClient/Queue+Codable.swift index 029a22c..f174ffd 100644 --- a/Sources/FalClient/Queue+Codable.swift +++ b/Sources/FalClient/Queue+Codable.swift @@ -1,4 +1,4 @@ - +import Foundation public struct QueueSubmitResult: Decodable { let requestId: String @@ -9,16 +9,23 @@ public struct QueueSubmitResult: Decodable { } public extension Queue { - func submit(_ id: String, input: (some Encodable) = EmptyInput.empty, webhookUrl _: String? = nil) async throws -> String { - let result: QueueSubmitResult = try await client.run(id, input: input, options: .route("/fal/queue/submit")) - return result.requestId + func submit(_ id: String, input: (some Encodable) = EmptyInput.empty, webhookUrl: String? = nil) async throws -> String { + // Convert some Encodable to Payload, so the underlying call can inspect the input more freely + var inputPayload: Payload? = nil + if !(input is EmptyInput) { + let encoder = JSONEncoder() + let data = try encoder.encode(input) + inputPayload = try Payload.create(fromJSON: data) + } + return try await submit(id, input: inputPayload, webhookUrl: webhookUrl) } func response(_ id: String, of requestId: String) async throws -> Output { - try await client.run( - id, - input: EmptyInput.empty, - options: .route("/fal/queue/requests/\(requestId)/response", withMethod: .get) + let appId = try AppId.parse(id: id) + return try await runOnQueue( + "\(appId.ownerId)/\(appId.appAlias)", + input: nil as Payload?, + options: .route("/requests/\(requestId)", withMethod: .get) ) } } diff --git a/Sources/FalClient/Queue.swift b/Sources/FalClient/Queue.swift index 00aaac8..82a6dc8 100644 --- a/Sources/FalClient/Queue.swift +++ b/Sources/FalClient/Queue.swift @@ -32,23 +32,8 @@ public extension Queue { } } -public struct QueueStatusInput: Encodable { - let logs: Bool - - enum CodingKeys: String, CodingKey { - case logs - } - - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(logs ? 1 : 0, forKey: .logs) - } -} - -public struct QueueClient: Queue { - public let client: Client - - func runOnQueue(_ app: String, input: Payload?, queryParams params: [String: Any] = [:], options: RunOptions = .withMethod(.post)) async throws -> Payload { +extension Queue { + func runOnQueue(_ app: String, input: Payload?, queryParams params: [String: Any] = [:], options: RunOptions = .withMethod(.post)) async throws -> Output { var requestInput = input if let storage = client.storage as? StorageClient, let input, @@ -67,12 +52,18 @@ public struct QueueClient: Queue { let url = buildUrl(fromId: app, path: options.path, subdomain: "queue") let data = try await client.sendRequest(to: url, input: requestInput?.json(), queryParams: params, options: options) - return try .create(fromJSON: data) + + let decoder = JSONDecoder() + return try decoder.decode(Output.self, from: data) } +} + +public struct QueueClient: Queue { + public let client: Client public func submit(_ id: String, input: Payload?, webhookUrl: String?) async throws -> String { let queryParams: [String: Any] = webhookUrl != nil ? ["fal_webhook": webhookUrl ?? ""] : [:] - let result = try await runOnQueue(id, input: input, queryParams: queryParams, options: .withMethod(.post)) + let result: Payload = try await runOnQueue(id, input: input, queryParams: queryParams, options: .withMethod(.post)) guard case let .string(requestId) = result["request_id"] else { throw FalError.invalidResultFormat } @@ -81,7 +72,7 @@ public struct QueueClient: Queue { public func status(_ id: String, of requestId: String, includeLogs: Bool) async throws -> QueueStatus { let appId = try AppId.parse(id: id) - let result = try await runOnQueue( + let result: QueueStatus = try await runOnQueue( "\(appId.ownerId)/\(appId.appAlias)", input: nil, queryParams: [ @@ -89,8 +80,7 @@ public struct QueueClient: Queue { ], options: .route("/requests/\(requestId)/status", withMethod: .get) ) - let json = try result.json() - return try JSONDecoder().decode(QueueStatus.self, from: json) + return result } public func response(_ id: String, of requestId: String) async throws -> Payload {