2021-05-03 13:33:13 +07:00
|
|
|
//
|
|
|
|
// URLParameterEncoder.swift
|
|
|
|
// MapDemoApp
|
|
|
|
//
|
|
|
|
// Created by JungpyoHong on 4/25/21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2021-05-20 11:13:58 +07:00
|
|
|
struct URLParameterEncoder: ParameterEncoder {
|
|
|
|
static func encode(urlRequest: inout URLRequest, with parameters: Parameters) throws {
|
|
|
|
guard let url = urlRequest.url else { throw AppError.badUrl }
|
2021-05-03 13:33:13 +07:00
|
|
|
|
2021-05-20 11:13:58 +07:00
|
|
|
if var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false), !parameters.isEmpty {
|
2021-05-03 13:33:13 +07:00
|
|
|
urlComponents.queryItems = [URLQueryItem]()
|
|
|
|
|
2021-05-20 11:13:58 +07:00
|
|
|
for (key, value) in parameters {
|
|
|
|
let queryItem = URLQueryItem(name: key, value: "\(value)")
|
2021-05-03 13:33:13 +07:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|