首页 > 编程语言 > 详细

ReactiveX 学习笔记(17)使用 RxSwift + Alamofire 调用 REST API

时间:2018-08-23 01:27:43      阅读:446      评论:0      收藏:0      [点我收藏+]

JSON : Placeholder

JSON : Placeholder (https://jsonplaceholder.typicode.com/) 是一个用于测试的 REST API 网站。
以下使用 RxJava2 + Retrofit2 调用该网站的 REST API,获取字符串以及 JSON 数据。

  • GET /posts/1
  • GET /posts
  • POST /posts
  • PUT /posts/1
  • DELETE /posts/1

除了 DELETE 之外其余4个API都返回JSON数据,格式(JSON-Schema)如下:

{
  "type":"object",
  "properties": {
    "userId": {"type" : "integer"},
    "id": {"type" : "integer"},
    "title": {"type" : "string"},
    "body": {"type" : "string"}
  }
}

创建工程

打开 Xcode,File / New / Project..
在 New Project 向导的第1页,选 macOS / Command Line Tool
在向导的第2页填上 Product Name: RestExample
在向导的第3页选择文件夹点击 Create 按钮创建工程

配置 Pods

在工程所在文件夹下创建 Podfile 文件,内容如下:

use_frameworks!

target 'RestExample' do
project 'RestExample'
pod 'CodableAlamofire'
pod 'RxAlamofire'
end

打开终端在工程所在文件夹下执行 pod install 命令。

$ cd RestExample
$ pod install
...
Installing Alamofire (4.7.3)
Installing CodableAlamofire (1.1.0)
Installing RxAlamofire (4.2.0)
Installing RxSwift (4.2.0)
...

用 Xcode 打开 RestExample.xcworkspace

RxCodableAlamofire

在 RxCodableAlamofire 工程中添加 RxCodableAlamofire.swift 文件,内容如下:

import Foundation

import RxSwift
import Alamofire
import RxAlamofire
import CodableAlamofire

class RxCodableAlamofire {
    public static func object<T: Decodable>(_ method: Alamofire.HTTPMethod, _ url: URLConvertible, parameters: [String: Any]? = nil, encoding: ParameterEncoding = URLEncoding.default, headers: [String: String]? = nil, queue: DispatchQueue? = nil, keyPath: String? = nil, mapToObject object: T? = nil, decoder: JSONDecoder = JSONDecoder()) -> Observable<T> {
        return SessionManager.default.rx.object(method, url, parameters: parameters, encoding: encoding, headers: headers, queue: queue, keyPath: keyPath, mapToObject: object, decoder: decoder)
    }
}

extension Reactive where Base: SessionManager {
    public func object<T: Decodable>(_ method: Alamofire.HTTPMethod, _ url: URLConvertible, parameters: [String: Any]? = nil, encoding: ParameterEncoding = URLEncoding.default, headers: [String: String]? = nil, queue: DispatchQueue? = nil, keyPath: String? = nil, mapToObject object: T? = nil, decoder: JSONDecoder = JSONDecoder()) -> Observable<T> {
        return request(method, url, parameters: parameters, encoding: encoding, headers: headers).flatMap { $0.rx.object(queue: queue, keyPath: keyPath, mapToObject: object, decoder: decoder) }
    }
}

extension Reactive where Base: DataRequest {
    public func object<T: Decodable>(queue: DispatchQueue? = nil, keyPath: String? = nil, mapToObject object: T? = nil, decoder: JSONDecoder = JSONDecoder()) -> Observable<T> {
        return result(queue: queue, responseSerializer: DataRequest.DecodableObjectSerializer(keyPath, decoder))
    }
}

在 Pods 工程中找到并打开 DataRequest+Decodable.swift (Pods/CodableAlamofire)
将其中的 DecodableObjectSerializer 的可见性从 private 改为 public。(提示是否 unlock 时按下 Unlock)

ReactiveX 学习笔记(17)使用 RxSwift + Alamofire 调用 REST API

原文:https://www.cnblogs.com/zwvista/p/9521239.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!