Files
fal-swift/Sources/FalClient/Utility.swift
Daniel Rochetti caf1e86d3b feat: fal.run url support (#7)
* feat: fal.run url support

* fix: msgpack ios15 support

* chore: add basic tests and build updates

* fix: build job runs-on

* fix: remove ubuntu build

* fix: remove version matrix for now

* fix: remove test temporarily

* fix: ios version

* fix: temp remove sample build

* chore: update msgpack dependency

* feat: add fal image codable struct

* chore: update all urls to fal.run
2024-01-19 19:19:02 -08:00

34 lines
1.2 KiB
Swift

import Foundation
func buildUrl(fromId id: String, path: String? = nil, subdomain: String? = nil) -> String {
let appId = (try? ensureAppIdFormat(id)) ?? id
let sub = subdomain != nil ? "\(subdomain!)." : ""
return "https://\(sub)fal.run/\(appId)" + (path ?? "")
}
func ensureAppIdFormat(_ id: String) throws -> String {
let parts = id.split(separator: "/")
if parts.count == 2 {
return id
}
let regex = try NSRegularExpression(pattern: "^([0-9]+)-([a-zA-Z0-9-]+)$")
let matches = regex.matches(in: id, options: [], range: NSRange(location: 0, length: id.utf16.count))
if let match = matches.first, match.numberOfRanges == 3,
let appOwnerRange = Range(match.range(at: 1), in: id),
let appIdRange = Range(match.range(at: 2), in: id)
{
let appOwner = String(id[appOwnerRange])
let appId = String(id[appIdRange])
return "\(appOwner)/\(appId)"
}
return id
}
func appAlias(fromId id: String) throws -> String {
let appId = try ensureAppIdFormat(id)
guard let alias = appId.split(separator: "/").last else {
throw FalError.invalidUrl(url: id)
}
return String(describing: alias)
}