为了熟悉 SwiftUI 应用程序开发,我正在使用 AppAuth 库 制作一个具有登录功能的简单应用程序。我已经可以使用基本的登录和注销功能,并且可以使用收到的访问令牌发出请求。目前,所有请求都是从单个类发出的。对于真正的应用程序,我想拆分请求并将它们放在不同的文件中。这就是我在用户登录后发出基本 API 请求以获取用户数据的方式:
class AuthStore: ObservableObject {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
private var authState : OIDAuthState?
@Published var isAuthenticated = false
func getUserData() {
let userEndpoint = URL(string: "https://example.com/my-api/user/1")!
self.authState?.performAction() { accessToken, idToken, error in
if error != nil {
print("Error fetching fresh tokens: \(error?.localizedDescription ?? "Unknown error")")
return
}
guard let accessToken = accessToken else {
return
}
// Add Bearer token to request
var urlRequest = URLRequest(url: userEndpoint)
urlRequest.allHTTPHeaderFields = ["Authorization": "Bearer \(accessToken)"]
let task = URLSession.shared.dataTask(with: urlRequest) { data, response, error in
print("Performing patron information request")
if let error = error {
print("Error: \(error.localizedDescription)")
return
}
if let data = data {
do {
let decodedData = try JSONDecoder().decode(User.self, from: data)
self.patron = decodedData
} catch {
print("JSON Decoding error: \(error.localizedDescription)")
}
}
}
task.resume()
}
}
如何使需要访问令牌的 API 请求更加通用和可重用?我想到的解决方案之一是使用泛型,如下所示:
func getData(url: URL) -> T {
self.authState?.performAction() { accessToken, idToken, error in
if error != nil {
print("Error fetching fresh tokens: \(error?.localizedDescription ?? "Unknown error")")
return
}
guard let accessToken = accessToken else {
return
}
// Add Bearer token to request
var urlRequest = URLRequest(url: url)
urlRequest.allHTTPHeaderFields = ["Authorization": "Bearer \(accessToken)"]
let task = URLSession.shared.dataTask(with: urlRequest) { data, response, error in
print("Performing patron information request")
if let error = error {
print("Error: \(error.localizedDescription)")
return
}
if let data = data {
do {
let decodedData = try JSONDecoder().decode(T.self, from: data)
self.patron = decodedData
} catch {
print("JSON Decoding error: \(error.localizedDescription)")
}
}
}
task.resume()
}
}
或者向
AuthStore
类添加一个方法会更好,该方法返回一个新的访问令牌,然后可以在其他地方使用该令牌来发出请求?