Skip to content

Instantly share code, notes, and snippets.

@stevencurtis
Created July 14, 2020 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevencurtis/8c1426b3d2fe354b765224b5d136f7f1 to your computer and use it in GitHub Desktop.
Save stevencurtis/8c1426b3d2fe354b765224b5d136f7f1 to your computer and use it in GitHub Desktop.
combinehttpmanager
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