Skip to content
Source
Sign inSign up
samwillis/jerry
Open History to see the last commit

JOLTypes.swift

72 lines3.6 KB

1import Foundation
2​
3struct JOLError: LocalizedError, Sendable {
4 let message: String
5 var errorDescription: String? { message }
6}
7struct JOLManifest: Codable, Sendable {
8 struct Auth: Codable, Sendable { let issuer: String; let clientID: String; let resource: String; let redirectURI: String }
9 struct Capabilities: Codable, Sendable {
10 let journal: Bool
11 let commands: Bool
12 let photos: Bool
13 let realtime: Bool
14 var detailsBackfill: Bool? = nil
15 var photosBackfill: Bool? = nil
16 }
17 let version: Int
18 let instanceID: String
19 let name: String
20 let origin: String
21 let auth: Auth
22 let capabilities: Capabilities
23 func validate(for server: URL) throws {
24 guard version == 1, UUID(uuidString: instanceID) != nil, origin == server.absoluteString.trimmingCharacters(in: CharacterSet(charactersIn: "/")),
25 let issuer = URL(string: auth.issuer), issuer.scheme == "https", issuer.user == nil, issuer.password == nil,
26 auth.resource == origin + "/api", !auth.clientID.isEmpty,
27 auth.redirectURI == "http://127.0.0.1:49381/jol/callback" else {
28 throw JOLError(message: "This server doesn’t offer a compatible Jerry On Line connection.")
29 }
30 }
31 static func serverURL(_ text: String) throws -> URL {
32 guard let url = URL(string: text.trimmingCharacters(in: .whitespacesAndNewlines)),
33 url.scheme == "https", url.host != nil, url.user == nil, url.password == nil,
34 url.query == nil, url.fragment == nil, url.path.isEmpty || url.path == "/" else {
35 throw JOLError(message: "Enter your JOL server’s HTTPS address, such as https://jol.example.com.")
36 }
37 return URL(string: url.absoluteString.trimmingCharacters(in: CharacterSet(charactersIn: "/")))!
38 }
40struct JOLCommand: Codable, Sendable {
41 let id: UUID
42 let kind: String
43 let prompt: String
44 let expedition_id: UUID?
45 let fence: Int
47struct JOLCommandResult: Sendable { let status: String; let message: String }
48​
49/// Reject redirects rather than allowing a chosen server to move bearer credentials.
50private final class JOLRedirectPolicy: NSObject, URLSessionTaskDelegate, @unchecked Sendable {
51 func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse,
52 newRequest request: URLRequest, completionHandler: @escaping @Sendable (URLRequest?) -> Void) { completionHandler(nil) }
54enum JOLHTTP {
55 static let session: URLSession = {
56 let configuration = URLSessionConfiguration.ephemeral
57 configuration.timeoutIntervalForRequest = 30
58 configuration.timeoutIntervalForResource = 60
59 return URLSession(configuration: configuration, delegate: JOLRedirectPolicy(), delegateQueue: nil)
60 }()
61 static func data(_ request: URLRequest) async throws -> Data {
62 let (data, response) = try await session.data(for: request)
63 guard let http = response as? HTTPURLResponse else { throw JOLError(message: "JOL didn’t respond.") }
64 guard (200..<300).contains(http.statusCode) else {
65 let message = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]
66 if message?["error"] as? String == "invalid_grant" { throw JOLError(message: "Your JOL sign-in expired. Disconnect and connect again.") }
67 throw JOLError(message: String((message?["error"] as? String ?? "JOL connection failed (\(http.statusCode)).").prefix(250)))
68 }
69 guard data.count <= 8_000_000 else { throw JOLError(message: "The JOL response was too large.") }
70 return data
71 }