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: webhook url #16

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
1 change: 1 addition & 0 deletions Sources/FalClient/Client+Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extension Client {
}

if let queryParams,
!queryParams.isEmpty,
var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
{
urlComponents.queryItems = queryParams.map {
Expand Down
34 changes: 22 additions & 12 deletions Sources/FalClient/Queue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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
}
Expand All @@ -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()
Expand Down
Loading