Go to file
Shial 6deeadde56 [0.1.1] update readme 2018-01-07 12:50:24 +11:00
Content [0.1.1] update readme 2018-01-07 12:46:25 +11:00
SLazeKit.xcodeproj [0.1.0] Example model class 2017-12-27 14:16:29 +11:00
Sources/SLazeKit [0.1.1] Commit changes on context, update parent context and refresh all objects 2018-01-04 16:55:40 +11:00
Tests [0.1.0] Example model class 2017-12-27 14:16:29 +11:00
.gitignore Initial Commit 2017-12-12 17:03:04 +11:00
.swift-version Initial Commit 2017-12-12 17:03:04 +11:00
LICENSE Initial Commit 2017-12-12 17:03:04 +11:00
Package.swift Initial Commit 2017-12-12 17:03:04 +11:00
README.md [0.1.1] update readme 2018-01-07 12:50:24 +11:00
SLazeKit.podspec [0.1.1] Update podspec 2018-01-07 12:47:27 +11:00

README.md

SLazeKit

Language License CocoaPods Carthage

SLazeKit is an easy to use Swift restful collection of extensions and classes. Don't spend hours writing your code to map your rest api request into models and coredata serialization. Stop wasting your time!

SLazeKit allows you:

  • map your models by Codable protocol
  • serialize CoreData models from API response
  • fast and simple extend your models with API & CoreData

  • 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 response provides tuple with HTTPURLResponse object and the data returned by the server.
  • The request handler is executed on the URLSession delegate queue.
class func getRequest(for modelId: String, success: @escaping ((Model?) ->()), failure: (() ->())? = nil) throws {
    let _ = ResponseModel.get(path: PathPattern.model.patternToPath(with: ["modelId":modelId])) { (response, result, error) in
    guard response?.urlResponse?.statusCode == 200 && error == nil else {
        failure?()
        return
    }
    success(try result?.serialized(SLazeKit.newBackgroundContext()) as? Model)
    }
}

If ResponseModel conforms to EntityMapping protocol it will be synchronize with CoreData. That means if mode object already exist it will update it or creat new instance and fill NSManagedObject with response data. You can serialize your response to recive CoreData object on given context by:

try result?.serialized(NSManagedObjectContext) as? Model)

Calling this method simple query NSManagedObject which was synchronize before by given object id.

Models example

Simple Object.swift Advance Model.swift

🔧 Installation

CocoaPods:

Add the line pod "SLazeKit" to your Podfile

Carthage:

Add the line github "shial4/SLazeKit" to your Cartfile

Manual:

Clone the repo and drag the folder SLazeKit into your Xcode project.

Swift Package Manager:

Add the line .package(url: "https://github.com/shial4/SLazeKit.git", from: "0.1.0"), to your Package.swift

Swift Package Manager in your iOS Project: This project demonstrates a working method for using Swift Package Manager (SPM) to manage the dependencies of an iOS project.

Example of how to use SPM v4 to manage iOS dependencies

💊 Usage

For positive experience, you should configure SLazeKit at first. This step is REQUIRED!

import SLazeKit

extension SLazeKit {
    /// 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.

import SLazeKit

extension SLazeKit {
    //Provide base path for your API requests.
    open class var basePath?: String { return "www.yourdomain.com" }
    //Additional you can set port for your requests.
    open class var basePort: Int? { return 8040  }
    //You can provide your own instance of JSONDecoder.
    open class var decoder: JSONDecoder { return JSONDecoder() }
    //You can customize instance of URLSession in here.
    open class var urlSession: URLSession { return URLSession.shared }
    
    //If you feel like to set header dor requests do it in here.
    open class 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
    }
    //If you feel like to monitor response. You can do it in here.
    open class func handle(_ response: HTTPURLResponse?) {
        if response?.statusCode == 401 {
            Client.logout()
        }
    }
}

Contributing

Be welcome to contribute to this project! :)

Questions

Just create an issue on GitHub.

📝 License

This project was released under the MIT license.