|
|
|
@ -41,18 +41,18 @@ internal enum HTTP {
|
|
|
|
|
|
|
|
|
|
// MARK: Main
|
|
|
|
|
internal static func execute(_ verb: Verb, _ url: String, parameters: JSON? = nil, timeout: TimeInterval = HTTP.timeout) -> Promise<JSON> {
|
|
|
|
|
return Promise<JSON> { seal in
|
|
|
|
|
var request = URLRequest(url: URL(string: url)!)
|
|
|
|
|
request.httpMethod = verb.rawValue
|
|
|
|
|
if let parameters = parameters {
|
|
|
|
|
do {
|
|
|
|
|
guard JSONSerialization.isValidJSONObject(parameters) else { return seal.reject(Error.invalidJSON) }
|
|
|
|
|
guard JSONSerialization.isValidJSONObject(parameters) else { return Promise(error: Error.invalidJSON) }
|
|
|
|
|
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: [])
|
|
|
|
|
} catch (let error) {
|
|
|
|
|
return seal.reject(error)
|
|
|
|
|
return Promise(error: error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
request.timeoutInterval = timeout
|
|
|
|
|
let (promise, seal) = Promise<JSON>.pending()
|
|
|
|
|
let task = urlSession.dataTask(with: request) { data, response, error in
|
|
|
|
|
guard let data = data, let response = response as? HTTPURLResponse else {
|
|
|
|
|
if let error = error {
|
|
|
|
@ -88,6 +88,6 @@ internal enum HTTP {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
task.resume()
|
|
|
|
|
}
|
|
|
|
|
return promise
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|