TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | Custom |
ReleasedLast Release | Jul 2016 |
SPMSupports SPM | ✓ |
Maintained by Posse, Kevin Gray.
H (named aitch /ˈeɪtʃ/ or haitch /ˈheɪtʃ/ in Ireland and parts of Australasia; plural aitches or haitches)
Haitch is an HTTP Client written in Swift for iOS and Mac OS X.
Request
/ Response
injection allowing for “plug-in” functionalityResponse
interface so you can design for whatever specific response your app requiresHaitch 0.7+
and development
require swift 2.2+. If you’re using a version of swift < 2.2, you should use Haitch version 0.6
.
Making a request is easy
let httpClient: HttpClient = HttpClient()
let req: Request = Request.Builder()
.url(url: "http://my.domain.com/path", params: params)
.method("GET")
.build()
httpClient.execute(req) { (response: Response?, error: NSError?) -> Void in
// deal with the response data (NSData) or error (NSError)
}
Getting back JSON is simple
client.execute(request: req, responseKind: JsonResponse.self) {
(response, error) -> Void in
if let jsonResponse: JsonResponse = response as? JsonResponse {
print(jsonResponse.json) // .json == AnyObject?
}
}
If you use SwiftyJSON you could create a custom Response class to convert the Response
data to the JSON
data type. It’s very easy to do so.
import Foundation
import SwiftyJSON
public class SwiftyJsonResponse: Response {
private (set) public var json: JSON?
private (set) public var jsonError: AnyObject?
public convenience required init(response: Response) {
self.init(request: response.request, data: response.data, statusCode: response.statusCode,
error: response.error)
}
public override init(request: Request, data: NSData?, statusCode: Int, error: NSError?) {
super.init(request: request, data: data, statusCode: statusCode, error: error)
self.populateJSONWithResponseData(data)
}
private func populateJSONWithResponseData(data: NSData?) {
if data != nil {
var jsonError: NSError? = nil
let json: JSON = JSON(data: data!, options: .AllowFragments, error: &jsonError)
self.jsonError = jsonError
self.json = json
}
}
}
sharedClient
(or some such)?Because it’s about your needs and not what we choose for you. You should both understand AND be in control of your network stack. If you feel strongly about it, subclass HttpClient
and add it yourself. Simple.
It’s up to you. There are other fantastic frameworks out there but, in our experience, we only need a small subset of the things they do. The goal of Haitch was to allow you to write modular, reusable notworking logic that matches your specific requirements. Not to deal with the possiblity of “what if?”.
The code here has been written based on Posse’s experiences with clients of all sizes. It has been production tested. That said, this incarnation of the code is our own. It’s fresh. We plan to use it in production and we plan to keep on improving it. If you find a bug, let us know!
We’re the best friggin mobile shop in NYC that’s who. Hey, but we’re biased. Our stuff is at http://goposse.com. Go check it out.
Haitch is sponsored, owned and maintained by Posse Productions LLC. Follow us on Twitter @goposse. Feel free to reach out with suggestions, ideas or to say hey.
If you believe you have identified a serious security vulnerability or issue with Haitch, please report it as soon as possible to [email protected]. Please refrain from posting it to the public issue tracker so that we have a chance to address it and notify everyone accordingly.
Haitch is released under a modified MIT license. See LICENSE for details.