refactor: use TISwiftUtils in TINetworking

This commit is contained in:
Ivan Smolin 2021-09-17 19:55:31 +03:00
parent 4082ddb30e
commit b8550bd8d5
5 changed files with 15 additions and 10 deletions

View File

@ -41,7 +41,7 @@ let package = Package(
.target(name: "TIFoundationUtils", dependencies: ["TISwiftUtils"], path: "TIFoundationUtils/Sources"),
.target(name: "TIKeychainUtils", dependencies: ["TIFoundationUtils", "KeychainAccess"], path: "TIKeychainUtils/Sources"),
.target(name: "TITableKitUtils", dependencies: ["TIUIElements", "TableKit"], path: "TITableKitUtils/Sources"),
.target(name: "TINetworking", dependencies: ["Alamofire"], path: "TINetworking/Sources"),
.target(name: "TINetworking", dependencies: ["TISwiftUtils", "Alamofire"], path: "TINetworking/Sources"),
// MARK: - Elements

View File

@ -1,7 +1,8 @@
import Foundation
import TISwiftUtils
open class ApplicationJsonBodyContent<Body>: BaseContent, BodyContent {
private let encodingClosure: () throws -> Data
private let encodingClosure: ThrowableResultClosure<Data>
public init(body: Body, jsonEncoder: JSONEncoder = JSONEncoder()) where Body: Encodable {
encodingClosure = {

View File

@ -1,10 +1,12 @@
import TISwiftUtils
public struct AnyTypeMapping<R> {
private let mappingClosure: () -> Result<R, Error>
private let mappingClosure: ResultClosure<Result<R, Error>>
public let type: Any.Type
public init<T: Decodable>(decoder: Decoder,
transform: @escaping (T) -> R) {
transform: @escaping Closure<T, R>) {
type = T.self

View File

@ -1,11 +1,12 @@
import Foundation
import TISwiftUtils
public struct MapResponseContent<Model>: ResponseContent {
private let decodeClosure: (Data) throws -> Model
private let decodeClosure: ThrowableClosure<Data, Model>
public let mediaTypeName: String
public init<C: ResponseContent>(responseContent: C, transform: @escaping (C.Model) -> Model) {
public init<C: ResponseContent>(responseContent: C, transform: @escaping Closure<C.Model, Model>) {
mediaTypeName = responseContent.mediaTypeName
decodeClosure = {
transform(try responseContent.decodeResponse(data: $0))
@ -18,7 +19,7 @@ public struct MapResponseContent<Model>: ResponseContent {
}
public extension ResponseContent {
typealias TransformClosure<T> = (Model) -> T
typealias TransformClosure<T> = Closure<Model, T>
func map<R>(_ transform: @escaping TransformClosure<R>) -> MapResponseContent<R> {
.init(responseContent: self, transform: transform)
@ -26,11 +27,11 @@ public extension ResponseContent {
}
public extension JSONDecoder {
func responseContent<T: Decodable, R>(_ tranfsorm: @escaping (T) -> R) -> MapResponseContent<R> {
func responseContent<T: Decodable, R>(_ tranfsorm: @escaping Closure<T, R>) -> MapResponseContent<R> {
responseContent().map(tranfsorm)
}
func decoding<T: Decodable, R>(to tranfsorm: @escaping (T) -> R) -> (Data) throws -> R {
func decoding<T: Decodable, R>(to tranfsorm: @escaping Closure<T, R>) -> ThrowableClosure<Data, R> {
responseContent(tranfsorm).decodeResponse
}
}

View File

@ -1,9 +1,10 @@
import Foundation
import TISwiftUtils
public typealias StatusCodeMimeType = (statusCode: Int, mimeType: String?)
public typealias StatusCodesMimeType = (statusCodes: Set<Int>, mimeType: String?)
public typealias DecodingClosure<R> = (Data) throws -> R
public typealias DecodingClosure<R> = ThrowableClosure<Data, R>
public extension ResponseType {
func decode<R>(mapping: [KeyValueTuple<StatusCodeMimeType, DecodingClosure<R>>]) -> Result<R, ErrorType> {