Redes 2.0.4

Redes 2.0.4

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Mar 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Moch, Moch Xiao.



Redes 2.0.4

  • By
  • Moch Xiao

Redes

High-level network layer abstraction library written in Swift.

Why not just use Alamofire directly

  • If one day the project manager tell you need to use Socket reconstruct all network interfaces, you are going to all the business code to replace the Alamofire to new Socket requests?
  • A few days late, the project manager tell you some old interfaces must compatible with previous methods, but requires the use of Socket new interfaces to achieve, you are going to change the network interfaces which you already reconstructed back?
  • Direct use Alamofire, all requests related parameters are located in the business code, and there are several different methods of calling the request, such as uploading and downloading and a normal request invoke methods on the inconsistency. Pack all related parameters into an object, you can use only one very method to achieve all different situations request call. Conforms to the Command Pattern rules
  • Wrap network layer, brings more conducive to expansion. If you preferred use Alamofire andSocket both, only need to specify which way you want to use for request in command inside, and then expand the corresponding request mode implement (default only implements Alamofire)
  • The code change of network layer does not affect the business layer, underlying free replacement transducer, a request to call as long as the business layer rule to conform redes ask

Requirements

  • iOS 9.0+
  • Xcode 8.0+

Usage

  • Make your API conforms to Requestable
struct LoginApi: Requestable {
    var userName: String = ""
    var passWord: String = ""

    var url: URLConvertible {
        return "https://host/to/path"
    }

    var method: HTTPMethod {
        return .POST
    }

    var var bodies: HTTPBodies {
        return [
            "user": userName,
            "pass": passWord
        ]
    }

    var headers: HTTPHeaders {
        return [:]
    }
}
  • Build api and start request & process result
let loginRequest = LoginApi().action()

loginRequest.responseJSON {
    debugPrint($0.result)
}

loginRequest.responseJSON(queue: DispatchQueue.global()) {
    debugPrint($0.result)
}
.responseString {
    switch $0.result {
    case .success(let value):
        debugPrint(value)
    case .failure(let error):
         debugPrint(error)
    }
}
// .cancel()
  • More information see the demo in project. (Before you run this project, checkout API.swift and change the setups to your server configuration.)

Caching

Caching is handled on the system framework level by URLCache.

Notice

  • v0.x for swift 2.2
  • v1.x for swift 2.3
  • v2.x for swift 3.x

License

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

Contact

Follow me on Twitter (@mochxiao)