Slide 37
Slide 37 text
BrightFutures & SwiftyJSON
// AppManager.swift
func getItems(pageInfo: PageInfo, query: String?) -> PageResponseFuture {
let promise = Promise()
AlamofireClient.requestJSON(QiitaAPIRouter.Items(pageInfo: pageInfo, query: query))
.onSuccess { (result: JSONResponse) in
let pageResponse = PageResponse(json: result.json, pageInfo: pageInfo, response: result.response!)
promise.success(pageResponse)
}
.onFailure { (errorResponse: ErrorResponse) in
promise.failure(errorResponse)
}
return promise.future
}
struct PageResponse {
var json: JSON
var totalCount: Int?
var pageInfo: PageInfo
init(json: JSON, pageInfo: PageInfo, response: NSHTTPURLResponse?) {
self.json = json
self.pageInfo = pageInfo
if let response = response {
let headers = JSON(response.allHeaderFields)
self.totalCount = headers["Total-Count"].intValue
}
}
}
typealias PageResponseFuture = Future
Inject something
before network
returns.