This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fileprivate func addAdditionalHeaders(_ additionalHeader: HTTPHeaders?, | |
| request: inout URLRequest) { | |
| guard let headers = additionalHeader else { return } | |
| for (key, value) in headers { | |
| request.setValue(value, forHTTPHeaderField: key) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fileprivate func configureParameters(bodyParameters: Parameters?, | |
| urlParameters: Parameters?, | |
| request: inout URLRequest) throws { | |
| do { | |
| // bodyParameters ็บ JSON ๆ ผๅผ๏ผไฝฟ็จ JSONParameterEncoder ไพ็ทจ็ขผ | |
| if let bodyParameters = bodyParameters { | |
| try JSONParameterEncoder.encode(urlRequest: &request, with: bodyParameters) | |
| } | |
| // urlParameters ็บ URL ็ทจ็ขผ๏ผไฝฟ็จ JSONParameterEncoder ไพ็ทจ็ขผ | |
| if let urlParameters = urlParameters { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fileprivate func buildRequest(from route: EndPoint) throws -> URLRequest { | |
| // ๅปบ็ซ URLRequest ่ฎๆธ๏ผ้้ route.baseURL ๅพๆนๅ ไธ็นๅฎ่ทฏๅพ route.path ็ๆ | |
| var request = URLRequest(url: route.baseURL.appendingPathComponent(route.path), | |
| cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, | |
| timeoutInterval: 10.0) | |
| // ่จญๅฎ httpMethod ็บ route.httpMethod(enum) ็ rawValue | |
| request.httpMethod = route.httpMethod.rawValue | |
| do { | |
| // ๆ นๆ switch case ๅคๆท task request ้กๅ๏ผๅผๅซๅ้ฉ็ Encoder |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Router<EndPoint: EndPointType>: NetworkRouter { | |
| private var task: URLSessionTask? | |
| func request(_ route: EndPoint, completion: @escaping NetworkRouterCompletion) { | |
| } | |
| func cancel() { | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol NetworkRouter: class { | |
| associatedtype EndPoint: EndPointType | |
| func request(_ route: EndPoint, completion: @escaping NetworkRouterCompletion) | |
| func cancel() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public struct JSONParameterEncoder: ParameterEncoder { | |
| public static 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public struct URLParameterEncoder: ParameterEncoder { | |
| public static 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]() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public enum NetworkError: String, Error { | |
| case parametersNil = "Parameters were nil." | |
| case encodingFail = "Parameters encoding failed." | |
| case missingURL = "URL is nil." | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public protocol ParameterEncoder { | |
| static func encode(urlRequest: inout URLRequest, with parameters: Parameters) throws | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public enum HTTPTask { | |
| case request | |
| case requestWithParameters(bodyParameters: Parameters?, | |
| urlParameters: Parameters?) | |
| case requestWithParametersAndHeaders(bodyParameters: Parameters?, | |
| urlParameters: Parameters?, | |
| additionHeaders: HTTPHeaders?) |