subscriptions work in progress

multistore
Laurent 2 years ago
parent e5531d0150
commit de3dc84ba7
  1. 30
      LeStorage/Services.swift
  2. 27
      LeStorage/Store.swift
  3. 4
      LeStorage/StoredCollection.swift

@ -120,7 +120,11 @@ public class Services {
return try jsonDecoder.decode(T.self, from: task.0)
}
fileprivate func getRequest(servicePath: String) throws -> URLRequest {
fileprivate func _postRequest(servicePath: String) throws -> URLRequest {
return try self._baseRequest(servicePath: servicePath, method: .post)
}
fileprivate func _getRequest(servicePath: String) throws -> URLRequest {
return try self._baseRequest(servicePath: servicePath, method: .get)
}
@ -146,11 +150,17 @@ public class Services {
// MARK: - Services
func get<T: Storable>() async throws -> [T] {
let getRequest = try getRequest(servicePath: T.resourceName() + "/")
public func get<T: Storable>() async throws -> [T] {
let getRequest = try _getRequest(servicePath: T.resourceName() + "/")
return try await self._runRequest(getRequest)
}
public func post<T: Storable>(_ instance: T) async throws -> T {
var postRequest = try self._postRequest(servicePath: T.resourceName())
postRequest.httpBody = try jsonEncoder.encode(instance)
return try await self._runRequest(postRequest)
}
func runApiCall<T: Storable>(_ apiCall: ApiCall<T>) async throws -> T {
let request = try self._request(from: apiCall)
return try await self._runRequest(request, apiCallId: apiCall.id)
@ -244,6 +254,20 @@ public class Services {
try self.keychainStore.deleteToken()
}
// public func addPurchase(_ purchase: Purchase) async throws {
// var postRequest = try self._postRequest(servicePath: Purchase.resourceName())
// postRequest.httpBody = try jsonEncoder.encode(purchase)
// let purchase: Purchase = try await self._runRequest(postRequest)
// if var purchases: [Purchase] = UserDefaults(suiteName: "le.storage")?.array(forKey: Purchase.resourceName()) as? [Purchase] {
// purchases.append(purchase)
// UserDefaults(suiteName: "le.storage")?.setValue(purchases, forKey: Purchase.resourceName())
// }
// }
//
// var purchases: [Purchase] {
// return UserDefaults(suiteName: "le.storage")?.array(forKey: Purchase.resourceName()) as? [Purchase] ?? []
// }
}
struct AuthResponse: Codable {

@ -41,8 +41,16 @@ public class Store {
fileprivate var _services: Services?
/// The service instance
public var service: Services? {
return self._services
// public var service: Services? {
// return self._services
// }
public func service() throws -> Services {
if let service = self._services {
return service
} else {
throw StoreError.missingService
}
}
/// The dictionary of registered StoredCollections
@ -109,14 +117,13 @@ public class Store {
}
public func disconnect() {
try? self.service?.disconnect()
try? self.service().disconnect()
self.settingsStorage.item.userUUIDString = nil
}
public func hasToken() -> Bool {
guard let service else { return false }
do {
_ = try service.keychainStore.getToken()
_ = try self.service().keychainStore.getToken()
return true
} catch {
return false
@ -215,10 +222,7 @@ public class Store {
/// Executes an ApiCall
fileprivate func _executeApiCall<T: Storable>(_ apiCall: ApiCall<T>) async throws -> T {
guard let service else {
throw StoreError.missingService
}
return try await service.runApiCall(apiCall)
return try await self.service().runApiCall(apiCall)
}
/// Executes an ApiCall
@ -232,10 +236,7 @@ public class Store {
/// Retrieves all the items on the server
func getItems<T: Storable>() async throws -> [T] {
guard let service else {
throw StoreError.missingService
}
return try await service.get()
return try await self.service().get()
}
public func reset() {

@ -301,9 +301,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
}
fileprivate func _createCall(_ instance: T, method: Method) throws -> ApiCall<T> {
guard let baseURL = _store.service?.baseURL else {
throw StoreError.missingService
}
let baseURL = try _store.service().baseURL
let jsonString = try instance.jsonString()
var url = baseURL + T.resourceName() + "/"
if method == .put || method == .delete {

Loading…
Cancel
Save