RxRequester 0.4.0

RxRequester 0.4.0

Shaban Kamel维护。



RxRequester-Swift

RxSwift,Alamofire和Moya的简单包装器,用于抽象重复代码,帮助您

  • 制作干净的RxSwift请求。
  • 内联和全局错误处理。
  • 在出现错误后(例如token过期错误)恢复当前请求。
  • 轻松控制加载指示器。

用法

extension ViewController: Presentable {
    public func showError(error: String) { show(error: error) }
    public func showLoading() { showLoading(show: true) }
    public func hideLoading() { showLoading(show: false) }
    public func onHandleErrorFailed(error: Error) { show(error: "Oops, something went wrong!") }
}
    // Set handlers
    RxRequester.nsErrorHandlers = [ConnectivityHandler()]
    RxRequester.errorHandlers = [MyErrorHandler()]
    RxRequester.resumableHandlers = [UnauthorizedHandler()]
      
    // Request
    RxRequester(presentable: self).request { loginApi.login() }

安装

Swift包管理器

要使用Apple的Swift包管理器进行集成,请在您的Package.swift中添加以下作为依赖项

.package(url: "https://github.com/ShabanKamell/RxRequester-Swift.git", .upToNextMajor(from: "0.3.0"))

然后指定

// swift-tools-version:5.0
import PackageDescription

let package = Package(
    name: "MyPackage",
    products: [
        .library(
            name: "MyPackage",
            targets: ["MyPackage"]),
    ],
    dependencies: [
        .package(url: "https://github.com/ShabanKamell/RxRequester-Swift.git", .upToNextMajor(from: "0.3.0"))
    ],
    targets: [
        .target(
            name: "MyPackage",
            dependencies: ["RxRequesterAlamofire"])
    ]
)

Accio

Accio 是基于 SwiftPM 的依赖管理器,可以构建 iOS/macOS/tvOS/watchOS 的框架。因此,RxRequester 的集成步骤与上述描述完全相同。一旦您配置了 Package.swift 文件,运行 accio update 而不是 swift package update

CocoaPods

对于 RxRequester,在您的 Podfile 中使用以下条目

pod 'RxRequester', '~> 0.3.0'

# or 

pod 'RxRequester/Alamofire', '~> 0.3.0'

# or

pod 'RxRequester/Moya', '~> 0.3.0'

然后运行 pod install

在您想要使用 RxRequester 的任何文件中,别忘了使用 import RxRequester 导入框架。对于 Alamofire,使用 import RxRequesterAlamofire。对于 Moya,使用 import RxRequesterMoya

Carthage

Carthage 用户可以指向此仓库,并使用任何他们想要的生成的框架 RxRequesterRxRequesterAlamofireRxRequesterMoya

在您的 Cartfile 中进行以下配置

github "ShabanKamell/RxRequester" ~> 13.0

然后运行 carthage update

如果您是第一次在项目中使用 Carthage,您需要遵循一些额外的步骤,如 Carthage 中所述。

注意:目前,Carthage 不提供只构建特定仓库子模块的方法。所有子模块及其依赖都将使用上述命令构建。但是,您不需要将不使用的框架复制到您的项目中。例如,如果您不使用 RxRequesterAlamofire,在 carthage update 完成后,可以随意删除该框架以及 RxRequesterMoya 从 Carthage Build 目录中。

错误处理

RxRequester 在需要处理错误时表现出色。RxRequester 中的错误可以通过为每个错误提供处理程序来处理。例如,如果您想处理连接错误 NSURLErrorNotConnectedToInternet,您必须提供以下处理程序

import RxRequester

struct ConnectivityHandler: NSErrorHandler {
    var supportedErrors: [Int] = [NSURLErrorNotConnectedToInternet]

    func handle(error: NSError, presentable: Presentable?) {
        presentable?.showError(error: error.localizedDescription)
    }
}

Alamofire & Moya

RxReqeuster支持处理 AlamofireMoya 错误。请查看以下处理程序类型。

错误处理程序类型

处理程序类型 描述
NSErrorHandler 处理 NSError
ResumableHandler 提供在
错误之后再进行调用的请求
并且在进行主请求之前。
ErrorHandler 处理任何 Swift.Error

Alamofire 处理程序

处理程序类型 描述
AlamofireStatusCodeHandler 处理 HTTP 状态码
AlamofireUnderlyingErrorHandler 处理底层错误
AlamofireErrorHandler 处理任何 AFError

Moya 处理程序

处理程序类型 描述
MoyaStatusCodeHandler 处理 HTTP 状态码
MoyaUnderlyingErrorHandler 处理底层错误
MoyaErrorHandler 处理任何 MoyaError

自定义请求

RxRequester允许您全面控制任何请求

  • 内联错误处理
  • 启用/禁用加载指示器
  • 在错误发生时调用代码。
  • 设置 subscribeOn 调度器
  • 设置 observeOn 调度器
    let options = RequestOptions.Builder()
         .showLoading(true)
         .inlineErrorHandling { error in false }
         .doOnError { error in }
         .observeOnScheduler(MainScheduler.instance)
         .subscribeOnScheduler(ConcurrentDispatchQueueScheduler(qos: .background))
         .build()
     rxRequester.request(options: options) { .. }

查看“示例”组以获取完整代码。

许可证

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   https://apache.ac.cn/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.