2020-08-30 23:33:11 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Combine
|
2020-09-05 02:31:43 +00:00
|
|
|
import Foundation
|
2020-08-31 01:40:58 +00:00
|
|
|
import HTTP
|
2020-09-04 01:55:46 +00:00
|
|
|
import Mastodon
|
2020-08-30 23:33:11 +00:00
|
|
|
|
2020-09-04 01:55:46 +00:00
|
|
|
public final class MastodonAPIClient: Client {
|
2020-08-30 23:33:11 +00:00
|
|
|
public var instanceURL: URL?
|
|
|
|
public var accessToken: String?
|
|
|
|
|
|
|
|
public required init(session: Session) {
|
2020-09-04 01:55:46 +00:00
|
|
|
super.init(session: session, decoder: MastodonDecoder())
|
2020-08-30 23:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override func request<T: DecodableTarget>(_ target: T) -> AnyPublisher<T.ResultType, Error> {
|
2020-08-30 23:59:49 +00:00
|
|
|
super.request(target, decodeErrorsAs: APIError.self)
|
2020-08-30 23:33:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 01:55:46 +00:00
|
|
|
extension MastodonAPIClient {
|
2020-08-30 23:59:49 +00:00
|
|
|
public func request<E: Endpoint>(_ endpoint: E) -> AnyPublisher<E.ResultType, Error> {
|
2020-08-30 23:33:11 +00:00
|
|
|
guard let instanceURL = instanceURL else {
|
|
|
|
return Fail(error: URLError(.badURL)).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.request(
|
2020-09-04 01:55:46 +00:00
|
|
|
MastodonAPITarget(baseURL: instanceURL, endpoint: endpoint, accessToken: accessToken),
|
2020-08-30 23:59:49 +00:00
|
|
|
decodeErrorsAs: APIError.self)
|
2020-08-30 23:33:11 +00:00
|
|
|
}
|
|
|
|
}
|