From 5ec07e617e81ab4595307394e253876306be26ab Mon Sep 17 00:00:00 2001 From: sunxiaojian Date: Wed, 31 Mar 2021 15:06:46 +0800 Subject: [PATCH] feature: add Suggestions API --- .../Scene/Search/SearchViewController.swift | 6 +- .../MastodonSDK/API/Mastodon+API+Search.swift | 2 +- .../API/Mastodon+API+Suggestions.swift | 60 ++++++++++++++++++- .../MastodonSDK/API/Mastodon+API+Trends.swift | 4 +- 4 files changed, 67 insertions(+), 5 deletions(-) diff --git a/Mastodon/Scene/Search/SearchViewController.swift b/Mastodon/Scene/Search/SearchViewController.swift index acf2759f..426120d5 100644 --- a/Mastodon/Scene/Search/SearchViewController.swift +++ b/Mastodon/Scene/Search/SearchViewController.swift @@ -16,13 +16,17 @@ final class SearchViewController: UIViewController, NeedsDependency { var disposeBag = Set() private(set) lazy var viewModel = SearchViewModel(context: context) + let searchBar: UISearchBar = { + let searchBar = UISearchBar() + return searchBar + }() } extension SearchViewController { override func viewDidLoad() { super.viewDidLoad() - + } } diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Search.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Search.swift index 114ca27b..b157d093 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Search.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Search.swift @@ -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, Error> { let request = Mastodon.API.get( diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Suggestions.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Suggestions.swift index bc2adaee..ddb2297d 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Suggestions.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Suggestions.swift @@ -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, 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 + } + } +} diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Trends.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Trends.swift index b311cf2b..3c7e8515 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Trends.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Trends.swift @@ -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, Error> { let request = Mastodon.API.get( url: trendsURL(domain: domain),