2020-07-29 23:50:30 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
|
|
|
|
class MastodonClient: HTTPClient {
|
|
|
|
var instanceURL: URL?
|
|
|
|
var accessToken: String?
|
|
|
|
|
2020-08-21 02:29:01 +00:00
|
|
|
required init(environment: AppEnvironment) {
|
|
|
|
let decoder = MastodonDecoder()
|
|
|
|
|
|
|
|
decoder.userInfo[.attributedStringCache] = environment.attributedStringCache
|
|
|
|
|
|
|
|
super.init(session: environment.session, decoder: decoder)
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func request<T: DecodableTarget>(_ target: T) -> AnyPublisher<T.ResultType, Error> {
|
|
|
|
super.request(target, decodeErrorsAs: MastodonError.self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension MastodonClient {
|
|
|
|
func request<E: MastodonEndpoint>(_ endpoint: E) -> AnyPublisher<E.ResultType, Error> {
|
|
|
|
guard let instanceURL = instanceURL else {
|
|
|
|
return Fail(error: URLError(.badURL)).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.request(
|
|
|
|
MastodonTarget(baseURL: instanceURL, endpoint: endpoint, accessToken: accessToken),
|
|
|
|
decodeErrorsAs: MastodonError.self)
|
|
|
|
}
|
|
|
|
}
|