Networkable
概览
Networkable 的基本思路是建立在 URLSession
之上的临时网络玩家。它应该足够简单,以便常见的事情易于处理,同时足够全面,以便复杂的事情也容易处理。
为什么不选择 Alamofire/Moya?
与它们相比,Networkable是一个小巧实用的库,旨在最基本的一层网络,触发请求然后处理响应。如果你是那种喜欢控制一切的开发者,那么可能喜欢一个可理解的包。
示例使用
/// An object provides methods for interacting with the crytocurrency market data in the remote database.
protocol RemoteCryptocurrencyMarketRepository {
/// Get all available exchanges.
/// - Parameter promise: A promise to be fulfilled with a result represents either a success or a failure.
/// - Returns: A URL session task that returns downloaded data directly to the app in memory.
@discardableResult
func exchangesTask(promise: @escaping (Result<[Exchange], Error>) -> Void) -> URLSessionDataTask?
/// Get all available exchanges.
/// - Returns: A publisher emits a list of exchanges
@available(macOS 10.15, macCatalyst 13.0, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
func exchangesPublisher() -> AnyPublisher<[Exchange], Error>
/// Get all available exchanges.
/// - Returns: An asynchronously-delivered list of exchanges.
@available(macOS 12.0, macCatalyst 15.0, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
func exchanges() async throws -> [Exchange]
}
/// An object provides methods for interacting with the crytocurrency market data in the remote database.
final class DefaultRemoteCryptocurrencyMarketRepository: RemoteCryptocurrencyMarketRepository {
// MARK: Dependencies
/// An ad-hoc network layer that is built on `URLSession` to perform an HTTP request.
let session: NetworkableSession
// MARK: Init
/// Initiate an object provides methods for interacting with the crytocurrency market data in the remote database.
/// - Parameter session: An ad-hoc network layer that is built on `URLSession` to perform an HTTP request.
init(session: NetworkableSession = NetworkSession.coincap) {
self.session = session
}
// MARK: RemoteCryptocurrencyMarketRepository
@discardableResult
func exchangesTask(promise: @escaping (Result<[Exchange], Error>) -> Void) -> URLSessionDataTask? {
session.dataTask(
for: API.exchanges,
resultQueue: nil,
decoder: JSONDecoder()
) { (result: Result<Datum<[Exchange]>, Error>) in
let exchanges = result.map { $0.data }
promise(exchanges)
}
}
@available(macOS 10.15, macCatalyst 13.0, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
func exchangesPublisher() -> AnyPublisher<[Exchange], Error> {
session
.dataTaskPublisher(
for: API.exchanges,
resultQueue: nil,
decoder: JSONDecoder())
.map(\Datum<[Exchange]>.data)
.eraseToAnyPublisher()
}
@available(macOS 12.0, macCatalyst 15.0, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
func exchanges() async throws -> [Exchange] {
let datum = try await session.data(for: API.exchanges, decoder: JSONDecoder()) as Datum<[Exchange]>
return datum.data
}
// MARK: Subtypes - API
/// An object abstracts an HTTP request.
private enum API: Request {
/// Get all available exchanges.
case exchanges
// MARK: Request
var headers: [String: String]? { nil }
var url: String { "/v2/exchanges" }
var method: Networkable.Method { .get }
func body() throws -> Data? { nil }
}
}
示例项目
cd NetworkableExample
pod install
open NetworkableExample.xcworkspace/
等待CocoaPods生成工作空间,然后你就可以开始了。
Networkable
的使用案例可以在UseCases
和Repositories
目录中找到。
功能
- 组合支持
- 支持async/await
- 简单易用的测试功能
- 允许您通过关联的枚举值明确定义不同端点的使用
- 中间件提供注入逻辑的能力
- 授权
- 本地化
- 日志记录
- 错误处理
- ...
要求
- macOS 10.12+
- iOS 8.0+
- tvOS 10.0+
- watchOS 3.0+
安装
Swift包管理器
嵌入到包中
dependencies: [
.package(url: "https://github.com/duytph/Networkable"),
]
嵌入到Xcode项目中
- 打开菜单文件 > Swift包 > 添加包依赖...
- 输入https://github.com/duytph/Networkable
CocoaPods
pod 'Networkable'
Carthage
未实现
作者
Duy Tran ([邮箱 protected])
许可
Networkable 在MIT许可下可用。更多信息请参见LICENSE文件。