mirror of
https://github.com/easychen/pushdeer.git
synced 2024-09-09 22:58:13 +08:00
29 lines
901 B
Swift
29 lines
901 B
Swift
import Foundation
|
|
|
|
internal extension URLRequest {
|
|
|
|
mutating func encoded(encodable: Encodable, encoder: JSONEncoder = JSONEncoder()) throws -> URLRequest {
|
|
do {
|
|
let encodable = AnyEncodable(encodable)
|
|
httpBody = try encoder.encode(encodable)
|
|
|
|
let contentTypeHeaderName = "Content-Type"
|
|
if value(forHTTPHeaderField: contentTypeHeaderName) == nil {
|
|
setValue("application/json", forHTTPHeaderField: contentTypeHeaderName)
|
|
}
|
|
|
|
return self
|
|
} catch {
|
|
throw MoyaError.encodableMapping(error)
|
|
}
|
|
}
|
|
|
|
func encoded(parameters: [String: Any], parameterEncoding: ParameterEncoding) throws -> URLRequest {
|
|
do {
|
|
return try parameterEncoding.encode(self, with: parameters)
|
|
} catch {
|
|
throw MoyaError.parameterEncoding(error)
|
|
}
|
|
}
|
|
}
|