This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
@testable import RealmDB_Manager | |
@testable import RealmSwift | |
@testable import Realm | |
class RealmDB_ManagerTests: XCTestCase { | |
struct service: LocalFileReader { } | |
struct db: Persister { } | |
var things = [Thing]() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Persister { | |
// MARK: - Read Methods | |
static func select<T: CodableObject>(type: T.Type, filter: NSPredicate? = nil) -> Results<T> { | |
do { | |
let realm = try Realm() | |
if let predicate = filter { | |
return realm.objects(T.self).filter(predicate) | |
} else { | |
return realm.objects(T.self) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Persister { | |
static func select<T: CodableObject>(type: T.Type, filter: NSPredicate?) -> Results<T> | |
static func insert(object: CodableObject, update: Bool?) | |
static func insert(objects: [CodableObject], update: Bool?) | |
static func delete(object: CodableObject) | |
static func delete<T: CodableObject>(type: T.Type, filter: NSPredicate?) | |
static func deleteAll<T: CodableObject>(type: T.Type) | |
static func resetDB() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct service: Requester {} | |
func doSomething() { | |
// we are going to call this test api | |
let request = HTTPRequest(url: "http://ip.jsontest.com/") | |
service.execute(ofType: ResponseMapper.self, request: request) { (result) in | |
switch result { | |
case .failure(let error): | |
print(error) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Requester { | |
public static func execute<T: Codable>(ofType: T.Type, request : Request, | |
completion: @escaping | |
(_ result: HTTPResult<T?,HTTPCallErrorType<Error>?>) -> Void) { | |
// first we create the URLRequest | |
if let httpRequest = createRequest(from: request) { | |
// we use the nativ URL session to exute it | |
URLSession.shared.dataTask(with: httpRequest) { (data, response, error) in | |
if let error = error { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public protocol Requester { | |
static func execute<T: Codable>(ofType: T.Type, request : Request, | |
completion: @escaping (_ result: HTTPResult<T?,HTTPCallErrorType<Error>?>) -> Void) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum HTTPResult<U,V> { | |
case success(U) | |
case failure(V) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum HTTPMethod: String { | |
case get, post, put, patch, delete | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum HTTPCallErrorType<E> { | |
case urlError | |
case dataError | |
case responseError(E) | |
case mappingError(E) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public typealias Headers = [String: String] | |
public typealias Params = [String: String] | |
public typealias BodyParams = [String: Any] | |
public protocol Request { | |
var urlString : String? { get } | |
var method : HTTPMethod { get } | |
var bodyParams : BodyParams? { get } |