expose APICall for external use

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

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

@ -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<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 [] }
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<T: Storable>: 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<T: Storable>: 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<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 {
static func resourceName() -> String { return "apicalls_" + T.resourceName() }
@ -148,6 +162,8 @@ class OldApiCall<T: Storable>: ModelObject, Storable, SomeCall {
}
}
var dataContent: String? { return self.body }
static func relationships() -> [Relationship] { return [] }
func toNewApiCall() -> ApiCall<T>? {

@ -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

@ -388,7 +388,7 @@ public class StoreCenter {
}
/// 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)
}
@ -773,6 +773,13 @@ public class StoreCenter {
public func apiCallsFileContent(resourceName: String) async -> String {
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
public func logsFailedAPICalls() {

@ -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<S: Storable>(type: S.Type, id: String) -> Int
}
protocol SomeSyncedCollection: SomeCollection {
@ -350,6 +352,8 @@ public class StoredCollection<T: Storable>: 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

Loading…
Cancel
Save