Change client requests to match others

This commit is contained in:
Shadowfacts 2018-09-17 20:58:05 -04:00
parent c4fb93579c
commit 6518ae236a
5 changed files with 83 additions and 102 deletions

View File

@ -130,180 +130,155 @@ public class Client {
} }
// MARK: - Self // MARK: - Self
public func getSelfAccount(completion: @escaping Callback<Account>) { public func getSelfAccount() -> Request<Account> {
let request = Request<Account>(method: .get, path: "/api/v1/accounts/verify_credentials") return Request<Account>(method: .get, path: "/api/v1/accounts/verify_credentials")
run(request, completion: completion)
} }
public func getFavourites(completion: @escaping Callback<[Status]>) { public func getFavourites() -> Request<[Status]> {
let request = Request<[Status]>(method: .get, path: "/api/v1/favourites") return Request<[Status]>(method: .get, path: "/api/v1/favourites")
run(request, completion: completion)
} }
public func getRelationships(accounts: [Account]? = nil, completion: @escaping Callback<[Relationship]>) { public func getRelationships(accounts: [Account]? = nil) -> Request<[Relationship]> {
let request = Request<[Relationship]>(method: .get, path: "/api/v1/accounts/relationships", queryParameters: "id" => accounts?.map { $0.id }) return Request<[Relationship]>(method: .get, path: "/api/v1/accounts/relationships", queryParameters: "id" => accounts?.map { $0.id })
run(request, completion: completion)
} }
public func getInstance(completion: @escaping Callback<Instance>) { public func getInstance() -> Request<Instance> {
let request = Request<Instance>(method: .get, path: "/api/v1/instance") return Request<Instance>(method: .get, path: "/api/v1/instance")
run(request, completion: completion)
} }
public func getCustomEmoji(completion: @escaping Callback<[Emoji]>) { public func getCustomEmoji() -> Request<[Emoji]> {
let request = Request<[Emoji]>(method: .get, path: "/api/v1/custom_emojis") return Request<[Emoji]>(method: .get, path: "/api/v1/custom_emojis")
run(request, completion: completion)
} }
// MARK: - Accounts // MARK: - Accounts
public func getAccount(id: String, completion: @escaping Callback<Account>) { public func getAccount(id: String) -> Request<Account> {
let request = Request<Account>(method: .get, path: "/api/v1/accounts/\(id)") return Request<Account>(method: .get, path: "/api/v1/accounts/\(id)")
run(request, completion: completion)
} }
public func searchForAccount(query: String, limit: Int? = nil, following: Bool? = nil, completion: @escaping Callback<[Account]>) { public func searchForAccount(query: String, limit: Int? = nil, following: Bool? = nil) -> Request<[Account]> {
let request = Request<[Account]>(method: .get, path: "/api/v1/accounts/search", queryParameters: [ return Request<[Account]>(method: .get, path: "/api/v1/accounts/search", queryParameters: [
"q" => query, "q" => query,
"limit" => limit, "limit" => limit,
"following" => following "following" => following
]) ])
run(request, completion: completion)
} }
// MARK: - Blocks // MARK: - Blocks
public func getBlocks(completion: @escaping Callback<[Account]>) { public func getBlocks() -> Request<[Account]> {
let request = Request<[Account]>(method: .get, path: "/api/v1/blocks") return Request<[Account]>(method: .get, path: "/api/v1/blocks")
run(request, completion: completion)
} }
public func getDomainBlocks(completion: @escaping Callback<[String]>) { public func getDomainBlocks() -> Request<[String]> {
let request = Request<[String]>(method: .get, path: "api/v1/domain_blocks") return Request<[String]>(method: .get, path: "api/v1/domain_blocks")
run(request, completion: completion)
} }
public func block(domain: String, completion: @escaping Callback<Empty>) { public func block(domain: String) -> Request<Empty> {
let request = Request<Empty>(method: .post, path: "/api/v1/domain_blocks", body: .parameters([ return Request<Empty>(method: .post, path: "/api/v1/domain_blocks", body: .parameters([
"domain" => domain "domain" => domain
])) ]))
run(request, completion: completion)
} }
public func unblock(domain: String, completion: @escaping Callback<Empty>) { public func unblock(domain: String) -> Request<Empty> {
let request = Request<Empty>(method: .delete, path: "/api/v1/domain_blocks", body: .parameters([ return Request<Empty>(method: .delete, path: "/api/v1/domain_blocks", body: .parameters([
"domain" => domain "domain" => domain
])) ]))
run(request, completion: completion)
} }
// MARK: - Filters // MARK: - Filters
public func getFilters(completion: @escaping Callback<[Filter]>) { public func getFilters() -> Request<[Filter]> {
let request = Request<[Filter]>(method: .get, path: "/api/v1/filters") return Request<[Filter]>(method: .get, path: "/api/v1/filters")
run(request, completion: completion)
} }
public func createFilter(phrase: String, context: [Filter.Context], irreversible: Bool? = nil, wholeWord: Bool? = nil, expiresAt: Date? = nil, completion: @escaping Callback<Filter>) { public func createFilter(phrase: String, context: [Filter.Context], irreversible: Bool? = nil, wholeWord: Bool? = nil, expiresAt: Date? = nil) -> Request<Filter> {
let request = Request<Filter>(method: .post, path: "/api/v1/filters", body: .parameters([ return Request<Filter>(method: .post, path: "/api/v1/filters", body: .parameters([
"phrase" => phrase, "phrase" => phrase,
"irreversible" => irreversible, "irreversible" => irreversible,
"whole_word" => wholeWord, "whole_word" => wholeWord,
"expires_at" => expiresAt "expires_at" => expiresAt
] + "context" => context.contextStrings)) ] + "context" => context.contextStrings))
run(request, completion: completion)
} }
public func getFilter(id: String, completion: @escaping Callback<Filter>) { public func getFilter(id: String) -> Request<Filter> {
let request = Request<Filter>(method: .get, path: "/api/v1/filters/\(id)") return Request<Filter>(method: .get, path: "/api/v1/filters/\(id)")
run(request, completion: completion)
} }
// MARK: - Follows // MARK: - Follows
public func getFollowRequests(range: RequestRange = .default, completion: @escaping Callback<[Account]>) { public func getFollowRequests(range: RequestRange = .default) -> Request<[Account]> {
var request = Request<[Account]>(method: .get, path: "/api/v1/follow_requests") var request = Request<[Account]>(method: .get, path: "/api/v1/follow_requests")
request.range = range request.range = range
run(request, completion: completion) return request
} }
public func getFollowSuggestions(completion: @escaping Callback<[Account]>) { public func getFollowSuggestions() -> Request<[Account]> {
let request = Request<[Account]>(method: .get, path: "/api/v1/suggestions") return Request<[Account]>(method: .get, path: "/api/v1/suggestions")
run(request, completion: completion)
} }
public func followRemote(acct: String, completion: @escaping Callback<Account>) { public func followRemote(acct: String) -> Request<Account> {
let request = Request<Account>(method: .post, path: "/api/v1/follows", body: .parameters(["uri" => acct])) return Request<Account>(method: .post, path: "/api/v1/follows", body: .parameters(["uri" => acct]))
run(request, completion: completion)
} }
// MARK: - Lists // MARK: - Lists
public func getLists(completion: @escaping Callback<[List]>) { public func getLists() -> Request<[List]> {
let request = Request<[List]>(method: .get, path: "/api/v1/lists") return Request<[List]>(method: .get, path: "/api/v1/lists")
run(request, completion: completion)
} }
public func getList(id: String, completion: @escaping Callback<List>) { public func getList(id: String) -> Request<List> {
let request = Request<List>(method: .get, path: "/api/v1/lists/\(id)") return Request<List>(method: .get, path: "/api/v1/lists/\(id)")
run(request, completion: completion)
} }
public func createList(title: String, completion: @escaping Callback<List>) { public func createList(title: String) -> Request<List> {
let request = Request<List>(method: .post, path: "/api/v1/lists", body: .parameters(["title" => title])) return Request<List>(method: .post, path: "/api/v1/lists", body: .parameters(["title" => title]))
run(request, completion: completion)
} }
// MARK: - Media // MARK: - Media
public func upload(attachment: FormAttachment, description: String? = nil, focus: (Float, Float)? = nil, completion: @escaping Callback<Attachment>) { public func upload(attachment: FormAttachment, description: String? = nil, focus: (Float, Float)? = nil) -> Request<Attachment> {
let request = Request<Attachment>(method: .post, path: "/api/v1/media", body: .formData([ return Request<Attachment>(method: .post, path: "/api/v1/media", body: .formData([
"description" => description, "description" => description,
"focus" => focus "focus" => focus
], attachment)) ], attachment))
run(request, completion: completion)
} }
// MARK: - Mutes // MARK: - Mutes
public func getMutes(range: RequestRange, completion: @escaping Callback<[Account]>) { public func getMutes(range: RequestRange) -> Request<[Account]> {
var request = Request<[Account]>(method: .get, path: "/api/v1/mutes") var request = Request<[Account]>(method: .get, path: "/api/v1/mutes")
request.range = range request.range = range
run(request, completion: completion) return request
} }
// MARK: - Notifications // MARK: - Notifications
public func getNotifications(range: RequestRange = .default, completion: @escaping Callback<[Notification]>) { public func getNotifications(range: RequestRange = .default) -> Request<[Notification]> {
var request = Request<[Notification]>(method: .get, path: "/api/v1/notifications") var request = Request<[Notification]>(method: .get, path: "/api/v1/notifications")
request.range = range request.range = range
run(request, completion: completion) return request
} }
public func clearNotifications(completion: @escaping Callback<Empty>) { public func clearNotifications() -> Request<Empty> {
let request = Request<Empty>(method: .post, path: "/api/v1/notifications/clear") return Request<Empty>(method: .post, path: "/api/v1/notifications/clear")
run(request, completion: completion)
} }
// MARK: - Reports // MARK: - Reports
public func getReports(completion: @escaping Callback<[Report]>) { public func getReports() -> Request<[Report]> {
let request = Request<[Report]>(method: .get, path: "/api/v1/reports") return Request<[Report]>(method: .get, path: "/api/v1/reports")
run(request, completion: completion)
} }
public func report(account: Account, statuses: [Status], comment: String, completion: @escaping Callback<Report>) { public func report(account: Account, statuses: [Status], comment: String) -> Request<Report> {
let request = Request<Report>(method: .post, path: "/api/v1/reports", body: .parameters([ return Request<Report>(method: .post, path: "/api/v1/reports", body: .parameters([
"account_id" => account.id, "account_id" => account.id,
"comment" => comment "comment" => comment
] + "status_ids" => statuses.map { $0.id })) ] + "status_ids" => statuses.map { $0.id }))
run(request, completion: completion)
} }
// MARK: - Search // MARK: - Search
public func search(query: String, resolve: Bool? = nil, completion: @escaping Callback<SearchResults>) { public func search(query: String, resolve: Bool? = nil) -> Request<SearchResults> {
let request = Request<SearchResults>(method: .get, path: "/api/v2/search", queryParameters: [ return Request<SearchResults>(method: .get, path: "/api/v2/search", queryParameters: [
"q" => query, "q" => query,
"resolve" => resolve "resolve" => resolve
]) ])
run(request, completion: completion)
} }
// MARK: - Statuses // MARK: - Statuses
public func getStatus(id: String, completion: @escaping Callback<Status>) { public func getStatus(id: String) -> Request<Status> {
let request = Request<Status>(method: .get, path: "/api/v1/statuses/\(id)") return Request<Status>(method: .get, path: "/api/v1/statuses/\(id)")
run(request, completion: completion)
} }
public func createStatus(text: String, public func createStatus(text: String,
@ -312,9 +287,8 @@ public class Client {
sensitive: Bool? = nil, sensitive: Bool? = nil,
spoilerText: String? = nil, spoilerText: String? = nil,
visiblity: Status.Visibility? = nil, visiblity: Status.Visibility? = nil,
language: String? = nil, language: String? = nil) -> Request<Status> {
completion: @escaping Callback<Status>) { return Request<Status>(method: .post, path: "/api/v1/statuses", body: .parameters([
let request = Request<Status>(method: .post, path: "/api/v1/statuses", body: .parameters([
"status" => text, "status" => text,
"in_reply_to_id" => inReplyTo?.id, "in_reply_to_id" => inReplyTo?.id,
"sensitive" => sensitive, "sensitive" => sensitive,
@ -322,13 +296,11 @@ public class Client {
"visibility" => visiblity?.rawValue, "visibility" => visiblity?.rawValue,
"language" => language "language" => language
] + "media_ids" => media?.map { $0.id })) ] + "media_ids" => media?.map { $0.id }))
run(request, completion: completion)
} }
// MARK: - Timelines // MARK: - Timelines
public func getStatuses(timeline: Timeline, range: RequestRange = .default, completion: @escaping Callback<[Status]>) { public func getStatuses(timeline: Timeline, range: RequestRange = .default) -> Request<[Status]> {
let request = timeline.request(range: range) return timeline.request(range: range)
run(request, completion: completion)
} }
} }

View File

@ -54,7 +54,8 @@ class MastodonController {
} }
func getOwnAccount() { func getOwnAccount() {
client.getSelfAccount { response in let request = client.getSelfAccount()
client.run(request) { response in
guard case let .success(account, _) = response else { fatalError() } guard case let .success(account, _) = response else { fatalError() }
self.account = account self.account = account
} }

View File

@ -203,7 +203,8 @@ class ComposeViewController: UIViewController {
attachments.append(nil) attachments.append(nil)
progressView.steps += 1 progressView.steps += 1
group.enter() group.enter()
MastodonController.shared.client.upload(attachment: FormAttachment(pngData: data), description: mediaView.mediaDescription) { response in let request = MastodonController.shared.client.upload(attachment: FormAttachment(pngData: data), description: mediaView.mediaDescription)
MastodonController.shared.client.run(request) { response in
guard case let .success(attachment, _) = response else { fatalError() } guard case let .success(attachment, _) = response else { fatalError() }
attachments[index] = attachment attachments[index] = attachment
self.progressView.step() self.progressView.step()
@ -217,12 +218,13 @@ class ComposeViewController: UIViewController {
group.notify(queue: .main) { group.notify(queue: .main) {
let attachments = attachments.compactMap { $0 } let attachments = attachments.compactMap { $0 }
MastodonController.shared.client.createStatus(text: text, let request = MastodonController.shared.client.createStatus(text: text,
inReplyTo: self.inReplyTo, inReplyTo: self.inReplyTo,
media: attachments, media: attachments,
sensitive: sensitive, sensitive: sensitive,
spoilerText: contentWarning, spoilerText: contentWarning,
visiblity: visibility) { response in visiblity: visibility)
MastodonController.shared.client.run(request) { response in
guard case let .success(status, _) = response else { fatalError() } guard case let .success(status, _) = response else { fatalError() }
self.status = status self.status = status
DispatchQueue.main.async { DispatchQueue.main.async {

View File

@ -43,7 +43,8 @@ class NotificationsTableViewController: UITableViewController {
tableView.register(UINib(nibName: "ActionNotificationTableViewCell", bundle: nil), forCellReuseIdentifier: "actionCell") tableView.register(UINib(nibName: "ActionNotificationTableViewCell", bundle: nil), forCellReuseIdentifier: "actionCell")
tableView.register(UINib(nibName: "FollowNotificationTableViewCell", bundle: nil), forCellReuseIdentifier: "followCell") tableView.register(UINib(nibName: "FollowNotificationTableViewCell", bundle: nil), forCellReuseIdentifier: "followCell")
MastodonController.shared.client.getNotifications() { result in let request = MastodonController.shared.client.getNotifications()
MastodonController.shared.client.run(request) { result in
guard case let .success(notifications, pagination) = result else { fatalError() } guard case let .success(notifications, pagination) = result else { fatalError() }
self.notifications = notifications self.notifications = notifications
self.newer = pagination?.newer self.newer = pagination?.newer
@ -110,7 +111,8 @@ class NotificationsTableViewController: UITableViewController {
if indexPath.row == notifications.count - 1 { if indexPath.row == notifications.count - 1 {
guard let older = older else { return } guard let older = older else { return }
MastodonController.shared.client.getNotifications(range: older) { result in let request = MastodonController.shared.client.getNotifications(range: older)
MastodonController.shared.client.run(request) { result in
guard case let .success(newNotifications, pagination) = result else { fatalError() } guard case let .success(newNotifications, pagination) = result else { fatalError() }
self.older = pagination?.older self.older = pagination?.older
self.notifications.append(contentsOf: newNotifications) self.notifications.append(contentsOf: newNotifications)
@ -133,7 +135,8 @@ class NotificationsTableViewController: UITableViewController {
@IBAction func refreshNotifications(_ sender: Any) { @IBAction func refreshNotifications(_ sender: Any) {
guard let newer = newer else { return } guard let newer = newer else { return }
MastodonController.shared.client.getNotifications(range: newer) { result in let request = MastodonController.shared.client.getNotifications(range: newer)
MastodonController.shared.client.run(request) { result in
guard case let .success(newNotifications, pagination) = result else { fatalError() } guard case let .success(newNotifications, pagination) = result else { fatalError() }
self.newer = pagination?.newer self.newer = pagination?.newer
self.notifications.insert(contentsOf: newNotifications, at: 0) self.notifications.insert(contentsOf: newNotifications, at: 0)

View File

@ -70,7 +70,8 @@ class TimelineTableViewController: UITableViewController {
tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell") tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell")
guard MastodonController.shared.client?.accessToken != nil else { return } guard MastodonController.shared.client?.accessToken != nil else { return }
MastodonController.shared.client.getStatuses(timeline: timeline) { response in let request = MastodonController.shared.client.getStatuses(timeline: timeline)
MastodonController.shared.client.run(request) { response in
guard case let .success(statuses, pagination) = response else { fatalError() } guard case let .success(statuses, pagination) = response else { fatalError() }
self.statuses = statuses self.statuses = statuses
self.newer = pagination?.newer self.newer = pagination?.newer
@ -125,7 +126,8 @@ class TimelineTableViewController: UITableViewController {
if indexPath.row == statuses.count - 1 { if indexPath.row == statuses.count - 1 {
guard let older = older else { return } guard let older = older else { return }
MastodonController.shared.client.getStatuses(timeline: timeline, range: older) { response in let request = MastodonController.shared.client.getStatuses(timeline: timeline, range: older)
MastodonController.shared.client.run(request) { response in
guard case let .success(newStatuses, pagination) = response else { fatalError() } guard case let .success(newStatuses, pagination) = response else { fatalError() }
self.older = pagination?.older self.older = pagination?.older
self.statuses.append(contentsOf: newStatuses) self.statuses.append(contentsOf: newStatuses)
@ -148,7 +150,8 @@ class TimelineTableViewController: UITableViewController {
@IBAction func refreshStatuses(_ sender: Any) { @IBAction func refreshStatuses(_ sender: Any) {
guard let newer = newer else { return } guard let newer = newer else { return }
MastodonController.shared.client.getStatuses(timeline: timeline, range: newer) { response in let request = MastodonController.shared.client.getStatuses(timeline: timeline, range: newer)
MastodonController.shared.client.run(request) { response in
guard case let .success(newStatuses, pagination) = response else { fatalError() } guard case let .success(newStatuses, pagination) = response else { fatalError() }
self.newer = pagination?.newer self.newer = pagination?.newer
self.statuses.insert(contentsOf: newStatuses, at: 0) self.statuses.insert(contentsOf: newStatuses, at: 0)