Skip to content

Latest commit

 

History

History
86 lines (65 loc) · 2.45 KB

README.md

File metadata and controls

86 lines (65 loc) · 2.45 KB

Santa

Version License Platform

A resource based network communication lib inspired by the first episode of the Swift Talk webshow. It decouples the definition of resources and the required network stack to make the actual network call.

So this type of request:

let request = URLRequest(url: URL(string: "your-url") !)
URLSession.shared.dataTask(with: request) { data, response, error in
    let httpResponse = response as! HTTPURLResponse
    guard httpResponse.statusCode == 200 else {
        fatalError()
    }
    let products = try !JSONDecoder().decode(Products.self, from: data!)
    // Some code to display products on screen
}

Can be written with Santa this way:

let resource = DataResource(url: "your-url", method: .get, body: nil) { data in
    return try JSONDecoder().decode(Products.self, from: data)
}

DefaultWebservice().load(resource: resource) { products, error in
    if let error = error {
        // do error handling
    }

    // Some code to display products on screen
}

This way resources can easily be placed right where they belong. As a part of the objects they are ment to fetch.

extension Products {
    static var all: DataResource < Products > {
        return DataResource(url: url, method: .get, body: nil) { data in
            return try JSONDecoder().decode(Products.self, from: data)
        }
    }
}

Features

  • Support for custom authorization
  • Background download tasks
  • Background upload tasks
  • Cancelling of running URLSessionTasks
  • ImageCache based on the url
  • Error handling for error status codes and network problems

Requirements

  • Swift 5
  • iOS 9.0 or newer

Installation

Santa is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Santa'

TODO

  • Write more tests
  • Add usage description
  • Make multipart form data more flexible

Author

Christian Braun

License

Santa is available under the MIT license. See the LICENSE file for more info.