mirror of
https://github.com/jphong1111/awesome-ios-developer.git
synced 2025-01-31 09:58:51 +07:00
54 lines
1.6 KiB
Swift
54 lines
1.6 KiB
Swift
//
|
|
// ParameterEncoding.swift
|
|
// MapDemoApp
|
|
//
|
|
// Created by JungpyoHong on 4/25/21.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public 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."
|
|
}
|