|
| 1 | +//: [Previous](@previous) |
| 2 | + |
| 3 | +import Foundation |
| 4 | + |
| 5 | +import Combine |
| 6 | +import ObjectiveC |
| 7 | + |
| 8 | +import APIWrapper |
| 9 | + |
| 10 | +/*: |
| 11 | + The design goal of `RaAPIWrapper` is to better encapsulate requests and simplify the request process rather than execute them. |
| 12 | + |
| 13 | + So we don't provide any methods for request api. You can define your own request methods by referring to the code in the `Demo/Sources/API+Request.swift` file. |
| 14 | + |
| 15 | + Here are 2 request wrappers for `Combine`, which are roughly written for reference only: |
| 16 | + */ |
| 17 | + |
| 18 | +// For subsequent examples |
| 19 | +enum CombineAPI { |
| 20 | + @POST("/post") |
| 21 | + static var post: APIParameterBuilder<String>? = { $0 } |
| 22 | +} |
| 23 | + |
| 24 | +// MARK: - AnyPublisher |
| 25 | + |
| 26 | +//: The first way: deliver an `AnyPublisher<T, Error>` object externally and subscribe to it to trigger requests. |
| 27 | + |
| 28 | +extension API { |
| 29 | + func requestPublisher(with params: Parameter) -> AnyPublisher<Data, URLError> { |
| 30 | + let info = createRequestInfo(params) |
| 31 | + |
| 32 | + // To simplify the demo process, here is a forced unpacking |
| 33 | + let url = URL(string: "https://postman-echo.com" + info.path)! |
| 34 | + |
| 35 | + var request = URLRequest(url: url) |
| 36 | + request.httpMethod = info.httpMethod.rawValue |
| 37 | + |
| 38 | + if let parameters = info.parameters { |
| 39 | + do { |
| 40 | + request.httpBody = try JSONEncoder().encode(parameters) |
| 41 | + } catch { |
| 42 | + fatalError("") |
| 43 | + } |
| 44 | + |
| 45 | + request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
| 46 | + } |
| 47 | + |
| 48 | + return URLSession.shared |
| 49 | + .dataTaskPublisher(for: request) |
| 50 | + .map { (data, _) in return data } |
| 51 | + .mapError { $0 } |
| 52 | + .eraseToAnyPublisher() |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +var cancellable = Set<AnyCancellable>() |
| 57 | +let publisher = CombineAPI.$post.requestPublisher(with: "123") |
| 58 | +publisher.sink(receiveCompletion: { |
| 59 | + print($0) |
| 60 | + |
| 61 | +}, receiveValue: { |
| 62 | + print(String(data: $0, encoding: .utf8) as Any) |
| 63 | + |
| 64 | +}).store(in: &cancellable) |
| 65 | + |
| 66 | +// MARK: - PassthroughSubject |
| 67 | + |
| 68 | +/*: |
| 69 | + The second one is to provide a `PassthroughSubject` object to the outside world, |
| 70 | + send parameters when requesting the api, subscribe to the object at other places, |
| 71 | + accept the parameters and send the request. |
| 72 | + */ |
| 73 | + |
| 74 | +private var kParamSubjectKey: String = "kParamSubjectKey" |
| 75 | + |
| 76 | +public extension API { |
| 77 | + @available(iOS 13.0, *) |
| 78 | + var paramSubject: PassthroughSubject<Parameter, URLError>? { |
| 79 | + get { |
| 80 | + if let value = objc_getAssociatedObject(self, &kParamSubjectKey) as? PassthroughSubject<Parameter, URLError> { |
| 81 | + return value |
| 82 | + } |
| 83 | + let paramSubject = PassthroughSubject<Parameter, URLError>() |
| 84 | + objc_setAssociatedObject(self, &kParamSubjectKey, paramSubject, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) |
| 85 | + return paramSubject |
| 86 | + } |
| 87 | + set { objc_setAssociatedObject(self, &kParamSubjectKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } |
| 88 | + } |
| 89 | + |
| 90 | + @available(iOS 13.0, *) |
| 91 | + func requestPublisher() -> AnyPublisher<Data, URLError>? { |
| 92 | + return paramSubject?.flatMap { self.requestPublisher(with: $0) }.eraseToAnyPublisher() |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +let api = CombineAPI.$post |
| 97 | + |
| 98 | +api.requestPublisher()?.sink(receiveCompletion: { |
| 99 | + print($0) |
| 100 | + |
| 101 | +}, receiveValue: { |
| 102 | + print(String(data: $0, encoding: .utf8) as Any) |
| 103 | + |
| 104 | +}).store(in: &cancellable) |
| 105 | + |
| 106 | +api.paramSubject?.send("233") |
| 107 | +api.paramSubject?.send("433") |
| 108 | +api.paramSubject?.send(completion: .finished) |
| 109 | + |
| 110 | +//: [Next](@next) |
0 commit comments