Merge pull request #3 from shial4/Release-0.1.2

Release 0.1.2
This commit is contained in:
Shial 2018-01-08 11:01:59 +11:00 committed by GitHub
commit e7ea34e0ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 340 additions and 133 deletions

View File

@ -26,6 +26,7 @@ SLazeKit is an easy to use Swift restful collection of extensions and classes. D
<img src="Content/SLazeKit.png"> <img src="Content/SLazeKit.png">
</p> </p>
- This request will use default configuration scheme.
- The type `ResponseModel` is a result object type for result callback argument. If you want to decode array you simple execute method on `[ResponseModel]` type. - The type `ResponseModel` is a result object type for result callback argument. If you want to decode array you simple execute method on `[ResponseModel]` type.
- The `error` indicates why the request failed, or nil if the request was successful. it include `EntityMaping` errors, url preparation error and so on. If a response from the server is received, regardless of whether the request completes successfully or fails, the response parameter contains that information. - The `error` indicates why the request failed, or nil if the request was successful. it include `EntityMaping` errors, url preparation error and so on. If a response from the server is received, regardless of whether the request completes successfully or fails, the response parameter contains that information.
- The `response` provides tuple with HTTPURLResponse object and the data returned by the server. - The `response` provides tuple with HTTPURLResponse object and the data returned by the server.
@ -57,7 +58,14 @@ Simple
Advance Advance
[Model.swift](Tests/SLazeKitTests/Models/Model.swift) [Model.swift](Tests/SLazeKitTests/Models/Model.swift)
In case of any other configuration you can use `Laze`
```swift
//Laze<C: LazeConfiguration, D: Decodable>
Laze<Default, ResponseModel>.get( // Default configuration, result in ResponseModel model
SLazeKit<Default>.get( // Default configuration, no result model
ResponseModel.get( // Default configuration, result in ResponseModel model
```
## 🔧 Installation ## 🔧 Installation
@ -84,25 +92,17 @@ This project demonstrates a working method for using Swift Package Manager (SPM)
## 💊 Usage ## 💊 Usage
For positive experience, you should configure `SLazeKit` at first. This step is REQUIRED! For positive experience, you should configure `SLazeKit` at first.
SLazeKit in default configuration uses `Default` class which conforms to `LazeConfiguration` protocol.
I do recommand extend `Default` configuration. Moreover , If you have more then one API in your project. You can create as many as needed configurations schemes by creating your own object which do conform to `LazeConfiguration` protocol.
```swift ```swift
import SLazeKit import SLazeKit
extension SLazeKit { extension Default {
/// Required override of this method which will provide Context for bacground execution.
///
/// - Returns: NSManagedObjectContext
open class func newBackgroundContext() -> NSManagedObjectContext? { return nil }
}
```
This step is optional. You may leave it as it is default.
```swift
import SLazeKit
extension SLazeKit {
//Provide base path for your API requests. //Provide base path for your API requests.
open class var basePath?: String { return "www.yourdomain.com" } open class var basePath: String? { return "www.yourdomain.com" }
//Additional you can set port for your requests. //Additional you can set port for your requests.
open class var basePort: Int? { return 8040 } open class var basePort: Int? { return 8040 }
//You can provide your own instance of JSONDecoder. //You can provide your own instance of JSONDecoder.
@ -123,6 +123,11 @@ extension SLazeKit {
Client.logout() Client.logout()
} }
} }
/// Required override of this method which will provide Context for bacground execution.
///
/// - Returns: NSManagedObjectContext
open class func newBackgroundContext() -> NSManagedObjectContext? { return nil }
} }
``` ```

View File

@ -1,6 +1,6 @@
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = 'SLazeKit' s.name = 'SLazeKit'
s.version = '0.1.1' s.version = '0.1.2'
s.summary = 'Swift restful manager.' s.summary = 'Swift restful manager.'
s.description = <<-DESC s.description = <<-DESC
SLazeKit is an easy to use Swift restfull collection of extensions and classes. Don't spend hours writing your code to map your rest api request into models and serialization. stop wasting your time! SLazeKit is an easy to use Swift restfull collection of extensions and classes. Don't spend hours writing your code to map your rest api request into models and serialization. stop wasting your time!

View File

@ -21,6 +21,8 @@
/* End PBXAggregateTarget section */ /* End PBXAggregateTarget section */
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
623922782002D8C30018982A /* LazeConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 623922762002D8C30018982A /* LazeConfiguration.swift */; };
6239227E2002DF1A0018982A /* Laze.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6239227D2002DF1A0018982A /* Laze.swift */; };
625B23AD1FF3462B001976C3 /* Object.swift in Sources */ = {isa = PBXBuildFile; fileRef = 625B23AB1FF34625001976C3 /* Object.swift */; }; 625B23AD1FF3462B001976C3 /* Object.swift in Sources */ = {isa = PBXBuildFile; fileRef = 625B23AB1FF34625001976C3 /* Object.swift */; };
OBJ_32 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_6 /* Package.swift */; }; OBJ_32 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_6 /* Package.swift */; };
OBJ_38 /* Model.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_21 /* Model.swift */; }; OBJ_38 /* Model.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_21 /* Model.swift */; };
@ -54,6 +56,8 @@
/* End PBXContainerItemProxy section */ /* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
623922762002D8C30018982A /* LazeConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LazeConfiguration.swift; sourceTree = "<group>"; };
6239227D2002DF1A0018982A /* Laze.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Laze.swift; sourceTree = "<group>"; };
625B23AB1FF34625001976C3 /* Object.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Object.swift; sourceTree = "<group>"; }; 625B23AB1FF34625001976C3 /* Object.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Object.swift; sourceTree = "<group>"; };
OBJ_10 /* Decodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Decodable.swift; sourceTree = "<group>"; }; OBJ_10 /* Decodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Decodable.swift; sourceTree = "<group>"; };
OBJ_11 /* NSManagedObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSManagedObject.swift; sourceTree = "<group>"; }; OBJ_11 /* NSManagedObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSManagedObject.swift; sourceTree = "<group>"; };
@ -94,6 +98,7 @@
children = ( children = (
OBJ_15 /* EntityMapping.swift */, OBJ_15 /* EntityMapping.swift */,
OBJ_16 /* StringInitializable.swift */, OBJ_16 /* StringInitializable.swift */,
623922762002D8C30018982A /* LazeConfiguration.swift */,
); );
path = Protocols; path = Protocols;
sourceTree = "<group>"; sourceTree = "<group>";
@ -158,6 +163,7 @@
children = ( children = (
OBJ_9 /* Extensions */, OBJ_9 /* Extensions */,
OBJ_14 /* Protocols */, OBJ_14 /* Protocols */,
6239227D2002DF1A0018982A /* Laze.swift */,
OBJ_17 /* SLazeKit.swift */, OBJ_17 /* SLazeKit.swift */,
); );
name = SLazeKit; name = SLazeKit;
@ -279,10 +285,12 @@
files = ( files = (
OBJ_49 /* Decodable.swift in Sources */, OBJ_49 /* Decodable.swift in Sources */,
OBJ_50 /* NSManagedObject.swift in Sources */, OBJ_50 /* NSManagedObject.swift in Sources */,
6239227E2002DF1A0018982A /* Laze.swift in Sources */,
OBJ_51 /* NSManagedObjectContext.swift in Sources */, OBJ_51 /* NSManagedObjectContext.swift in Sources */,
OBJ_52 /* String.swift in Sources */, OBJ_52 /* String.swift in Sources */,
OBJ_53 /* EntityMapping.swift in Sources */, OBJ_53 /* EntityMapping.swift in Sources */,
OBJ_54 /* StringInitializable.swift in Sources */, OBJ_54 /* StringInitializable.swift in Sources */,
623922782002D8C30018982A /* LazeConfiguration.swift in Sources */,
OBJ_55 /* SLazeKit.swift in Sources */, OBJ_55 /* SLazeKit.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;

View File

@ -2,98 +2,98 @@ import Foundation
extension Decodable { extension Decodable {
public static func request(path: String, method: HTTPMethod, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func request(path: String, method: HTTPMethod, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
} }
public static func request<T: Encodable>(path: String, method: HTTPMethod, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func request<T: Encodable>(path: String, method: HTTPMethod, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
} }
public static func request<T: Encodable>(path: String, method: HTTPMethod, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func request<T: Encodable>(path: String, method: HTTPMethod, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
} }
public static func request(path: String, method: HTTPMethod, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func request(path: String, method: HTTPMethod, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: method, queryItems: queryItems, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: method, queryItems: queryItems, handler: handler)
} }
public static func get(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func get(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
} }
public static func get<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func get<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
} }
public static func get<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func get<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
} }
public static func get(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func get(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, queryItems: queryItems, handler: handler) return SLazeKit<Default>.networkTask(path: path, queryItems: queryItems, handler: handler)
} }
public static func post(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func post(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
} }
public static func post<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func post<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
} }
public static func post<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func post<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
} }
public static func post(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func post(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .POST, queryItems: queryItems, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: .POST, queryItems: queryItems, handler: handler)
} }
public static func put(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func put(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
} }
public static func put<T: Encodable>(path: String, queryItems: [URLQueryItem]? = nil, body: T, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func put<T: Encodable>(path: String, queryItems: [URLQueryItem]? = nil, body: T, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
} }
public static func put<T: Encodable>(path: String, queryItems: [URLQueryItem]? = nil, body: [T], handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func put<T: Encodable>(path: String, queryItems: [URLQueryItem]? = nil, body: [T], handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
} }
public static func put(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func put(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .PUT, queryItems: queryItems, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: .PUT, queryItems: queryItems, handler: handler)
} }
public static func patch(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func patch(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler)
} }
public static func patch<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func patch<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler)
} }
public static func patch<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func patch<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler)
} }
public static func patch(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func patch(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .PATCH, queryItems: queryItems, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: .PATCH, queryItems: queryItems, handler: handler)
} }
public static func delete(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func delete(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
} }
public static func delete<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func delete<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
} }
public static func delete<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func delete<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
} }
public static func delete(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? { public static func delete(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .DELETE, queryItems: queryItems, handler: handler) return SLazeKit<Default>.networkTask(path: path, method: .DELETE, queryItems: queryItems, handler: handler)
} }
} }

View File

@ -3,7 +3,7 @@ import CoreData
extension NSManagedObject { extension NSManagedObject {
/// String entity name /// String entity name
public class var entityName: String? { final class var entityName: String? {
return NSStringFromClass(self).components(separatedBy: ".").last return NSStringFromClass(self).components(separatedBy: ".").last
} }
/// Finds `NSManagedObject` by EntityAttribute parameters. /// Finds `NSManagedObject` by EntityAttribute parameters.
@ -12,7 +12,7 @@ extension NSManagedObject {
/// - context: Context on which fetch should be executed /// - context: Context on which fetch should be executed
/// - attributes: Entity attribute mapped by key - value /// - attributes: Entity attribute mapped by key - value
/// - Returns: Returns first object that meet the criteria specified by a given fetch request. /// - Returns: Returns first object that meet the criteria specified by a given fetch request.
public class func find(_ context: NSManagedObjectContext?, by attributes: EntityAttribute...) throws -> NSManagedObject? { final class func find(_ context: NSManagedObjectContext?, by attributes: EntityAttribute...) throws -> NSManagedObject? {
guard let name = entityName else { return nil } guard let name = entityName else { return nil }
let fetchRequest: NSFetchRequest<NSManagedObject> = NSFetchRequest<NSManagedObject>(entityName: name) let fetchRequest: NSFetchRequest<NSManagedObject> = NSFetchRequest<NSManagedObject>(entityName: name)
var predicates: [NSPredicate] = [] var predicates: [NSPredicate] = []
@ -28,7 +28,7 @@ extension NSManagedObject {
/// - context: Context on which fetch should be executed /// - context: Context on which fetch should be executed
/// - attributes: Entity attribute mapped by key - value /// - attributes: Entity attribute mapped by key - value
/// - Returns: Returns first object that meet the criteria specified by a given fetch request. /// - Returns: Returns first object that meet the criteria specified by a given fetch request.
public class func find(_ context: NSManagedObjectContext?, by attributes: [EntityAttribute]) throws -> NSManagedObject? { final class func find(_ context: NSManagedObjectContext?, by attributes: [EntityAttribute]) throws -> NSManagedObject? {
guard let name = entityName else { return nil } guard let name = entityName else { return nil }
let fetchRequest: NSFetchRequest<NSManagedObject> = NSFetchRequest<NSManagedObject>(entityName: name) let fetchRequest: NSFetchRequest<NSManagedObject> = NSFetchRequest<NSManagedObject>(entityName: name)
var predicates: [NSPredicate] = [] var predicates: [NSPredicate] = []

View File

@ -0,0 +1,99 @@
import Foundation
public class Laze<C: LazeConfiguration, D: Decodable> {
public static func request(path: String, method: HTTPMethod, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
}
public static func request<T: Encodable>(path: String, method: HTTPMethod, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
}
public static func request<T: Encodable>(path: String, method: HTTPMethod, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
}
public static func request(path: String, method: HTTPMethod, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: method, queryItems: queryItems, handler: handler)
}
public static func get(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
}
public static func get<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
}
public static func get<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
}
public static func get(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, queryItems: queryItems, handler: handler)
}
public static func post(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
}
public static func post<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
}
public static func post<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
}
public static func post(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .POST, queryItems: queryItems, handler: handler)
}
public static func put(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
}
public static func put<T: Encodable>(path: String, queryItems: [URLQueryItem]? = nil, body: T, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
}
public static func put<T: Encodable>(path: String, queryItems: [URLQueryItem]? = nil, body: [T], handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
}
public static func put(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .PUT, queryItems: queryItems, handler: handler)
}
public static func patch(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler)
}
public static func patch<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler)
}
public static func patch<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler)
}
public static func patch(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .PATCH, queryItems: queryItems, handler: handler)
}
public static func delete(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
}
public static func delete<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
}
public static func delete<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
}
public static func delete(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: D?, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .DELETE, queryItems: queryItems, handler: handler)
}
}

View File

@ -12,8 +12,7 @@ public protocol EntityMapping {
} }
extension EntityMapping { extension EntityMapping {
func map() throws -> NSManagedObject? { func map(_ context: NSManagedObjectContext) throws -> NSManagedObject? {
guard let context = SLazeKit.newBackgroundContext() else { return nil }
var model: NSManagedObject? = nil var model: NSManagedObject? = nil
var mapError: Error? var mapError: Error?
context.performAndWait { context.performAndWait {
@ -26,7 +25,6 @@ extension EntityMapping {
if let object = model { if let object = model {
fillObject(with: object) fillObject(with: object)
} }
context.commit()
} catch { } catch {
mapError = error mapError = error
} }

View File

@ -0,0 +1,45 @@
//
// LazeConfiguration.swift
// SLazeKit
//
// Created by Shial on 8/1/18.
//
import Foundation
import CoreData
/// LazeConfiguration protocol represents API configuration
public protocol LazeConfiguration {
/// Provide base path for your API requests.
static var basePath: String? { get }
/// Additional you can set port for your requests.
static var basePort: Int? { get }
/// Optional provider for JSONDecoder instance.
static var decoder: JSONDecoder { get }
/// Optional provider for JSONDecoder instance.
static var urlSession: URLSession { get }
/// Global outgoing `URLRequest` customization. Called everytime request is created before executed.
///
/// - Parameter request: `URLRequest` object to setup
/// - Returns: already setup and customize URLRequest object
static func setup(_ request: URLRequest) -> URLRequest
static func handle(_ response: HTTPURLResponse?)
/// Required override of this method which will provide Context for bacground execution.
///
/// - Returns: NSManagedObjectContext
static func newBackgroundContext() -> NSManagedObjectContext?
}
extension LazeConfiguration {
public static var basePath: String? { return nil }
public static var basePort: Int? { return nil }
public static var decoder: JSONDecoder { return JSONDecoder() }
public static var urlSession: URLSession { return URLSession.shared }
public static func setup(_ request: URLRequest) -> URLRequest { return request }
public static func handle(_ response: HTTPURLResponse?) {}
public static func newBackgroundContext() -> NSManagedObjectContext? { return nil }
}

View File

@ -12,32 +12,14 @@ public enum HTTPMethod {
case GET, POST, PUT, PATCH, DELETE, COPY, HEAD, OPTIONS, LINK, UNLINK, PURGE, LOCK, UNLOCK, PROPFIND, VIEW case GET, POST, PUT, PATCH, DELETE, COPY, HEAD, OPTIONS, LINK, UNLINK, PURGE, LOCK, UNLOCK, PROPFIND, VIEW
} }
/// Default laze configuration
open class Default: LazeConfiguration {}
/// SLazeKit is an easy to use restful collection of extensions and classes. Maps your rest api request into models and provides coredata serialization. /// SLazeKit is an easy to use restful collection of extensions and classes. Maps your rest api request into models and provides coredata serialization.
public class SLazeKit { public class SLazeKit<Config: LazeConfiguration> {
/// Provide base path for your API requests.
open class var basePath: String? { return nil }
/// Additional you can set port for your requests.
open class var basePort: Int? { return nil }
/// Optional provider for JSONDecoder instance.
open class var decoder: JSONDecoder { return JSONDecoder() }
/// Optional provider for JSONDecoder instance.
open class var urlSession: URLSession { return URLSession.shared }
/// Global outgoing `URLRequest` customization. Called everytime request is created before executed.
///
/// - Parameter request: `URLRequest` object to setup
/// - Returns: already setup and customize URLRequest object
open class func setup(_ request: URLRequest) -> URLRequest { return request }
open class func handle(_ response: HTTPURLResponse?) {}
/// Required override of this method which will provide Context for bacground execution.
///
/// - Returns: NSManagedObjectContext
open class func newBackgroundContext() -> NSManagedObjectContext? { return nil }
class func networkTask(request: URLRequest, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { class func networkTask(request: URLRequest, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
let task = urlSession.dataTask(with: setup(request)) { (data, response, error) in let task = Config.urlSession.dataTask(with: Config.setup(request)) { (data, response, error) in
handle(response as? HTTPURLResponse) Config.handle(response as? HTTPURLResponse)
handler((data, response as? HTTPURLResponse), error) handler((data, response as? HTTPURLResponse), error)
} }
task.resume() task.resume()
@ -45,11 +27,11 @@ public class SLazeKit {
} }
class func networkTask<T: Decodable>(request: URLRequest, handler: @escaping (_ response: NetworkResponse, _ result: T?, _ error: Error?) -> ()) -> URLSessionDataTask? { class func networkTask<T: Decodable>(request: URLRequest, handler: @escaping (_ response: NetworkResponse, _ result: T?, _ error: Error?) -> ()) -> URLSessionDataTask? {
let task = urlSession.dataTask(with: setup(request)) { (data, response, error) in let task = Config.urlSession.dataTask(with: Config.setup(request)) { (data, response, error) in
handle(response as? HTTPURLResponse) Config.handle(response as? HTTPURLResponse)
if let data = data, error == nil { if let data = data, error == nil {
do { do {
let object = try decoder.decode(T.self, from: data) let object = try Config.decoder.decode(T.self, from: data)
try synchronize(object) try synchronize(object)
handler((data, response as? HTTPURLResponse), object, nil) handler((data, response as? HTTPURLResponse), object, nil)
} catch { } catch {
@ -179,116 +161,118 @@ public class SLazeKit {
} }
private class func components(for path: String, queryItems: [URLQueryItem]?) -> URLComponents? { private class func components(for path: String, queryItems: [URLQueryItem]?) -> URLComponents? {
var urlComponents = URLComponents(string: (basePath ?? "") + path) var urlComponents = URLComponents(string: (Config.basePath ?? "") + path)
urlComponents?.port = basePort urlComponents?.port = Config.basePort
urlComponents?.queryItems = queryItems urlComponents?.queryItems = queryItems
return urlComponents return urlComponents
} }
private class func synchronize(_ obj: Any) throws { private class func synchronize(_ obj: Any) throws {
guard let context = Config.newBackgroundContext() else { return }
if let array = obj as? [EntityMapping] { if let array = obj as? [EntityMapping] {
array.forEach({_ = try? $0.map()}) array.forEach({_ = try? $0.map(context)})
} else { } else {
guard let mapper = obj as? EntityMapping else { return } guard let mapper = obj as? EntityMapping else { return }
_ = try mapper.map() _ = try mapper.map(context)
} }
context.commit()
} }
} }
extension SLazeKit { extension SLazeKit {
public class func request(path: String, method: HTTPMethod, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func request(path: String, method: HTTPMethod, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
} }
public class func request<T: Encodable>(path: String, method: HTTPMethod, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func request<T: Encodable>(path: String, method: HTTPMethod, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
} }
public class func request<T: Encodable>(path: String, method: HTTPMethod, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func request<T: Encodable>(path: String, method: HTTPMethod, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
} }
public class func request(path: String, method: HTTPMethod, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func request(path: String, method: HTTPMethod, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: method, queryItems: queryItems, handler: handler) return networkTask(path: path, method: method, queryItems: queryItems, handler: handler)
} }
public class func get(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func get(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
} }
public class func get<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func get<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
} }
public class func get<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func get<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
} }
public class func get(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func get(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, queryItems: queryItems, handler: handler) return networkTask(path: path, queryItems: queryItems, handler: handler)
} }
public class func post(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func post(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
} }
public class func post<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func post<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
} }
public class func post<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func post<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
} }
public class func post(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func post(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .POST, queryItems: queryItems, handler: handler) return networkTask(path: path, method: .POST, queryItems: queryItems, handler: handler)
} }
public class func put(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func put(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
} }
public class func put<T: Encodable>(path: String, queryItems: [URLQueryItem]? = nil, body: T, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func put<T: Encodable>(path: String, queryItems: [URLQueryItem]? = nil, body: T, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
} }
public class func put<T: Encodable>(path: String, queryItems: [URLQueryItem]? = nil, body: [T], handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func put<T: Encodable>(path: String, queryItems: [URLQueryItem]? = nil, body: [T], handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
} }
public class func put(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func put(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .PUT, queryItems: queryItems, handler: handler) return networkTask(path: path, method: .PUT, queryItems: queryItems, handler: handler)
} }
public class func patch(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func patch(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler)
} }
public class func patch<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func patch<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler)
} }
public class func patch<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func patch<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler)
} }
public class func patch(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func patch(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .PATCH, queryItems: queryItems, handler: handler) return networkTask(path: path, method: .PATCH, queryItems: queryItems, handler: handler)
} }
public class func delete(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func delete(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
} }
public class func delete<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func delete<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
} }
public class func delete<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func delete<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler) return networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
} }
public class func delete(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? { final public class func delete(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> ()) -> URLSessionDataTask? {
return SLazeKit.networkTask(path: path, method: .DELETE, queryItems: queryItems, handler: handler) return networkTask(path: path, method: .DELETE, queryItems: queryItems, handler: handler)
} }
} }

View File

@ -61,7 +61,7 @@ class Model: NSManagedObject {
/// - model: CoreData model used to post request with body of ModelRequest /// - model: CoreData model used to post request with body of ModelRequest
class func create(model: Model, success: @escaping (() ->()), failure: ((_ statusCode: Int, _ error: Error?) ->())? = nil) { class func create(model: Model, success: @escaping (() ->()), failure: ((_ statusCode: Int, _ error: Error?) ->())? = nil) {
/// SLazeKit request are done i background. To handle response on main thread we need to dispatch it. /// SLazeKit request are done i background. To handle response on main thread we need to dispatch it.
let _ = SLazeKit.post(path: PathPattern.create.patternToPath(with: ["modelId":model.id]), body: ModelRequest(with: model)) { (response, error) in let _ = SLazeKit<Default>.post(path: PathPattern.create.patternToPath(with: ["modelId":model.id]), body: ModelRequest(with: model)) { (response, error) in
guard error == nil else { guard error == nil else {
DispatchQueue.main.async { failure?(response.urlResponse?.statusCode ?? -1,error) } DispatchQueue.main.async { failure?(response.urlResponse?.statusCode ?? -1,error) }
return return
@ -76,7 +76,7 @@ class Model: NSManagedObject {
/// ///
/// SLazeKit request are done i background. To handle response on main thread we need to dispatch it. /// SLazeKit request are done i background. To handle response on main thread we need to dispatch it.
class func remove(for modelId: String, success: @escaping (() ->()), failure: ((_ statusCode: Int, _ error: Error?) ->())? = nil) { class func remove(for modelId: String, success: @escaping (() ->()), failure: ((_ statusCode: Int, _ error: Error?) ->())? = nil) {
let _ = SLazeKit.delete(path: PathPattern.delete.patternToPath(with: ["modelId":modelId])) { (response, error) in let _ = SLazeKit<Default>.delete(path: PathPattern.delete.patternToPath(with: ["modelId":modelId])) { (response, error) in
guard error == nil else { guard error == nil else {
DispatchQueue.main.async { failure?(response.urlResponse?.statusCode ?? -1,error) } DispatchQueue.main.async { failure?(response.urlResponse?.statusCode ?? -1,error) }
return return
@ -98,7 +98,7 @@ class Model: NSManagedObject {
} }
var models: [Model]? = nil var models: [Model]? = nil
do { do {
models = try result?.serialized(SLazeKit.newBackgroundContext()) models = try result?.serialized(Default.newBackgroundContext())
} catch { } catch {
print(error) print(error)
} }
@ -118,7 +118,49 @@ class Model: NSManagedObject {
} }
var models: Model? = nil var models: Model? = nil
do { do {
models = try result?.serialized(SLazeKit.newBackgroundContext()) models = try result?.serialized(Default.newBackgroundContext())
} catch {
print(error)
}
success(models)
}
}
/// In this example API request will decode single object of type ModelResponse instead of an array.
///
///Result of type `ModelResponse` to return CoreData model we need to serialize it.
///
///`result?.serialized` will return `Model`
class func getFromOtherServer(for modelId: String, success: @escaping ((Model?) ->()), failure: (() ->())? = nil) {
class NoneDefault: LazeConfiguration {
static var basePath: String? { return "www.yourdomain.com" }
static var basePort: Int? { return 8765 }
static var decoder: JSONDecoder { return JSONDecoder() }
static var urlSession: URLSession { return URLSession.shared }
static func setup(_ request: URLRequest) -> URLRequest {
var request: URLRequest = request
request.setValue("Your token", forHTTPHeaderField: "X-Access-Token")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
return request
}
static func handle(_ response: HTTPURLResponse?) {
if response?.statusCode == 401 {
print("unauthorised")
}
}
public static func newBackgroundContext() -> NSManagedObjectContext? { return NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType) }
}
let _ = Laze<NoneDefault,ModelResponse>.get(path: PathPattern.model.patternToPath(with: ["modelId":modelId])) { (response, result, error) in
guard error == nil else {
failure?()
return
}
var models: Model? = nil
do {
models = try result?.serialized(Default.newBackgroundContext())
} catch { } catch {
print(error) print(error)
} }

View File

@ -1,11 +1,37 @@
import XCTest import XCTest
@testable import SLazeKit @testable import SLazeKit
class SLazeKitTests: XCTestCase { struct Configuration: LazeConfiguration {
static var allTests = [ static var basePath: String? { return "www.yourdomain.com" }
("testExample", testExample), static var basePort: Int? { return 8765 }
] static var decoder: JSONDecoder { return JSONDecoder() }
static var urlSession: URLSession { return URLSession.shared }
func testExample() { static func setup(_ request: URLRequest) -> URLRequest {
var request: URLRequest = request
request.setValue("Your token", forHTTPHeaderField: "X-Access-Token")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
return request
}
static func handle(_ response: HTTPURLResponse?) {
if response?.statusCode == 401 {
print("unauthorised")
}
}
}
class SLazeKitTests: XCTestCase {
static var allTests = [
("testBasePath", testBasePath),
("testBasePort", testBasePort),
]
func testBasePath() {
XCTAssertTrue(Configuration.basePath == "www.yourdomain.com")
}
func testBasePort() {
XCTAssertTrue(Configuration.basePort == 8765)
} }
} }