BFAstral 1.4.0

BFAstral 1.4.0

Julio Alorro 维护。



 
依赖关系
Astral>= 0
BrightFutures>= 0
 

BFAstral 1.4.0

  • Julio Alorro

BFAstral

BFAstral 是 Astral 的一个扩展,使用 Futures/Promises 来进行 HTTP 网络调用。

BFAstral 利用 BrightFutures 库简化与网络相关的异步调用,使您的代码库尽可能地更容易阅读。

CI Status Version License Platform

要求

BFAstral 需要 iOS 9.3 或更高版本和 Swift 3.x。

安装

CocoaPods

  1. 将以下内容添加到您的 Podfile
pod 'BFAstral'
  1. 使用框架集成依赖项:请在 Podfile 中添加 use_frameworks!
  2. 运行 pod install

简单示例

这是一个使用 Pokeapi API 和 RequestBuilder 以及 BFDispatcher 实现的示例。

请随意构建和自定义您自己的实现。只需采用适当的协议。

struct PokeAPIConfiguration: RequestConfiguration {

    let scheme: URLScheme = URLScheme.http

    let host: String = "pokeapi.co"

    let basePathComponents: [String] = [
        "api",
        "v2"
    ]

    let baseHeaders: Set<Header> = [
        Header(key: Header.Field.contentType, value: Header.Value.mediaType(MediaType.applicationJSON))
    ]
}
struct PokemonRequest: Request {

    let id: Int

    let configuration: RequestConfiguration = PokeAPIConfiguration()

    let method: HTTPMethod = HTTPMethod.get

    let pathComponents: [String] = [
        "pokemon",
        "\(self.id)"
    ]

    let parameters: [String : Any] = [:]

    let headers: Set<Header> = []
}
let queue: DispatchQueue = DispatchQueue(label: "pokeapi", qos: DispatchQoS.utility, attributes: [DispatchQueue.Attributes.concurrent])

let request: Request = PokemonRequest(id: 1)
let dispatcher: BFDispatcher = BFDispatcher()

dispatcher.response(of: request)
    .onSuccess(queue.context) { (response: Response) -> Void in
    // let responseData: Data = response.data
    // Do something with data
    // or
    // let dictionary: [String: Any] = response.json.dictValue
    // Do something with dictionary
    }
    .onFailure(queue.context) { (error: NetworkingError) -> Void in
    // Handle the error
    }
    .onComplete(queue.context) { (result: Result<Data, NetworkingError>) -> Void in
    // Handle the completion of the network request
    // such as clean up of the UI
    }