Created
July 14, 2020 17:00
-
-
Save stevencurtis/8c1426b3d2fe354b765224b5d136f7f1 to your computer and use it in GitHub Desktop.
combinehttpmanager
This file contains 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 HTTPManager<T: URLSessionProtocol> { | |
/// A URLProtocol instance that is replaced by the URLSession in production code | |
let session: T | |
required init(session: T) { | |
self.session = session | |
} | |
public func post<T: Decodable>( | |
url: URL, | |
headers: [String : String], | |
data: Data | |
) | |
-> AnyPublisher<T, Error> | |
{ | |
var request = URLRequest( | |
url: url, | |
cachePolicy: .reloadIgnoringLocalCacheData, | |
timeoutInterval: 2.0) | |
request.httpMethod = "Post" | |
request.allHTTPHeaderFields = headers | |
request.httpBody = data | |
return session.response(for : request) | |
.map { $0.data } | |
.decode(type: T.self, | |
decoder: JSONDecoder()) | |
.mapError{ $0 } | |
.eraseToAnyPublisher() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment