mirror of
https://github.com/jphong1111/awesome-ios-developer.git
synced 2025-07-08 23:09:02 +07:00
upload network layer encoding
This commit is contained in:
22
Helper/Network Layer/Encoding/JSONParameterEncoder.swift
Normal file
22
Helper/Network Layer/Encoding/JSONParameterEncoder.swift
Normal file
@ -0,0 +1,22 @@
|
||||
//
|
||||
// JSONParameterEncoder.swift
|
||||
// MapDemoApp
|
||||
//
|
||||
// Created by JungpyoHong on 4/25/21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct JSONParameterEncoder: ParameterEncoder {
|
||||
public func encode(urlRequest: inout URLRequest, with parameters: Parameters) throws {
|
||||
do {
|
||||
let jsonAsData = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
|
||||
urlRequest.httpBody = jsonAsData
|
||||
if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
|
||||
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
}
|
||||
}catch {
|
||||
throw NetworkError.encodingFailed
|
||||
}
|
||||
}
|
||||
}
|
53
Helper/Network Layer/Encoding/ParameterEncoding.swift
Normal file
53
Helper/Network Layer/Encoding/ParameterEncoding.swift
Normal file
@ -0,0 +1,53 @@
|
||||
//
|
||||
// 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."
|
||||
}
|
33
Helper/Network Layer/Encoding/URLParameterEncoder.swift
Normal file
33
Helper/Network Layer/Encoding/URLParameterEncoder.swift
Normal file
@ -0,0 +1,33 @@
|
||||
//
|
||||
// URLParameterEncoder.swift
|
||||
// MapDemoApp
|
||||
//
|
||||
// Created by JungpyoHong on 4/25/21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct URLParameterEncoder: ParameterEncoder {
|
||||
public func encode(urlRequest: inout URLRequest, with parameters: Parameters) throws {
|
||||
|
||||
guard let url = urlRequest.url else { throw NetworkError.missingURL }
|
||||
|
||||
if var urlComponents = URLComponents(url: url,
|
||||
resolvingAgainstBaseURL: false), !parameters.isEmpty {
|
||||
|
||||
urlComponents.queryItems = [URLQueryItem]()
|
||||
|
||||
for (key,value) in parameters {
|
||||
let queryItem = URLQueryItem(name: key,
|
||||
value: "\(value)".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed))
|
||||
urlComponents.queryItems?.append(queryItem)
|
||||
}
|
||||
urlRequest.url = urlComponents.url
|
||||
}
|
||||
|
||||
if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
|
||||
urlRequest.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user