Update ParameterEncoding.swift

This commit is contained in:
Jungpyo Hong 2021-05-19 23:13:33 -05:00 committed by GitHub
parent 21af3d0920
commit 274174ccdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,47 +7,9 @@
import Foundation
public typealias Parameters = [String:Any]
typealias Parameters = [String: Any]
public protocol ParameterEncoder {
func encode(urlRequest: inout URLRequest, with parameters: Parameters) throws
}
public enum ParameterEncoding {
case urlEncoding
case jsonEncoding
case urlAndJsonEncoding
public func encode(urlRequest: inout URLRequest,
bodyParameters: Parameters?,
urlParameters: Parameters?) throws {
do {
switch self {
case .urlEncoding:
guard let urlParameters = urlParameters else { return }
try URLParameterEncoder().encode(urlRequest: &urlRequest, with: urlParameters)
case .jsonEncoding:
guard let bodyParameters = bodyParameters else { return }
try JSONParameterEncoder().encode(urlRequest: &urlRequest, with: bodyParameters)
case .urlAndJsonEncoding:
guard let bodyParameters = bodyParameters,
let urlParameters = urlParameters else { return }
try URLParameterEncoder().encode(urlRequest: &urlRequest, with: urlParameters)
try JSONParameterEncoder().encode(urlRequest: &urlRequest, with: bodyParameters)
}
}catch {
throw error
}
}
}
public enum NetworkError : String, Error {
case parametersNil = "Parameters were nil."
case encodingFailed = "Parameter encoding failed."
case missingURL = "URL is nil."
protocol ParameterEncoder {
static func encode(urlRequest: inout URLRequest,
with parameters: Parameters) throws
}