mirror of
https://github.com/zhigang1992/fal-swift.git
synced 2026-01-12 22:47:17 +08:00
fix(queue): codable input request url (#17)
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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<Output: Decodable>(_ 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)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Output: Decodable>(_ 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 {
|
||||
|
||||
Reference in New Issue
Block a user