2022-11-20 15:39:06 +00:00
|
|
|
import Foundation
|
2022-11-21 08:31:32 +00:00
|
|
|
import SwiftUI
|
2022-12-01 08:05:26 +00:00
|
|
|
import Models
|
2022-11-20 15:39:06 +00:00
|
|
|
|
2022-12-20 14:37:51 +00:00
|
|
|
public class Client: ObservableObject, Equatable {
|
|
|
|
public static func == (lhs: Client, rhs: Client) -> Bool {
|
|
|
|
lhs.isAuth == rhs.isAuth &&
|
|
|
|
lhs.server == rhs.server &&
|
|
|
|
lhs.oauthToken?.accessToken == rhs.oauthToken?.accessToken
|
|
|
|
}
|
|
|
|
|
2022-11-20 15:39:06 +00:00
|
|
|
public enum Version: String {
|
|
|
|
case v1
|
|
|
|
}
|
|
|
|
|
2022-12-01 08:05:26 +00:00
|
|
|
public enum OauthError: Error {
|
|
|
|
case missingApp
|
|
|
|
case invalidRedirectURL
|
|
|
|
}
|
|
|
|
|
|
|
|
public var server: String
|
2022-11-20 15:39:06 +00:00
|
|
|
public let version: Version
|
|
|
|
private let urlSession: URLSession
|
|
|
|
private let decoder = JSONDecoder()
|
|
|
|
|
2022-12-01 08:05:26 +00:00
|
|
|
/// Only used as a transitionary app while in the oauth flow.
|
2022-12-20 15:08:09 +00:00
|
|
|
private var oauthApp: InstanceApp?
|
2022-12-01 08:05:26 +00:00
|
|
|
|
|
|
|
private var oauthToken: OauthToken?
|
|
|
|
|
|
|
|
public var isAuth: Bool {
|
|
|
|
oauthToken != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
public init(server: String, version: Version = .v1, oauthToken: OauthToken? = nil) {
|
2022-11-20 15:39:06 +00:00
|
|
|
self.server = server
|
|
|
|
self.version = version
|
|
|
|
self.urlSession = URLSession.shared
|
|
|
|
self.decoder.keyDecodingStrategy = .convertFromSnakeCase
|
2022-12-01 08:05:26 +00:00
|
|
|
self.oauthToken = oauthToken
|
2022-11-20 15:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func makeURL(endpoint: Endpoint) -> URL {
|
|
|
|
var components = URLComponents()
|
|
|
|
components.scheme = "https"
|
|
|
|
components.host = server
|
2022-12-01 08:05:26 +00:00
|
|
|
if type(of: endpoint) == Oauth.self {
|
|
|
|
components.path += "/\(endpoint.path())"
|
|
|
|
} else {
|
|
|
|
components.path += "/api/\(version.rawValue)/\(endpoint.path())"
|
|
|
|
}
|
2022-11-25 11:00:01 +00:00
|
|
|
components.queryItems = endpoint.queryItems()
|
2022-11-20 15:39:06 +00:00
|
|
|
return components.url!
|
|
|
|
}
|
2022-12-01 08:05:26 +00:00
|
|
|
|
|
|
|
private func makeURLRequest(url: URL, httpMethod: String) -> URLRequest {
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
request.httpMethod = httpMethod
|
|
|
|
if let oauthToken {
|
|
|
|
request.setValue("Bearer \(oauthToken.accessToken)", forHTTPHeaderField: "Authorization")
|
|
|
|
}
|
|
|
|
return request
|
|
|
|
}
|
2022-12-24 08:21:04 +00:00
|
|
|
|
|
|
|
private func makeGet(endpoint: Endpoint) -> URLRequest {
|
|
|
|
let url = makeURL(endpoint: endpoint)
|
|
|
|
return makeURLRequest(url: url, httpMethod: "GET")
|
|
|
|
}
|
2022-11-20 15:39:06 +00:00
|
|
|
|
2022-12-01 08:05:26 +00:00
|
|
|
public func get<Entity: Decodable>(endpoint: Endpoint) async throws -> Entity {
|
2022-12-24 08:21:04 +00:00
|
|
|
let (data, httpResponse) = try await urlSession.data(for: makeGet(endpoint: endpoint))
|
2022-12-01 08:05:26 +00:00
|
|
|
logResponseOnError(httpResponse: httpResponse, data: data)
|
2022-11-20 15:39:06 +00:00
|
|
|
return try decoder.decode(Entity.self, from: data)
|
|
|
|
}
|
2022-12-01 08:05:26 +00:00
|
|
|
|
2022-12-24 08:21:04 +00:00
|
|
|
public func getWithLink<Entity: Decodable>(endpoint: Endpoint) async throws -> (Entity, LinkHandler?) {
|
|
|
|
let (data, httpResponse) = try await urlSession.data(for: makeGet(endpoint: endpoint))
|
|
|
|
var linkHandler: LinkHandler?
|
|
|
|
if let response = httpResponse as? HTTPURLResponse,
|
|
|
|
let link = response.allHeaderFields["Link"] as? String{
|
|
|
|
linkHandler = .init(rawLink: link)
|
|
|
|
}
|
|
|
|
logResponseOnError(httpResponse: httpResponse, data: data)
|
|
|
|
return (try decoder.decode(Entity.self, from: data), linkHandler)
|
|
|
|
}
|
|
|
|
|
2022-12-01 08:05:26 +00:00
|
|
|
public func post<Entity: Decodable>(endpoint: Endpoint) async throws -> Entity {
|
|
|
|
let url = makeURL(endpoint: endpoint)
|
|
|
|
let request = makeURLRequest(url: url, httpMethod: "POST")
|
|
|
|
let (data, httpResponse) = try await urlSession.data(for: request)
|
|
|
|
logResponseOnError(httpResponse: httpResponse, data: data)
|
|
|
|
return try decoder.decode(Entity.self, from: data)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func oauthURL() async throws -> URL {
|
2022-12-20 15:08:09 +00:00
|
|
|
let app: InstanceApp = try await post(endpoint: Apps.registerApp)
|
2022-12-01 08:05:26 +00:00
|
|
|
self.oauthApp = app
|
|
|
|
return makeURL(endpoint: Oauth.authorize(clientId: app.clientId))
|
|
|
|
}
|
|
|
|
|
|
|
|
public func continueOauthFlow(url: URL) async throws -> OauthToken {
|
|
|
|
guard let app = oauthApp else {
|
|
|
|
throw OauthError.missingApp
|
|
|
|
}
|
|
|
|
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
|
|
|
|
let code = components.queryItems?.first(where: { $0.name == "code"})?.value else {
|
|
|
|
throw OauthError.invalidRedirectURL
|
|
|
|
}
|
|
|
|
let token: OauthToken = try await post(endpoint: Oauth.token(code: code,
|
|
|
|
clientId: app.clientId,
|
|
|
|
clientSecret: app.clientSecret))
|
|
|
|
self.oauthToken = token
|
|
|
|
return token
|
|
|
|
}
|
|
|
|
|
|
|
|
private func logResponseOnError(httpResponse: URLResponse, data: Data) {
|
|
|
|
if let httpResponse = httpResponse as? HTTPURLResponse, httpResponse.statusCode > 299 {
|
|
|
|
print(httpResponse)
|
|
|
|
print(String(data: data, encoding: .utf8) ?? "")
|
|
|
|
}
|
|
|
|
}
|
2022-11-20 15:39:06 +00:00
|
|
|
}
|