請求設置函數
private func handelHttpMethod <E: Encodable>(method: HttpMethod, path: ApiPathConstantse, parameter: E?) -> URLRequest {
let baseUrl = NewWorkConstants.httpsBaseUrl + NewWorkConstants.appServer + path.rawValue
let url = URL(string: baseUrl)
var urlRequest = URLRequest (url: url!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10)
let httpType = ContenType.json.rawValue
urlRequest.allHTTPHeaderFields = [HttpHeaderField.contentType.rawValue : httpType]
urlRequest.httpMethod = method.rawValue
let dicl = try? parameter.asDictionary()
switch method {
case .get:
let parameters = dicl as? [String : String]
urlRequest.url = requestwithURL(urlString: urlRequest.url?.absoluteString ?? "",parameters: parameters ?? [:])
default:
urlRequest.httpBody = try? JSONSerialization.data(withJSONObject: dicl ?? [:], options: .prettyPrinted)
}
return urlRequest
}
handelHttpMethod 函數是一個泛型函數,它接受三個參數: