metatext/MastodonAPI/Sources/MastodonAPI/MastodonAPIClient.swift

32 lines
961 B
Swift
Raw Normal View History

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
import Mastodon
2020-08-30 23:33:11 +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) {
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
}
}
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(
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
}
}