diff --git a/Sources/FalClient/Client+Request.swift b/Sources/FalClient/Client+Request.swift index 11f41d6..30dc95b 100644 --- a/Sources/FalClient/Client+Request.swift +++ b/Sources/FalClient/Client+Request.swift @@ -15,6 +15,7 @@ extension Client { } if let queryParams, + !queryParams.isEmpty, var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) { urlComponents.queryItems = queryParams.map { diff --git a/Sources/FalClient/Queue.swift b/Sources/FalClient/Queue.swift index 13b56d7..00aaac8 100644 --- a/Sources/FalClient/Queue.swift +++ b/Sources/FalClient/Queue.swift @@ -4,10 +4,9 @@ import Foundation public protocol Queue { var client: Client { get } - /// Submits a request to the given [id], an optional [path]. This method - /// uses the [queue] API to initiate the request. Next you need to rely on - /// [status] and [result] to poll for the result. - func submit(_ id: String, path: String?, input: Payload?, webhookUrl: String?) async throws -> String + /// Submits a request to the given [id]. This method uses the [queue] API to initiate + /// the request. Next you need to rely on [status] and [result] to poll for the result. + func submit(_ id: String, input: Payload?, webhookUrl: String?) async throws -> String /// Checks the queue for the status of the request with the given [requestId]. /// See [QueueStatus] for the different statuses. @@ -24,8 +23,8 @@ public protocol Queue { } public extension Queue { - func submit(_ id: String, path: String? = nil, input: Payload? = nil, webhookUrl: String? = nil) async throws -> String { - try await submit(id, path: path, input: input, webhookUrl: webhookUrl) + func submit(_ id: String, input: Payload? = nil, webhookUrl: String? = nil) async throws -> String { + try await submit(id, input: input, webhookUrl: webhookUrl) } func status(_ id: String, of requestId: String, includeLogs: Bool = false) async throws -> QueueStatus { @@ -49,7 +48,7 @@ public struct QueueStatusInput: Encodable { public struct QueueClient: Queue { public let client: Client - func runOnQueue(_ app: String, input: Payload?, options: RunOptions) async throws -> Payload { + func runOnQueue(_ app: String, input: Payload?, queryParams params: [String: Any] = [:], options: RunOptions = .withMethod(.post)) async throws -> Payload { var requestInput = input if let storage = client.storage as? StorageClient, let input, @@ -58,14 +57,22 @@ public struct QueueClient: Queue { { requestInput = try await storage.autoUpload(input: input) } - let queryParams = options.httpMethod == .get ? input : nil + var queryParams: [String: Any] = [:] + if let inputDict = requestInput?.asDictionary, options.httpMethod == .get { + queryParams.merge(inputDict) { _, new in new } + } + if !params.isEmpty { + queryParams.merge(params) { _, new in new } + } + let url = buildUrl(fromId: app, path: options.path, subdomain: "queue") - let data = try await client.sendRequest(to: url, input: requestInput?.json(), queryParams: queryParams?.asDictionary, options: options) + let data = try await client.sendRequest(to: url, input: requestInput?.json(), queryParams: params, options: options) return try .create(fromJSON: data) } - public func submit(_ id: String, path: String?, input: Payload?, webhookUrl _: String?) async throws -> String { - let result = try await runOnQueue(id, input: input, options: .route(path ?? "", withMethod: .post)) + 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)) guard case let .string(requestId) = result["request_id"] else { throw FalError.invalidResultFormat } @@ -76,7 +83,10 @@ public struct QueueClient: Queue { let appId = try AppId.parse(id: id) let result = try await runOnQueue( "\(appId.ownerId)/\(appId.appAlias)", - input: ["logs": .bool(includeLogs)], + input: nil, + queryParams: [ + "logs": includeLogs ? 1 : 0, + ], options: .route("/requests/\(requestId)/status", withMethod: .get) ) let json = try result.json()