mirror of
https://github.com/metabolist/metatext.git
synced 2024-05-16 15:33:10 +00:00
31 lines
977 B
Swift
31 lines
977 B
Swift
|
// Copyright © 2020 Metabolist. All rights reserved.
|
||
|
|
||
|
import Foundation
|
||
|
import Combine
|
||
|
import Alamofire
|
||
|
|
||
|
class MastodonClient: HTTPClient {
|
||
|
var instanceURL: URL?
|
||
|
var accessToken: String?
|
||
|
|
||
|
init(configuration: URLSessionConfiguration = URLSessionConfiguration.af.default) {
|
||
|
super.init(configuration: configuration, decoder: MastodonDecoder())
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
}
|
||
|
}
|