Tusker/Pachyderm/Model/List.swift

54 lines
1.8 KiB
Swift

//
// List.swift
// Pachyderm
//
// Created by Shadowfacts on 9/9/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import Foundation
public class List: Decodable, ClientModel {
var client: Client!
public let id: String
public var title: String
public func getAccounts(range: RequestRange = .default, completion: @escaping Client.Callback<[Account]>) {
var request = Request<[Account]>(method: .get, path: "/api/v1/lists/\(id)/accounts")
request.range = range
client.run(request, completion: completion)
}
public func update(completion: Client.Callback<List>?) {
let request = Request<List>(method: .put, path: "/api/v1/lists/\(id)", body: .parameters(["title" => title]))
client.run(request) { result in
completion?(result)
}
}
public func delete(completion: @escaping Client.Callback<Empty>) {
let request = Request<Empty>(method: .delete, path: "/api/v1/lists/\(id)")
client.run(request, completion: completion)
}
public func add(accounts: [Account], completion: @escaping Client.Callback<Empty>) {
let request = Request<Empty>(method: .post, path: "/api/v1/lists/\(id)/accounts", body: .parameters(
"account_ids" => accounts.map { $0.id }
))
client.run(request, completion: completion)
}
public func remove(accounts: [Account], completion: @escaping Client.Callback<Empty>) {
let request = Request<Empty>(method: .delete, path: "/api/v1/lists/\(id)/accounts", body: .parameters(
"account_ids" => accounts.map { $0.id }
))
client.run(request, completion: completion)
}
private enum CodingKeys: String, CodingKey {
case id
case title
}
}