refactor: fix code review notes

This commit is contained in:
Ivan Smolin 2021-09-27 10:55:55 +03:00
parent 8397f15ec5
commit 4b818afa85
8 changed files with 22 additions and 11 deletions

View File

@ -42,6 +42,8 @@ open class ApplicationJsonBodyContent<Body>: BaseContent, BodyContent {
super.init(mediaTypeName: CommonMediaTypes.applicationJson.rawValue)
}
// MARK: - BodyContent
public func encodeBody() throws -> Data {
try encodingClosure()
}

View File

@ -23,6 +23,9 @@
import Foundation
public final class EmptyBodyContent: BaseContent, BodyContent {
// MARK: - BodyContent
public func encodeBody() throws -> Data {
Data()
}

View File

@ -31,6 +31,8 @@ open class ApplicationJsonResponseContent<Model: Decodable>: BaseContent, Respon
super.init(mediaTypeName: CommonMediaTypes.applicationJson.rawValue)
}
// MARK: - ResponseContent
public func decodeResponse(data: Data) throws -> Model {
try jsonDecoder.decode(Model.self, from: data)
}

View File

@ -23,18 +23,19 @@
import Foundation
import TISwiftUtils
public struct MapResponseContent<Model>: ResponseContent {
public final class MapResponseContent<Model>: BaseContent, ResponseContent {
private let decodeClosure: ThrowableClosure<Data, Model>
public let mediaTypeName: String
public init<C: ResponseContent>(responseContent: C, transform: @escaping Closure<C.Model, Model>) {
mediaTypeName = responseContent.mediaTypeName
decodeClosure = {
transform(try responseContent.decodeResponse(data: $0))
}
super.init(mediaTypeName: responseContent.mediaTypeName)
}
// MARK: - ResponseContent
public func decodeResponse(data: Data) throws -> Model {
try decodeClosure(data)
}

View File

@ -22,20 +22,18 @@
import Foundation
public struct TextPlainResponseContent: ResponseContent {
struct StringDecodingError: Error {
public final class TextPlainResponseContent: BaseContent, ResponseContent {
public struct StringDecodingError: Error {
let data: Data
let encoding: String.Encoding
}
private let encoding: String.Encoding
// MARK: - Content
public let mediaTypeName = CommonMediaTypes.textPlain.rawValue
public init(encoding: String.Encoding = .utf8) {
self.encoding = encoding
super.init(mediaTypeName: CommonMediaTypes.textPlain.rawValue)
}
// MARK: - ResponseContent

View File

@ -27,6 +27,8 @@ open class PathParameterEncoding: BaseUrlParameterEncoding, ParameterEncoding {
self.templateUrl = templateUrl
}
// MARK: - ParameterEncoding
open func encode(parameters: [String: Parameter<LocationPath>]) -> String {
.render(template: templateUrl, using: encode(parameters: parameters))
}

View File

@ -21,6 +21,9 @@
//
open class QueryStringParameterEncoding: BaseUrlParameterEncoding, ParameterEncoding {
// MARK: - ParameterEncoding
open func encode(parameters: [String: Parameter<LocationQuery>]) -> [String: Any] {
let includedKeys = Set(super.encode(parameters: parameters).map { $0.key })

View File

@ -25,4 +25,4 @@ protocol ParameterEncoding {
associatedtype Result
func encode(parameters: [String: Parameter<Location>]) -> Result
}
}