|
|
|
|
@ -220,64 +220,53 @@ actor ApiCallCollection<T: Storable>: SomeCallCollection { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Sends an insert api call for the provided [instance] |
|
|
|
|
func sendInsertion(_ instance: T) { |
|
|
|
|
Task { |
|
|
|
|
do { |
|
|
|
|
try await self._synchronize(instance, method: HTTPMethod.post) |
|
|
|
|
} catch { |
|
|
|
|
self.rescheduleApiCallsIfNecessary() |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
func sendInsertion(_ instance: T) async throws -> T? { |
|
|
|
|
do { |
|
|
|
|
return try await self._synchronize(instance, method: HTTPMethod.post) |
|
|
|
|
} catch { |
|
|
|
|
self.rescheduleApiCallsIfNecessary() |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
return nil |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Sends an update api call for the provided [instance] |
|
|
|
|
func sendUpdate(_ instance: T) { |
|
|
|
|
Task { |
|
|
|
|
do { |
|
|
|
|
try await self._synchronize(instance, method: HTTPMethod.put) |
|
|
|
|
} catch { |
|
|
|
|
self.rescheduleApiCallsIfNecessary() |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
func sendUpdate(_ instance: T) async throws -> T? { |
|
|
|
|
do { |
|
|
|
|
return try await self._synchronize(instance, method: HTTPMethod.put) |
|
|
|
|
} catch { |
|
|
|
|
self.rescheduleApiCallsIfNecessary() |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Sends an delete api call for the provided [instance] |
|
|
|
|
func sendDeletion(_ instance: T) { |
|
|
|
|
Task { |
|
|
|
|
do { |
|
|
|
|
try await self._synchronize(instance, method: HTTPMethod.delete) |
|
|
|
|
} catch { |
|
|
|
|
self.rescheduleApiCallsIfNecessary() |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
func sendDeletion(_ instance: T) async throws -> T? { |
|
|
|
|
do { |
|
|
|
|
return try await self._synchronize(instance, method: HTTPMethod.delete) |
|
|
|
|
} catch { |
|
|
|
|
self.rescheduleApiCallsIfNecessary() |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Initiates the process of sending the data with the server |
|
|
|
|
fileprivate func _synchronize(_ instance: T, method: HTTPMethod) async throws { |
|
|
|
|
fileprivate func _synchronize(_ instance: T, method: HTTPMethod) async throws -> T? { |
|
|
|
|
if let apiCall = try self._callForInstance(instance, method: method) { |
|
|
|
|
try self._prepareCall(apiCall: apiCall) |
|
|
|
|
try await self._executeApiCall(apiCall) |
|
|
|
|
return try await self._executeApiCall(apiCall) |
|
|
|
|
} else { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Executes an API call |
|
|
|
|
/// For POST requests, potentially copies additional data coming from the server during the insert |
|
|
|
|
fileprivate func _executeApiCall(_ apiCall: ApiCall<T>) async throws { |
|
|
|
|
let result = try await StoreCenter.main.execute(apiCall: apiCall) |
|
|
|
|
switch apiCall.method { |
|
|
|
|
case .post: |
|
|
|
|
if let instance = self.findById(result.stringId) { |
|
|
|
|
self._hasChanged = instance.copyFromServerInstance(result) |
|
|
|
|
} |
|
|
|
|
default: |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
// Logger.log("") |
|
|
|
|
fileprivate func _executeApiCall(_ apiCall: ApiCall<T>) async throws -> T { |
|
|
|
|
return try await StoreCenter.main.execute(apiCall: apiCall) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Returns the content of the API call file as a String |
|
|
|
|
|