feature: add Suggestions API

This commit is contained in:
sunxiaojian 2021-03-31 15:06:46 +08:00
parent 0033ea0680
commit 5ec07e617e
4 changed files with 67 additions and 5 deletions

View File

@ -16,13 +16,17 @@ final class SearchViewController: UIViewController, NeedsDependency {
var disposeBag = Set<AnyCancellable>()
private(set) lazy var viewModel = SearchViewModel(context: context)
let searchBar: UISearchBar = {
let searchBar = UISearchBar()
return searchBar
}()
}
extension SearchViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}

View File

@ -32,7 +32,7 @@ extension Mastodon.API.Search {
public static func search(
session: URLSession,
domain: String,
query: Query,
query: Mastodon.API.Search.Query,
authorization: Mastodon.API.OAuth.Authorization
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.SearchResult>, Error> {
let request = Mastodon.API.get(

View File

@ -1,8 +1,66 @@
//
// File.swift
// Mastodon+API+Suggestions.swift
//
//
// Created by sxiaojian on 2021/3/31.
//
import Combine
import Foundation
/// Follow suggestions
///
/// Server-generated suggestions on who to follow, based on previous positive interactions.
///
/// Version history:
/// 2.4.3 - added
/// # Reference
/// [Document](https://docs.joinmastodon.org/methods/accounts/suggestions/)
/// - Parameters:
/// - session: `URLSession`
/// - domain: Mastodon instance domain. e.g. "example.com"
/// - query: query
/// - authorization: User token.
/// - Returns: `AnyPublisher` contains `Accounts` nested in the response
extension Mastodon.API.Suggestions {
static func suggestionsURL(domain: String) -> URL {
Mastodon.API.endpointURL(domain: domain).appendingPathComponent("api/v1/suggestions")
}
public static func get(
session: URLSession,
domain: String,
query: Mastodon.API.Suggestions.Query,
authorization: Mastodon.API.OAuth.Authorization
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Account]>, Error> {
let request = Mastodon.API.get(
url: suggestionsURL(domain: domain),
query: query,
authorization: authorization
)
return session.dataTaskPublisher(for: request)
.tryMap { data, response in
let value = try Mastodon.API.decode(type: [Mastodon.Entity.Account].self, from: data, response: response)
return Mastodon.Response.Content(value: value, response: response)
}
.eraseToAnyPublisher()
}
}
extension Mastodon.API.Suggestions {
public struct Query: Codable, GetQuery {
public init(limit: Int?) {
self.limit = limit
}
public let limit: Int? // Maximum number of results to return. Defaults to 40.
var queryItems: [URLQueryItem]? {
var items: [URLQueryItem] = []
limit.flatMap { items.append(URLQueryItem(name: "limit", value: String($0))) }
guard !items.isEmpty else { return nil }
return items
}
}
}

View File

@ -25,13 +25,13 @@ import Foundation
extension Mastodon.API.Trends {
static func trendsURL(domain: String) -> URL {
Mastodon.API.endpointURL(domain: domain).appendingPathComponent("/api/v1/trends")
Mastodon.API.endpointURL(domain: domain).appendingPathComponent("api/v1/trends")
}
public static func get(
session: URLSession,
domain: String,
query: Query
query: Mastodon.API.Trends.Query
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Tag]>, Error> {
let request = Mastodon.API.get(
url: trendsURL(domain: domain),