-
Notifications
You must be signed in to change notification settings - Fork 159
Minor validation logic improvements #782
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
92a4e0c
ac3d3c4
e898941
a1257ed
666cbf3
79f7fb9
f4f4e52
b55107a
c74abb8
6667f57
4bf60ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| patch type="changed" "Minor validation logic improvements" |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -30,6 +30,7 @@ public enum LiveKitErrorType: Int, Sendable { | |||
| case webRTC = 201 | ||||
|
|
||||
| case network // Network issue | ||||
| case validation // Network issue | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I'd rename that to Otherwise LGTM^2
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we used to call this "validation", the endpoint (path is /validate)
So maybe validation is better ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If that matches backend, then why not |
||||
|
|
||||
| // Server | ||||
| case duplicateIdentity = 500 | ||||
|
|
@@ -76,6 +77,8 @@ extension LiveKitErrorType: CustomStringConvertible { | |||
| "WebRTC error" | ||||
| case .network: | ||||
| "Network error" | ||||
| case .validation: | ||||
| "Validation error" | ||||
| case .duplicateIdentity: | ||||
| "Duplicate Participant identity" | ||||
| case .serverShutdown: | ||||
|
|
@@ -111,26 +114,29 @@ extension LiveKitErrorType: CustomStringConvertible { | |||
| public class LiveKitError: NSError, @unchecked Sendable { | ||||
| public let type: LiveKitErrorType | ||||
| public let message: String? | ||||
| public let underlyingError: Error? | ||||
| public let internalError: Error? | ||||
|
|
||||
| override public var underlyingErrors: [Error] { | ||||
| [underlyingError].compactMap { $0 } | ||||
| [internalError].compactMap { $0 } | ||||
| } | ||||
|
|
||||
| public init(_ type: LiveKitErrorType, | ||||
| message: String? = nil, | ||||
| internalError: Error? = nil) | ||||
| { | ||||
| func _computeDescription() -> String { | ||||
| var suffix = "" | ||||
| if let message { | ||||
| return "\(String(describing: type))(\(message))" | ||||
| suffix = "(\(message))" | ||||
| } else if let internalError { | ||||
| suffix = "(\(internalError.localizedDescription))" | ||||
| } | ||||
| return String(describing: type) | ||||
| return String(describing: type) + suffix | ||||
| } | ||||
|
|
||||
| self.type = type | ||||
| self.message = message | ||||
| underlyingError = internalError | ||||
| self.internalError = internalError | ||||
| super.init(domain: "io.livekit.swift-sdk", | ||||
| code: type.rawValue, | ||||
| userInfo: [NSLocalizedDescriptionKey: _computeDescription()]) | ||||
|
|
||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,27 +16,51 @@ | |
|
|
||
| import Foundation | ||
|
|
||
| enum ServerValidationResponse { | ||
| case valid | ||
| case invalid(message: String) | ||
| // Network error etc. | ||
| case unknown(error: Error) | ||
|
||
| } | ||
|
|
||
| class HTTP: NSObject { | ||
| static let statusCodeOK = 200 | ||
|
|
||
| private static let operationQueue = OperationQueue() | ||
|
|
||
| private static let session: URLSession = .init(configuration: .default, | ||
| delegate: nil, | ||
| delegateQueue: operationQueue) | ||
|
|
||
| static func requestValidation(from url: URL, token: String) async throws -> String { | ||
| // let data = try await requestData(from: url, token: token) | ||
| var request = URLRequest(url: url, | ||
| cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, | ||
| timeoutInterval: .defaultHTTPConnect) | ||
| // Attach token to header | ||
| request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization") | ||
| // Make the data request | ||
| let (data, _) = try await session.data(for: request) | ||
| // Convert to string | ||
| guard let string = String(data: data, encoding: .utf8) else { | ||
| throw LiveKitError(.failedToConvertData, message: "Failed to convert string") | ||
| } | ||
| static func requestValidation(from url: URL, token: String) async -> ServerValidationResponse { | ||
| do { | ||
| var request = URLRequest(url: url, | ||
| cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, | ||
| timeoutInterval: .defaultHTTPConnect) | ||
| // Attach token to header | ||
| request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization") | ||
|
|
||
| // Make the data request | ||
| let (data, response) = try await session.data(for: request) | ||
|
|
||
| return string | ||
| // Print HTTP status code | ||
| guard let httpResponse = response as? HTTPURLResponse else { | ||
| throw URLError(.badServerResponse) | ||
| } | ||
|
|
||
| // Valid if 200 | ||
| if httpResponse.statusCode == statusCodeOK { | ||
| return .valid | ||
| } | ||
|
|
||
| guard let string = String(data: data, encoding: .utf8) else { | ||
| throw URLError(.badServerResponse) | ||
| } | ||
|
|
||
| // Consider anything other than 200 invalid | ||
| return .invalid(message: string) | ||
| } catch { | ||
| return .unknown(error: error) | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.