From 711827ba80f35f0b734f84392cb09ccc5ac01e16 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 1 May 2025 12:18:39 +0200 Subject: [PATCH] compilation requirements --- LeStorage/Services.swift | 22 +++++++++++++++++++++- LeStorage/StoredSingleton.swift | 8 ++++++++ LeStorage/Utils/Codable+Extensions.swift | 6 +++--- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/LeStorage/Services.swift b/LeStorage/Services.swift index 86879a1..77c96b0 100644 --- a/LeStorage/Services.swift +++ b/LeStorage/Services.swift @@ -241,7 +241,7 @@ public class Services { /// - method: the HTTP method to execute /// - requiresToken: An optional boolean to indicate if the token is required /// - identifier: an optional StoreIdentifier that allows to filter GET requests with the StoreIdentifier values - fileprivate func _baseRequest( + public func _baseRequest( servicePath: String, method: HTTPMethod, requiresToken: Bool? = nil, identifier: String? = nil, getArguments: [String : String]? = nil ) throws -> URLRequest { @@ -499,6 +499,26 @@ public class Services { return try await self._runRequest(getRequest) } + /// Executes a POST request on the generated DRF services corresponding to T + public func rawPost(_ instance: T) async throws -> T { + var postRequest = try self._postRequest(type: T.self) + postRequest.httpBody = try JSON.encoder.encode(instance) + return try await self._runRequest(postRequest) + } + + /// Executes a PUT request on the generated DRF services corresponding to T + public func rawPut(_ instance: T) async throws -> T { + var postRequest = try self._putRequest(type: T.self, id: instance.stringId) + postRequest.httpBody = try JSON.encoder.encode(instance) + return try await self._runRequest(postRequest) + } + + /// Executes a DELETE request on the generated DRF services corresponding to T + public func rawDelete(_ instance: T) async throws -> T { + let deleteRequest = try self._deleteRequest(type: T.self, id: instance.stringId) + return try await self._runRequest(deleteRequest) + } + /// Executes a POST request public func post(_ instance: T) async throws -> T { diff --git a/LeStorage/StoredSingleton.swift b/LeStorage/StoredSingleton.swift index 89f439d..2f59e94 100644 --- a/LeStorage/StoredSingleton.swift +++ b/LeStorage/StoredSingleton.swift @@ -27,6 +27,14 @@ public class StoredSingleton: SyncedCollection { return self.items.first } + public func tryPutBeforeUpdating(_ instance: T) async throws { + let result = try await StoreCenter.main.service().rawPut(instance) + if let item = self.item() { + item.copy(from: result) + self.addOrUpdate(instance: item) + } + } + // MARK: - Protects from use public override func addOrUpdate(contentOfs sequence: any Sequence) { diff --git a/LeStorage/Utils/Codable+Extensions.swift b/LeStorage/Utils/Codable+Extensions.swift index 5fd14e7..5294b6b 100644 --- a/LeStorage/Utils/Codable+Extensions.swift +++ b/LeStorage/Utils/Codable+Extensions.swift @@ -7,9 +7,9 @@ import Foundation -class JSON { +public class JSON { - static var encoder: JSONEncoder = { + public static var encoder: JSONEncoder = { let encoder = JSONEncoder() encoder.keyEncodingStrategy = .convertToSnakeCase #if DEBUG @@ -23,7 +23,7 @@ class JSON { return encoder }() - static var decoder: JSONDecoder = { + public static var decoder: JSONDecoder = { let decoder = JSONDecoder() decoder.keyDecodingStrategy = .convertFromSnakeCase decoder.dateDecodingStrategy = .custom { decoder in