Tusker/Pachyderm/Client.swift

424 lines
17 KiB
Swift
Raw Normal View History

2018-09-11 14:52:21 +00:00
//
// Client.swift
// Pachyderm
//
// Created by Shadowfacts on 9/8/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import Foundation
/**
The base Mastodon API client.
*/
public class Client {
public typealias Callback<Result: Decodable> = (Response<Result>) -> Void
let baseURL: URL
let session: URLSession
public var accessToken: String?
public var appID: String?
public var clientID: String?
public var clientSecret: String?
public var timeoutInterval: TimeInterval = 60
2020-09-15 03:25:26 +00:00
static let decoder: JSONDecoder = {
2018-09-11 14:52:21 +00:00
let decoder = JSONDecoder()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SZ"
formatter.timeZone = TimeZone(abbreviation: "UTC")
formatter.locale = Locale(identifier: "en_US_POSIX")
let iso8601 = ISO8601DateFormatter()
decoder.dateDecodingStrategy = .custom({ (decoder) in
let container = try decoder.singleValueContainer()
let str = try container.decode(String.self)
// for the next time mastodon accidentally changes date formats >.>
if let date = formatter.date(from: str) {
return date
} else if let date = iso8601.date(from: str) {
return date
} else {
throw DecodingError.typeMismatch(Date.self, .init(codingPath: container.codingPath, debugDescription: "unexpected date format"))
}
})
2018-09-11 14:52:21 +00:00
return decoder
}()
2020-09-15 03:25:26 +00:00
static let encoder: JSONEncoder = {
let encoder = JSONEncoder()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SZ"
formatter.timeZone = TimeZone(abbreviation: "UTC")
formatter.locale = Locale(identifier: "en_US_POSIX")
encoder.dateEncodingStrategy = .formatted(formatter)
return encoder
}()
2018-09-11 14:52:21 +00:00
public init(baseURL: URL, accessToken: String? = nil, session: URLSession = .shared) {
self.baseURL = baseURL
self.accessToken = accessToken
self.session = session
}
@discardableResult
public func run<Result>(_ request: Request<Result>, completion: @escaping Callback<Result>) -> URLSessionTask? {
2018-09-11 14:52:21 +00:00
guard let request = createURLRequest(request: request) else {
completion(.failure(Error.invalidRequest))
return nil
2018-09-11 14:52:21 +00:00
}
let task = session.dataTask(with: request) { data, response, error in
if let error = error {
2020-09-09 22:33:59 +00:00
completion(.failure(.networkError(error)))
2018-09-11 14:52:21 +00:00
return
}
guard let data = data,
let response = response as? HTTPURLResponse else {
2020-09-09 22:33:59 +00:00
completion(.failure(.invalidResponse))
2018-09-11 14:52:21 +00:00
return
}
guard response.statusCode == 200 else {
2020-09-15 03:25:26 +00:00
let mastodonError = try? Client.decoder.decode(MastodonError.self, from: data)
2020-09-09 22:33:59 +00:00
let error: Error = mastodonError.flatMap { .mastodonError($0.description) } ?? .unexpectedStatus(response.statusCode)
2018-09-11 14:52:21 +00:00
completion(.failure(error))
return
}
2020-09-15 03:25:26 +00:00
guard let result = try? Client.decoder.decode(Result.self, from: data) else {
2020-09-09 22:33:59 +00:00
completion(.failure(.invalidModel))
2018-09-11 14:52:21 +00:00
return
}
let pagination = response.allHeaderFields["Link"].flatMap { $0 as? String }.flatMap(Pagination.init)
completion(.success(result, pagination))
}
task.resume()
return task
2018-09-11 14:52:21 +00:00
}
func createURLRequest<Result>(request: Request<Result>) -> URLRequest? {
guard var components = URLComponents(url: baseURL, resolvingAgainstBaseURL: true) else { return nil }
2018-09-11 14:52:21 +00:00
components.path = request.path
2020-11-01 18:59:58 +00:00
components.queryItems = request.queryParameters.isEmpty ? nil : request.queryParameters.queryItems
2018-09-11 14:52:21 +00:00
guard let url = components.url else { return nil }
var urlRequest = URLRequest(url: url, timeoutInterval: timeoutInterval)
urlRequest.httpMethod = request.method.name
urlRequest.httpBody = request.body.data
if let mimeType = request.body.mimeType {
urlRequest.setValue(mimeType, forHTTPHeaderField: "Content-Type")
}
2018-09-11 14:52:21 +00:00
if let accessToken = accessToken {
urlRequest.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
}
return urlRequest
}
// MARK: - Authorization
public func registerApp(name: String, redirectURI: String, scopes: [Scope], website: URL? = nil, completion: @escaping Callback<RegisteredApplication>) {
2020-09-15 03:25:26 +00:00
let request = Request<RegisteredApplication>(method: .post, path: "/api/v1/apps", body: ParametersBody([
2018-09-11 14:52:21 +00:00
"client_name" => name,
"redirect_uris" => redirectURI,
"scopes" => scopes.scopeString,
"website" => website?.absoluteString
]))
run(request) { result in
defer { completion(result) }
guard case let .success(application, _) = result else { return }
self.appID = application.id
self.clientID = application.clientID
self.clientSecret = application.clientSecret
}
}
public func getAccessToken(authorizationCode: String, redirectURI: String, completion: @escaping Callback<LoginSettings>) {
2020-09-15 03:25:26 +00:00
let request = Request<LoginSettings>(method: .post, path: "/oauth/token", body: ParametersBody([
2018-09-11 14:52:21 +00:00
"client_id" => clientID,
"client_secret" => clientSecret,
"grant_type" => "authorization_code",
"code" => authorizationCode,
"redirect_uri" => redirectURI
]))
run(request) { result in
defer { completion(result) }
guard case let .success(loginSettings, _) = result else { return }
self.accessToken = loginSettings.accessToken
}
}
public func nodeInfo(completion: @escaping Callback<NodeInfo>) {
let wellKnown = Request<WellKnown>(method: .get, path: "/.well-known/nodeinfo")
run(wellKnown) { result in
switch result {
case let .failure(error):
completion(.failure(error))
case let .success(wellKnown, _):
if let url = wellKnown.links.first(where: { $0.rel == "http://nodeinfo.diaspora.software/ns/schema/2.0" }),
let components = URLComponents(string: url.href),
components.host == self.baseURL.host {
let nodeInfo = Request<NodeInfo>(method: .get, path: components.path)
self.run(nodeInfo, completion: completion)
}
}
}
}
2018-09-11 14:52:21 +00:00
// MARK: - Self
public static func getSelfAccount() -> Request<Account> {
2018-09-18 00:58:05 +00:00
return Request<Account>(method: .get, path: "/api/v1/accounts/verify_credentials")
2018-09-11 14:52:21 +00:00
}
public static func getFavourites() -> Request<[Status]> {
2018-09-18 00:58:05 +00:00
return Request<[Status]>(method: .get, path: "/api/v1/favourites")
2018-09-11 14:52:21 +00:00
}
public static func getRelationships(accounts: [String]? = nil) -> Request<[Relationship]> {
2018-09-24 12:49:39 +00:00
return Request<[Relationship]>(method: .get, path: "/api/v1/accounts/relationships", queryParameters: "id" => accounts)
2018-09-11 14:52:21 +00:00
}
public static func getInstance() -> Request<Instance> {
2018-09-18 00:58:05 +00:00
return Request<Instance>(method: .get, path: "/api/v1/instance")
2018-09-11 14:52:21 +00:00
}
public static func getCustomEmoji() -> Request<[Emoji]> {
2018-09-18 00:58:05 +00:00
return Request<[Emoji]>(method: .get, path: "/api/v1/custom_emojis")
2018-09-11 14:52:21 +00:00
}
// MARK: - Accounts
public static func getAccount(id: String) -> Request<Account> {
2018-09-18 00:58:05 +00:00
return Request<Account>(method: .get, path: "/api/v1/accounts/\(id)")
2018-09-11 14:52:21 +00:00
}
public static func searchForAccount(query: String, limit: Int? = nil, following: Bool? = nil) -> Request<[Account]> {
2018-09-18 00:58:05 +00:00
return Request<[Account]>(method: .get, path: "/api/v1/accounts/search", queryParameters: [
2018-09-11 14:52:21 +00:00
"q" => query,
"limit" => limit,
"following" => following
])
}
// MARK: - Blocks
public static func getBlocks() -> Request<[Account]> {
2018-09-18 00:58:05 +00:00
return Request<[Account]>(method: .get, path: "/api/v1/blocks")
2018-09-11 14:52:21 +00:00
}
public static func getDomainBlocks() -> Request<[String]> {
2018-09-18 00:58:05 +00:00
return Request<[String]>(method: .get, path: "api/v1/domain_blocks")
2018-09-11 14:52:21 +00:00
}
public static func block(domain: String) -> Request<Empty> {
2020-09-15 03:25:26 +00:00
return Request<Empty>(method: .post, path: "/api/v1/domain_blocks", body: ParametersBody([
2018-09-11 14:52:21 +00:00
"domain" => domain
]))
}
public static func unblock(domain: String) -> Request<Empty> {
2020-09-15 03:25:26 +00:00
return Request<Empty>(method: .delete, path: "/api/v1/domain_blocks", body: ParametersBody([
2018-09-11 14:52:21 +00:00
"domain" => domain
]))
}
// MARK: - Filters
public static func getFilters() -> Request<[Filter]> {
2018-09-18 00:58:05 +00:00
return Request<[Filter]>(method: .get, path: "/api/v1/filters")
2018-09-11 14:52:21 +00:00
}
public static func createFilter(phrase: String, context: [Filter.Context], irreversible: Bool? = nil, wholeWord: Bool? = nil, expiresAt: Date? = nil) -> Request<Filter> {
2020-09-15 03:25:26 +00:00
return Request<Filter>(method: .post, path: "/api/v1/filters", body: ParametersBody([
2018-09-11 14:52:21 +00:00
"phrase" => phrase,
"irreversible" => irreversible,
"whole_word" => wholeWord,
"expires_at" => expiresAt
] + "context" => context.contextStrings))
}
public static func getFilter(id: String) -> Request<Filter> {
2018-09-18 00:58:05 +00:00
return Request<Filter>(method: .get, path: "/api/v1/filters/\(id)")
2018-09-11 14:52:21 +00:00
}
// MARK: - Follows
public static func getFollowRequests(range: RequestRange = .default) -> Request<[Account]> {
2018-09-11 14:52:21 +00:00
var request = Request<[Account]>(method: .get, path: "/api/v1/follow_requests")
request.range = range
2018-09-18 00:58:05 +00:00
return request
2018-09-11 14:52:21 +00:00
}
public static func getFollowSuggestions() -> Request<[Account]> {
2018-09-18 00:58:05 +00:00
return Request<[Account]>(method: .get, path: "/api/v1/suggestions")
2018-09-11 14:52:21 +00:00
}
public static func followRemote(acct: String) -> Request<Account> {
2020-09-15 03:25:26 +00:00
return Request<Account>(method: .post, path: "/api/v1/follows", body: ParametersBody(["uri" => acct]))
2018-09-11 14:52:21 +00:00
}
// MARK: - Lists
public static func getLists() -> Request<[List]> {
2018-09-18 00:58:05 +00:00
return Request<[List]>(method: .get, path: "/api/v1/lists")
2018-09-11 14:52:21 +00:00
}
public static func getList(id: String) -> Request<List> {
2018-09-18 00:58:05 +00:00
return Request<List>(method: .get, path: "/api/v1/lists/\(id)")
2018-09-11 14:52:21 +00:00
}
public static func createList(title: String) -> Request<List> {
2020-09-15 03:25:26 +00:00
return Request<List>(method: .post, path: "/api/v1/lists", body: ParametersBody(["title" => title]))
2018-09-11 14:52:21 +00:00
}
// MARK: - Media
public static func upload(attachment: FormAttachment, description: String? = nil, focus: (Float, Float)? = nil) -> Request<Attachment> {
2020-09-15 03:25:26 +00:00
return Request<Attachment>(method: .post, path: "/api/v1/media", body: FormDataBody([
2018-09-11 14:52:21 +00:00
"description" => description,
"focus" => focus
], attachment))
}
// MARK: - Mutes
public static func getMutes(range: RequestRange) -> Request<[Account]> {
2018-09-11 14:52:21 +00:00
var request = Request<[Account]>(method: .get, path: "/api/v1/mutes")
request.range = range
2018-09-18 00:58:05 +00:00
return request
2018-09-11 14:52:21 +00:00
}
// MARK: - Notifications
public static func getNotifications(excludeTypes: [Notification.Kind], range: RequestRange = .default) -> Request<[Notification]> {
var request = Request<[Notification]>(method: .get, path: "/api/v1/notifications", queryParameters:
"exclude_types" => excludeTypes.map { $0.rawValue }
)
2018-09-11 14:52:21 +00:00
request.range = range
2018-09-18 00:58:05 +00:00
return request
2018-09-11 14:52:21 +00:00
}
public static func clearNotifications() -> Request<Empty> {
2018-09-18 00:58:05 +00:00
return Request<Empty>(method: .post, path: "/api/v1/notifications/clear")
2018-09-11 14:52:21 +00:00
}
// MARK: - Reports
public static func getReports() -> Request<[Report]> {
2018-09-18 00:58:05 +00:00
return Request<[Report]>(method: .get, path: "/api/v1/reports")
2018-09-11 14:52:21 +00:00
}
public static func report(account: Account, statuses: [Status], comment: String) -> Request<Report> {
2020-09-15 03:25:26 +00:00
return Request<Report>(method: .post, path: "/api/v1/reports", body: ParametersBody([
2018-09-11 14:52:21 +00:00
"account_id" => account.id,
"comment" => comment
] + "status_ids" => statuses.map { $0.id }))
}
// MARK: - Search
public static func search(query: String, types: [SearchResultType]? = nil, resolve: Bool? = nil, limit: Int? = nil) -> Request<SearchResults> {
2018-09-18 00:58:05 +00:00
return Request<SearchResults>(method: .get, path: "/api/v2/search", queryParameters: [
2018-09-11 14:52:21 +00:00
"q" => query,
2019-09-15 00:47:08 +00:00
"resolve" => resolve,
"limit" => limit,
] + "types" => types?.map { $0.rawValue })
2018-09-11 14:52:21 +00:00
}
// MARK: - Statuses
public static func getStatus(id: String) -> Request<Status> {
2018-09-18 00:58:05 +00:00
return Request<Status>(method: .get, path: "/api/v1/statuses/\(id)")
2018-09-11 14:52:21 +00:00
}
public static func createStatus(text: String,
contentType: StatusContentType = .plain,
inReplyTo: String? = nil,
media: [Attachment]? = nil,
sensitive: Bool? = nil,
spoilerText: String? = nil,
visibility: Status.Visibility? = nil,
2021-05-04 03:12:59 +00:00
language: String? = nil,
pollOptions: [String]? = nil,
pollExpiresIn: Int? = nil,
pollMultiple: Bool? = nil,
localOnly: Bool? = nil /* hometown only, not glitch */) -> Request<Status> {
2020-09-15 03:25:26 +00:00
return Request<Status>(method: .post, path: "/api/v1/statuses", body: ParametersBody([
2018-09-11 14:52:21 +00:00
"status" => text,
2019-01-14 00:46:51 +00:00
"content_type" => contentType.mimeType,
2018-09-18 01:57:46 +00:00
"in_reply_to_id" => inReplyTo,
2018-09-11 14:52:21 +00:00
"sensitive" => sensitive,
"spoiler_text" => spoilerText,
2018-09-24 01:10:45 +00:00
"visibility" => visibility?.rawValue,
2021-05-04 03:12:59 +00:00
"language" => language,
"poll[expires_in]" => pollExpiresIn,
"poll[multiple]" => pollMultiple,
"local_only" => localOnly,
2021-05-04 03:12:59 +00:00
] + "media_ids" => media?.map { $0.id } + "poll[options]" => pollOptions))
2018-09-11 14:52:21 +00:00
}
// MARK: - Timelines
public static func getStatuses(timeline: Timeline, range: RequestRange = .default) -> Request<[Status]> {
2018-09-18 00:58:05 +00:00
return timeline.request(range: range)
2018-09-11 14:52:21 +00:00
}
// MARK: - Bookmarks
public static func getBookmarks(range: RequestRange = .default) -> Request<[Status]> {
var request = Request<[Status]>(method: .get, path: "/api/v1/bookmarks")
request.range = range
return request
}
2021-02-08 00:39:22 +00:00
// MARK: - Instance
public static func getTrends(limit: Int? = nil) -> Request<[Hashtag]> {
let parameters: [Parameter]
if let limit = limit {
parameters = ["limit" => limit]
} else {
parameters = []
}
return Request<[Hashtag]>(method: .get, path: "/api/v1/trends", queryParameters: parameters)
}
2021-02-08 00:39:22 +00:00
public static func getFeaturedProfiles(local: Bool, order: DirectoryOrder, offset: Int? = nil, limit: Int? = nil) -> Request<[Account]> {
var parameters = [
"order" => order.rawValue,
"local" => local,
]
if let offset = offset {
parameters.append("offset" => offset)
}
if let limit = limit {
parameters.append("limit" => limit)
}
return Request<[Account]>(method: .get, path: "/api/v1/directory", queryParameters: parameters)
}
2018-09-11 14:52:21 +00:00
}
extension Client {
2020-08-31 23:28:50 +00:00
public enum Error: LocalizedError {
2020-09-09 22:33:59 +00:00
case networkError(Swift.Error)
case unexpectedStatus(Int)
2018-09-11 14:52:21 +00:00
case invalidRequest
case invalidResponse
case invalidModel
case mastodonError(String)
2020-08-31 23:28:50 +00:00
public var localizedDescription: String {
switch self {
2020-09-09 22:33:59 +00:00
case .networkError(let error):
return "Network Error: \(error.localizedDescription)"
// todo: support more status codes
case .unexpectedStatus(413):
return "HTTP 413: Payload Too Large"
case .unexpectedStatus(let code):
return "HTTP Code \(code)"
2020-08-31 23:28:50 +00:00
case .invalidRequest:
return "Invalid Request"
case .invalidResponse:
return "Invalid Response"
case .invalidModel:
return "Invalid Model"
case .mastodonError(let error):
return "Server Error: \(error)"
}
}
2018-09-11 14:52:21 +00:00
}
}