expose APICall for external use

sync2
Laurent 8 months ago
parent 7125d868ae
commit 89582de30c
  1. 4
      LeStorage/ApiCallCollection.swift
  2. 34
      LeStorage/Codables/ApiCall.swift
  3. 4
      LeStorage/Store.swift
  4. 9
      LeStorage/StoreCenter.swift
  5. 4
      LeStorage/StoredCollection.swift

@ -357,4 +357,8 @@ actor ApiCallCollection<T: SyncedStorable>: SomeCallCollection {
return self.items.isNotEmpty return self.items.isNotEmpty
} }
func apiCalls() -> [ApiCall<T>] {
return self.items
}
} }

@ -7,18 +7,22 @@
import Foundation import Foundation
protocol SomeCall: Storable { public protocol SomeCall: Identifiable, Storable {
var id: String { get } var id: String { get }
var lastAttemptDate: Date { get } var lastAttemptDate: Date { get }
var attemptsCount: Int { get } var attemptsCount: Int { get }
var method: HTTPMethod { get }
var dataId: String? { get }
var dataContent: String? { get }
} }
class ApiCall<T: Storable>: ModelObject, Storable, SomeCall { public class ApiCall<T: Storable>: 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 [] } static func tokenExemptedMethods() -> [HTTPMethod] { return [] }
var id: String = Store.randomId() public var id: String = Store.randomId()
/// The transactionId to group calls together /// The transactionId to group calls together
var transactionId: String = Store.randomId() var transactionId: String = Store.randomId()
@ -27,16 +31,16 @@ class ApiCall<T: Storable>: ModelObject, Storable, SomeCall {
var creationDate: Date? = Date() var creationDate: Date? = Date()
/// The HTTP method of the call /// The HTTP method of the call
var method: HTTPMethod public var method: HTTPMethod
/// The content of the call /// The content of the call
var data: T? var data: T?
/// The number of times the call has been executed /// The number of times the call has been executed
var attemptsCount: Int = 0 public var attemptsCount: Int = 0
/// The date of the last execution /// 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" /// The parameters to add in the URL to obtain : "?p1=v1&p2=v2"
var urlParameters: [String : String]? = nil var urlParameters: [String : String]? = nil
@ -49,7 +53,7 @@ class ApiCall<T: Storable>: ModelObject, Storable, SomeCall {
} }
} }
func copy(from other: any Storable) { public func copy(from other: any Storable) {
fatalError("should not happen") fatalError("should not happen")
} }
@ -72,9 +76,19 @@ class ApiCall<T: Storable>: 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<T: Storable>: ModelObject, Storable, SomeCall { class OldApiCall<T: Storable>: ModelObject, Storable, SomeCall {
@ -148,6 +162,8 @@ class OldApiCall<T: Storable>: ModelObject, Storable, SomeCall {
} }
} }
var dataContent: String? { return self.body }
static func relationships() -> [Relationship] { return [] } static func relationships() -> [Relationship] { return [] }
func toNewApiCall() -> ApiCall<T>? { func toNewApiCall() -> ApiCall<T>? {

@ -192,8 +192,8 @@ final public class Store {
} }
/// Returns the names of all collections /// Returns the names of all collections
public func collectionNames() -> [String] { public func collectionNames() -> [(String, any Storable.Type)] {
return self._collections.values.map { $0.resourceName } return self._collections.values.map { ($0.resourceName, $0.type) }
} }
// MARK: - Synchronization // MARK: - Synchronization

@ -388,7 +388,7 @@ public class StoreCenter {
} }
/// Executes an API call /// Executes an API call
func execute<T: SyncedStorable>(apiCalls: [ApiCall<T>]) async throws -> [T] { public func execute<T: SyncedStorable>(apiCalls: [ApiCall<T>]) async throws -> [T] {
return try await self.service().runApiCalls(apiCalls) return try await self.service().runApiCalls(apiCalls)
} }
@ -774,6 +774,13 @@ public class StoreCenter {
return await self._apiCallCollections[resourceName]?.contentOfFile() ?? "" return await self._apiCallCollections[resourceName]?.contentOfFile() ?? ""
} }
public func apiCalls<T: SyncedStorable>(type: T.Type) async -> [ApiCall<T>] {
if let apiCallCollection: ApiCallCollection<T> = try? self.apiCallCollection() {
return await apiCallCollection.apiCalls()
}
return []
}
/// This method triggers the framework to save and send failed api calls /// This method triggers the framework to save and send failed api calls
public func logsFailedAPICalls() { public func logsFailedAPICalls() {
self._failedAPICallsCollection = Store.main.registerCollection(limit: 50) self._failedAPICallsCollection = Store.main.registerCollection(limit: 50)

@ -18,9 +18,11 @@ protocol SomeCollection: CollectionHolder, Identifiable {
var resourceName: String { get } var resourceName: String { get }
var hasLoaded: Bool { get } var hasLoaded: Bool { get }
var inMemory: Bool { get } var inMemory: Bool { get }
var type: any Storable.Type { get }
func allItems() -> [any Storable] func allItems() -> [any Storable]
func referenceCount<S: Storable>(type: S.Type, id: String) -> Int func referenceCount<S: Storable>(type: S.Type, id: String) -> Int
} }
protocol SomeSyncedCollection: SomeCollection { protocol SomeSyncedCollection: SomeCollection {
@ -350,6 +352,8 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
self.store.removeFile(type: T.self) self.store.removeFile(type: T.self)
} }
var type: any Storable.Type { return T.self }
// MARK: - Reference count // MARK: - Reference count
/// Counts the references to an object - given its type and id - inside the collection /// Counts the references to an object - given its type and id - inside the collection

Loading…
Cancel
Save