Astral
Astral 是一个精简的 HTTP 网络库,旨在通过抽象创建网络请求所需的步骤到多个对象中,从而简化应用程序的网络层。
它旨在避免典型的网络层单例模式,通过将每个网络请求部分封装为一个对象。
受到 Soroush Khanlou 的关于协议导向编程的文章的启发。
要求
Astral 需要 iOS 9.3 或更高版本以及 Swift 3.x
安装
CocoaPods
- 在你的 Podfile 中添加以下内容
pod 'Astral'
- 使用框架集成依赖性:在你的 Podfile 中添加
use_frameworks!
- 运行
pod install
。
Carthage
将以下内容添加到您的 Cartfile 中
github "hooliooo/Astral"
Swift Package Manager
在您的 Package.swift 文件中添加以下依赖关系
.package(url: "https://github.com/hooliooo/Astral.git", from: "0.9.0")
简单示例
以下是一个使用精灵宝可梦API 和 Astral 提供的 RequestBuilder 和 RequestDispatcher 实现的示例。
请随意构建和自定义自己的实现。只需采用适当的协议。
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.Key.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: Parameters = .none
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: RequestDispatcher = BaseRequestDispatcher(queue: queue)
dispatcher.response(
of: request,
onSuccess: { [weak self] (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: { (error: NetworkingError) -> Void in
// Handle the error
},
onComplete: { () -> Void in
// Handle the completion of the network request
// such as clean up of the UI
}
)
详细示例
假设您有一个需要 URL 路径组件的 GET 请求。以下是一个相关请求的示例:
struct YourAPIConfiguration: RequestConfiguration {
let scheme: URLScheme = URLScheme.https
let host: String = "yourhost.com"
let basePathComponents: [String] = [
"api",
"v1"
]
let baseHeaders: Set<Header> = [
Header(key: Header.Key.contentType, value: Header.Value.mediaType(MediaType.applicationJSON))
]
}
struct YourRequest: Request {
let configuration: RequestConfiguration = YourAPIConfiguration()
let method: HTTPMethod = HTTPMethod.get
let pathComponents: [String] = [
"your",
"path",
"components"
]
let parameters: Parameters = .dict([
"yourKey": "yourValue,
"anotherKey": "anotherValue"
])
let headers: Set<Header> = [
Header(key: Header.Key.accept, value: Header.Value.mediaType(MediaType.applicationJSON))
]
}
let request: Request = YourRequest()
let dispatcher: RequestDispatcher = BaseRequestDispatcher(isDebugMode: true)
dispatcher.urlRequest(of: request).url
这些代码行会让您的模拟请求创建以下 URL
https://yourhost.com/api/v1/your/path/components?yourKey=yourValue&anotherKey=anotherValue
响应也将包含以下标题:
Content-Type = application/json
Accept = application/json
如果 RequestConfiguration 和 Request 都有相同键的 Header,则 Request 会覆盖 RequestConfiguration 的 Header。
作者
许可证
Astral 使用 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。