metatext/Mastodon/Sources/Mastodon/API/Endpoints/FilterEndpoint.swift

88 lines
2.4 KiB
Swift
Raw Normal View History

2020-08-29 10:26:26 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
2020-08-31 01:40:58 +00:00
import HTTP
2020-08-29 10:26:26 +00:00
2020-08-30 23:33:11 +00:00
public enum FilterEndpoint {
2020-08-29 10:26:26 +00:00
case create(
phrase: String,
context: [Filter.Context],
irreversible: Bool,
wholeWord: Bool,
expiresIn: Date?)
case update(
id: String,
phrase: String,
context: [Filter.Context],
irreversible: Bool,
wholeWord: Bool,
expiresIn: Date?)
}
2020-08-30 23:59:49 +00:00
extension FilterEndpoint: Endpoint {
2020-08-30 23:33:11 +00:00
public typealias ResultType = Filter
2020-08-29 10:26:26 +00:00
2020-08-30 23:33:11 +00:00
public var context: [String] {
2020-08-29 10:26:26 +00:00
defaultContext + ["filters"]
}
2020-08-30 23:33:11 +00:00
public var pathComponentsInContext: [String] {
2020-08-29 10:26:26 +00:00
switch self {
case .create:
return []
case let .update(id, _, _, _, _, _):
return [id]
}
}
2020-08-30 23:33:11 +00:00
public var parameters: [String: Any]? {
2020-08-29 10:26:26 +00:00
switch self {
case let .create(phrase, context, irreversible, wholeWord, expiresIn):
return params(phrase: phrase,
context: context,
irreversible: irreversible,
wholeWord: wholeWord,
expiresIn: expiresIn)
case let .update(id, phrase, context, irreversible, wholeWord, expiresIn):
var params = self.params(phrase: phrase,
context: context,
irreversible: irreversible,
wholeWord: wholeWord,
expiresIn: expiresIn)
params["id"] = id
return params
}
}
2020-08-30 23:33:11 +00:00
public var method: HTTPMethod {
2020-08-29 10:26:26 +00:00
switch self {
case .create:
return .post
case .update:
return .put
}
}
}
private extension FilterEndpoint {
func params(phrase: String,
context: [Filter.Context],
irreversible: Bool,
wholeWord: Bool,
expiresIn: Date?) -> [String: Any] {
var params: [String: Any] = [
"phrase": phrase,
"context": context.map(\.rawValue),
"irreversible": irreversible,
"whole_word": wholeWord]
if let expiresIn = expiresIn {
2020-08-30 00:32:34 +00:00
params["expires_in"] = Int(expiresIn.timeIntervalSinceNow)
2020-08-29 10:26:26 +00:00
}
return params
}
}