diff --git a/LeStorage/ApiCallCollection.swift b/LeStorage/ApiCallCollection.swift index f89d6fb..e49245e 100644 --- a/LeStorage/ApiCallCollection.swift +++ b/LeStorage/ApiCallCollection.swift @@ -357,4 +357,8 @@ actor ApiCallCollection: SomeCallCollection { return self.items.isNotEmpty } + func apiCalls() -> [ApiCall] { + return self.items + } + } diff --git a/LeStorage/Codables/ApiCall.swift b/LeStorage/Codables/ApiCall.swift index 6af96ef..e155658 100644 --- a/LeStorage/Codables/ApiCall.swift +++ b/LeStorage/Codables/ApiCall.swift @@ -7,18 +7,22 @@ import Foundation -protocol SomeCall: Storable { +public protocol SomeCall: Identifiable, Storable { var id: String { get } var lastAttemptDate: Date { get } var attemptsCount: Int { get } + + var method: HTTPMethod { get } + var dataId: String? { get } + var dataContent: String? { get } } -class ApiCall: ModelObject, Storable, SomeCall { +public class ApiCall: ModelObject, Storable, SomeCall { - static func resourceName() -> String { return "apicalls_" + T.resourceName() } + public static func resourceName() -> String { return "apicalls_" + T.resourceName() } static func tokenExemptedMethods() -> [HTTPMethod] { return [] } - var id: String = Store.randomId() + public var id: String = Store.randomId() /// The transactionId to group calls together var transactionId: String = Store.randomId() @@ -27,16 +31,16 @@ class ApiCall: ModelObject, Storable, SomeCall { var creationDate: Date? = Date() /// The HTTP method of the call - var method: HTTPMethod + public var method: HTTPMethod /// The content of the call var data: T? /// The number of times the call has been executed - var attemptsCount: Int = 0 + public var attemptsCount: Int = 0 /// The date of the last execution - var lastAttemptDate: Date = Date() + public var lastAttemptDate: Date = Date() /// The parameters to add in the URL to obtain : "?p1=v1&p2=v2" var urlParameters: [String : String]? = nil @@ -49,7 +53,7 @@ class ApiCall: ModelObject, Storable, SomeCall { } } - func copy(from other: any Storable) { + public func copy(from other: any Storable) { fatalError("should not happen") } @@ -72,10 +76,20 @@ class ApiCall: ModelObject, Storable, SomeCall { } } - static func relationships() -> [Relationship] { return [] } + public var dataId: String? { + return self.data?.stringId + } + + public var dataContent: String? { + if let data = self.data { + return try? data.jsonString() + } + return nil + } + + public static func relationships() -> [Relationship] { return [] } } - class OldApiCall: ModelObject, Storable, SomeCall { static func resourceName() -> String { return "apicalls_" + T.resourceName() } @@ -148,6 +162,8 @@ class OldApiCall: ModelObject, Storable, SomeCall { } } + var dataContent: String? { return self.body } + static func relationships() -> [Relationship] { return [] } func toNewApiCall() -> ApiCall? { diff --git a/LeStorage/Store.swift b/LeStorage/Store.swift index 7213295..9e70366 100644 --- a/LeStorage/Store.swift +++ b/LeStorage/Store.swift @@ -192,8 +192,8 @@ final public class Store { } /// Returns the names of all collections - public func collectionNames() -> [String] { - return self._collections.values.map { $0.resourceName } + public func collectionNames() -> [(String, any Storable.Type)] { + return self._collections.values.map { ($0.resourceName, $0.type) } } // MARK: - Synchronization diff --git a/LeStorage/StoreCenter.swift b/LeStorage/StoreCenter.swift index 9f2f58d..d061b28 100644 --- a/LeStorage/StoreCenter.swift +++ b/LeStorage/StoreCenter.swift @@ -388,7 +388,7 @@ public class StoreCenter { } /// Executes an API call - func execute(apiCalls: [ApiCall]) async throws -> [T] { + public func execute(apiCalls: [ApiCall]) async throws -> [T] { return try await self.service().runApiCalls(apiCalls) } @@ -773,6 +773,13 @@ public class StoreCenter { public func apiCallsFileContent(resourceName: String) async -> String { return await self._apiCallCollections[resourceName]?.contentOfFile() ?? "" } + + public func apiCalls(type: T.Type) async -> [ApiCall] { + if let apiCallCollection: ApiCallCollection = try? self.apiCallCollection() { + return await apiCallCollection.apiCalls() + } + return [] + } /// This method triggers the framework to save and send failed api calls public func logsFailedAPICalls() { diff --git a/LeStorage/StoredCollection.swift b/LeStorage/StoredCollection.swift index a0ceb3b..de83721 100644 --- a/LeStorage/StoredCollection.swift +++ b/LeStorage/StoredCollection.swift @@ -18,9 +18,11 @@ protocol SomeCollection: CollectionHolder, Identifiable { var resourceName: String { get } var hasLoaded: Bool { get } var inMemory: Bool { get } - + var type: any Storable.Type { get } + func allItems() -> [any Storable] func referenceCount(type: S.Type, id: String) -> Int + } protocol SomeSyncedCollection: SomeCollection { @@ -350,6 +352,8 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti self.store.removeFile(type: T.self) } + var type: any Storable.Type { return T.self } + // MARK: - Reference count /// Counts the references to an object - given its type and id - inside the collection