Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(queue): codable input request url #17

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Sources/FalClient/Client+Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
23 changes: 15 additions & 8 deletions Sources/FalClient/Queue+Codable.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

import Foundation

public struct QueueSubmitResult: Decodable {
let requestId: String
Expand All @@ -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)
)
}
}
34 changes: 12 additions & 22 deletions Sources/FalClient/Queue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand All @@ -81,16 +72,15 @@ 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: [
"logs": includeLogs ? 1 : 0,
],
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 {
Expand Down
Loading