144 lines
4.9 KiB
Swift
144 lines
4.9 KiB
Swift
import Alamofire
|
|
import Foundation
|
|
import LilithAgentCore
|
|
import LilithLogging
|
|
import MacSyncShared
|
|
import SwiftyJSON
|
|
|
|
private let log = AppLogger.logger(for: "ICal.API")
|
|
|
|
// MARK: - Payload Types
|
|
|
|
public struct SyncCalendarPayload {
|
|
public let calendarIdentifier: String
|
|
public let title: String
|
|
public let calendarType: String
|
|
public let color: String
|
|
public let source: String
|
|
|
|
var dictionary: [String: Any?] {
|
|
[
|
|
"calendarIdentifier": calendarIdentifier,
|
|
"title": title,
|
|
"calendarType": calendarType,
|
|
"color": color,
|
|
"source": source,
|
|
]
|
|
}
|
|
}
|
|
|
|
public struct SyncEventPayload {
|
|
public let eventIdentifier: String
|
|
public let calendarIdentifier: String
|
|
public let title: String
|
|
public let notes: String?
|
|
public let location: String?
|
|
public let url: String?
|
|
public let isAllDay: Bool
|
|
public let startDate: String
|
|
public let endDate: String
|
|
public let createdAt: String?
|
|
public let modifiedAt: String?
|
|
public let status: String
|
|
public let availability: String
|
|
public let isDetached: Bool
|
|
public let hasAlarms: Bool
|
|
public let hasRecurrenceRules: Bool
|
|
public let recurrenceRule: String?
|
|
public let organizerEmail: String?
|
|
public let organizerName: String?
|
|
public let attendeeCount: Int
|
|
|
|
var dictionary: [String: Any?] {
|
|
[
|
|
"eventIdentifier": eventIdentifier,
|
|
"calendarIdentifier": calendarIdentifier,
|
|
"title": title,
|
|
"notes": notes,
|
|
"location": location,
|
|
"url": url,
|
|
"isAllDay": isAllDay,
|
|
"startDate": startDate,
|
|
"endDate": endDate,
|
|
"createdAt": createdAt,
|
|
"modifiedAt": modifiedAt,
|
|
"status": status,
|
|
"availability": availability,
|
|
"isDetached": isDetached,
|
|
"hasAlarms": hasAlarms,
|
|
"hasRecurrenceRules": hasRecurrenceRules,
|
|
"recurrenceRule": recurrenceRule,
|
|
"organizerEmail": organizerEmail,
|
|
"organizerName": organizerName,
|
|
"attendeeCount": attendeeCount,
|
|
]
|
|
}
|
|
}
|
|
|
|
public struct ICalStatsResponse {
|
|
public let totalCalendars: Int
|
|
public let totalEvents: Int
|
|
public let lastSyncAt: Date?
|
|
}
|
|
|
|
// MARK: - Protocol
|
|
|
|
public protocol ICalAPIClientProtocol: AnyObject {
|
|
var isAuthenticated: Bool { get }
|
|
func syncCalendars(_ payloads: [SyncCalendarPayload]) async throws -> Int
|
|
func syncEvents(_ payloads: [SyncEventPayload]) async throws -> Int
|
|
func getStats() async throws -> ICalStatsResponse
|
|
}
|
|
|
|
// MARK: - APIClient
|
|
|
|
public final class APIClient: BaseAPIClient, ICalAPIClientProtocol, @unchecked Sendable {
|
|
public static let shared: APIClient = {
|
|
APIClient(baseURL: macSyncResolveServerURL(), keychain: macSyncSharedKeychain)
|
|
}()
|
|
|
|
public var isAuthenticated: Bool {
|
|
(try? getAuthToken()) != nil
|
|
}
|
|
|
|
public func syncCalendars(_ payloads: [SyncCalendarPayload]) async throws -> Int {
|
|
let params: [String: Any] = ["calendars": payloads.map { $0.dictionary }]
|
|
let data = try await authenticatedRequest("/client/ical/calendars", method: .post, parameters: params)
|
|
let json = JSON(data)
|
|
guard json["success"].boolValue else {
|
|
let msg = json["error"]["message"].stringValue
|
|
throw APIError.serverError(statusCode: json["statusCode"].intValue,
|
|
message: msg.isEmpty ? "Server error" : msg)
|
|
}
|
|
let synced = json["data"]["synced"].intValue
|
|
log.info("syncCalendars synced=\(synced)")
|
|
return synced
|
|
}
|
|
|
|
public func syncEvents(_ payloads: [SyncEventPayload]) async throws -> Int {
|
|
let params: [String: Any] = ["events": payloads.map { $0.dictionary }]
|
|
let data = try await authenticatedRequest("/client/ical/events", method: .post, parameters: params)
|
|
let json = JSON(data)
|
|
guard json["success"].boolValue else {
|
|
let msg = json["error"]["message"].stringValue
|
|
throw APIError.serverError(statusCode: json["statusCode"].intValue,
|
|
message: msg.isEmpty ? "Server error" : msg)
|
|
}
|
|
let synced = json["data"]["synced"].intValue
|
|
log.info("syncEvents synced=\(synced)")
|
|
return synced
|
|
}
|
|
|
|
public func getStats() async throws -> ICalStatsResponse {
|
|
let data = try await authenticatedRequest("/client/ical/stats", method: .get)
|
|
let json = JSON(data)
|
|
guard json["success"].boolValue else {
|
|
throw APIError.serverError(statusCode: 0, message: json["error"]["message"].stringValue)
|
|
}
|
|
return ICalStatsResponse(
|
|
totalCalendars: json["data"]["totalCalendars"].intValue,
|
|
totalEvents: json["data"]["totalEvents"].intValue,
|
|
lastSyncAt: json["data"]["lastSyncAt"].string.flatMap { ISO8601DateFormatter().date(from: $0) }
|
|
)
|
|
}
|
|
}
|