feat: refactor the mastodonSDK

chore: update API docs
This commit is contained in:
jk234ert 2021-02-19 17:40:05 +08:00
parent 2a84275288
commit e44821bce8
7 changed files with 67 additions and 31 deletions

View File

@ -186,7 +186,7 @@ extension MastodonRegisterViewController {
viewModel.isRegistering.value = true viewModel.isRegistering.value = true
let query = Mastodon.API.Account.RegisterQuery( let query = Mastodon.API.Account.RegisterQuery(
reason: "", reason: nil,
username: username, username: username,
email: email, email: email,
password: password, password: password,

View File

@ -22,7 +22,8 @@ extension Mastodon.API.Account {
/// Test to make sure that the user token works. /// Test to make sure that the user token works.
/// ///
/// - Version: 3.0.0 /// - Since: 0.0.0
/// - Version: 3.3.0
/// # Last Update /// # Last Update
/// 2021/2/9 /// 2021/2/9
/// # Reference /// # Reference
@ -30,6 +31,7 @@ extension Mastodon.API.Account {
/// - Parameters: /// - Parameters:
/// - session: `URLSession` /// - session: `URLSession`
/// - domain: Mastodon instance domain. e.g. "example.com" /// - domain: Mastodon instance domain. e.g. "example.com"
/// - authorization: App token
/// - Returns: `AnyPublisher` contains `Account` nested in the response /// - Returns: `AnyPublisher` contains `Account` nested in the response
public static func verifyCredentials( public static func verifyCredentials(
session: URLSession, session: URLSession,
@ -51,7 +53,8 @@ extension Mastodon.API.Account {
/// Creates a user and account records. /// Creates a user and account records.
/// ///
/// - Version: 3.0.0 /// - Since: 2.7.0
/// - Version: 3.3.0
/// # Last Update /// # Last Update
/// 2021/2/9 /// 2021/2/9
/// # Reference /// # Reference
@ -59,6 +62,8 @@ extension Mastodon.API.Account {
/// - Parameters: /// - Parameters:
/// - session: `URLSession` /// - session: `URLSession`
/// - domain: Mastodon instance domain. e.g. "example.com" /// - domain: Mastodon instance domain. e.g. "example.com"
/// - query: `RegisterQuery` with account registration information
/// - authorization: App token
/// - Returns: `AnyPublisher` contains `Token` nested in the response /// - Returns: `AnyPublisher` contains `Token` nested in the response
public static func register( public static func register(
session: URLSession, session: URLSession,
@ -81,7 +86,8 @@ extension Mastodon.API.Account {
/// Update the user's display and preferences. /// Update the user's display and preferences.
/// ///
/// - Version: 3.0.0 /// - Since: 1.1.1
/// - Version: 3.3.0
/// # Last Update /// # Last Update
/// 2021/2/9 /// 2021/2/9
/// # Reference /// # Reference
@ -89,12 +95,13 @@ extension Mastodon.API.Account {
/// - Parameters: /// - Parameters:
/// - session: `URLSession` /// - session: `URLSession`
/// - domain: Mastodon instance domain. e.g. "example.com" /// - domain: Mastodon instance domain. e.g. "example.com"
/// - query: `CredentialQuery` with update information /// - query: `CredentialQuery` with update credential information
/// - authorization: `UpdateCredentialQuery` with update information
/// - Returns: `AnyPublisher` contains updated `Account` nested in the response /// - Returns: `AnyPublisher` contains updated `Account` nested in the response
public static func updateCredentials( public static func updateCredentials(
session: URLSession, session: URLSession,
domain: String, domain: String,
query: CredentialQuery, query: UpdateCredentialQuery,
authorization: Mastodon.API.OAuth.Authorization authorization: Mastodon.API.OAuth.Authorization
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Account>, Error> { ) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Account>, Error> {
let request = Mastodon.API.patch( let request = Mastodon.API.patch(
@ -112,7 +119,8 @@ extension Mastodon.API.Account {
/// View information about a profile. /// View information about a profile.
/// ///
/// - Version: 3.0.0 /// - Since: 0.0.0
/// - Version: 3.3.0
/// # Last Update /// # Last Update
/// 2021/2/9 /// 2021/2/9
/// # Reference /// # Reference
@ -120,12 +128,14 @@ extension Mastodon.API.Account {
/// - Parameters: /// - Parameters:
/// - session: `URLSession` /// - session: `URLSession`
/// - domain: Mastodon instance domain. e.g. "example.com" /// - domain: Mastodon instance domain. e.g. "example.com"
/// - query: `AccountInfoQuery` with account query information,
/// - authorization: user token
/// - Returns: `AnyPublisher` contains `Account` nested in the response /// - Returns: `AnyPublisher` contains `Account` nested in the response
public static func accountInfo( public static func accountInfo(
session: URLSession, session: URLSession,
domain: String, domain: String,
query: AccountInfoQuery, query: AccountInfoQuery,
authorization: Mastodon.API.OAuth.Authorization authorization: Mastodon.API.OAuth.Authorization?
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Account>, Error> { ) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Account>, Error> {
let request = Mastodon.API.get( let request = Mastodon.API.get(
url: accountsEndpointURL(domain: domain), url: accountsEndpointURL(domain: domain),
@ -162,7 +172,7 @@ extension Mastodon.API.Account {
} }
} }
public struct CredentialQuery: Codable, PatchQuery { public struct UpdateCredentialQuery: Codable, PatchQuery {
public var discoverable: Bool? public var discoverable: Bool?
public var bot: Bool? public var bot: Bool?
@ -177,7 +187,7 @@ extension Mastodon.API.Account {
enum CodingKeys: String, CodingKey { enum CodingKeys: String, CodingKey {
case discoverable case discoverable
case bot case bot
case displayName case displayName = "display_name"
case note case note
case avatar case avatar

View File

@ -16,7 +16,7 @@ extension Mastodon.API.Instance {
/// Information about the server /// Information about the server
/// ///
/// - Since: 1.0.0 /// - Since: 1.1.0
/// - Version: 3.3.0 /// - Version: 3.3.0
/// # Last Update /// # Last Update
/// 2021/2/5 /// 2021/2/5

View File

@ -38,7 +38,7 @@ extension Mastodon.API.OAuth {
/// ///
/// This method construct a URL for user authorize /// This method construct a URL for user authorize
/// ///
/// - Since: 0.1.0 /// - Since: 0.0.0
/// - Version: 3.3.0 /// - Version: 3.3.0
/// # Last Update /// # Last Update
/// 2021/1/29 /// 2021/1/29

View File

@ -17,6 +17,19 @@ extension Mastodon.API.Timeline {
return Mastodon.API.endpointURL(domain: domain).appendingPathComponent("timelines/home") return Mastodon.API.endpointURL(domain: domain).appendingPathComponent("timelines/home")
} }
/// View public timeline statuses
///
/// - Since: 0.0.0
/// - Version: 3.3.0
/// # Last Update
/// 2021/2/19
/// # Reference
/// [Document](https://https://docs.joinmastodon.org/methods/timelines/)
/// - Parameters:
/// - session: `URLSession`
/// - domain: Mastodon instance domain. e.g. "example.com"
/// - query: `PublicTimelineQuery` with query parameters
/// - Returns: `AnyPublisher` contains `Token` nested in the response
public static func `public`( public static func `public`(
session: URLSession, session: URLSession,
domain: String, domain: String,
@ -35,6 +48,19 @@ extension Mastodon.API.Timeline {
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
/// View statuses from followed users.
///
/// - Since: 0.0.0
/// - Version: 3.3.0
/// # Last Update
/// 2021/2/19
/// # Reference
/// [Document](https://https://docs.joinmastodon.org/methods/timelines/)
/// - Parameters:
/// - session: `URLSession`
/// - domain: Mastodon instance domain. e.g. "example.com"
/// - query: `PublicTimelineQuery` with query parameters
/// - Returns: `AnyPublisher` contains `Token` nested in the response
public static func home( public static func home(
session: URLSession, session: URLSession,
domain: String, domain: String,

View File

@ -96,7 +96,7 @@ extension Mastodon.API {
query: GetQuery?, query: GetQuery?,
authorization: OAuth.Authorization? authorization: OAuth.Authorization?
) -> URLRequest { ) -> URLRequest {
return buildRequest(url: url, query: query, authorization: authorization) return buildRequest(url: url, method: .GET, query: query, authorization: authorization)
} }
static func post( static func post(
@ -104,7 +104,7 @@ extension Mastodon.API {
query: PostQuery?, query: PostQuery?,
authorization: OAuth.Authorization? authorization: OAuth.Authorization?
) -> URLRequest { ) -> URLRequest {
return buildRequest(url: url, query: query, authorization: authorization) return buildRequest(url: url, method: .POST, query: query, authorization: authorization)
} }
static func patch( static func patch(
@ -112,24 +112,24 @@ extension Mastodon.API {
query: PatchQuery?, query: PatchQuery?,
authorization: OAuth.Authorization? authorization: OAuth.Authorization?
) -> URLRequest { ) -> URLRequest {
return buildRequest(url: url, query: query, authorization: authorization) return buildRequest(url: url, method: .PATCH, query: query, authorization: authorization)
} }
private static func buildRequest( private static func buildRequest(
url: URL, url: URL,
method: RequestMethod,
query: RequestQuery?, query: RequestQuery?,
authorization: OAuth.Authorization? authorization: OAuth.Authorization?
) -> URLRequest { ) -> URLRequest {
var components = URLComponents(string: url.absoluteString)! var components = URLComponents(string: url.absoluteString)!
if let requestQuery = query as? GetQuery { components.queryItems = query?.queryItems
components.queryItems = requestQuery.queryItems
}
let requestURL = components.url! let requestURL = components.url!
var request = URLRequest( var request = URLRequest(
url: requestURL, url: requestURL,
cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: Mastodon.API.timeoutInterval timeoutInterval: Mastodon.API.timeoutInterval
) )
request.httpMethod = method.rawValue
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.httpBody = query?.body request.httpBody = query?.body
if let authorization = authorization { if let authorization = authorization {
@ -138,10 +138,7 @@ extension Mastodon.API {
forHTTPHeaderField: Mastodon.API.OAuth.authorizationField forHTTPHeaderField: Mastodon.API.OAuth.authorizationField
) )
} }
request.httpMethod = query?.method.rawValue ?? RequestMethod.GET.rawValue
return request return request
} }
static func decode<T>(type: T.Type, from data: Data, response: URLResponse) throws -> T where T : Decodable { static func decode<T>(type: T.Type, from data: Data, response: URLResponse) throws -> T where T : Decodable {

View File

@ -12,33 +12,36 @@ enum RequestMethod: String {
} }
protocol RequestQuery { protocol RequestQuery {
// All kinds of queries could have queryItems and body
var queryItems: [URLQueryItem]? { get }
var body: Data? { get } var body: Data? { get }
var method: RequestMethod { get }
} }
// An `Encodable` query provides its body by encoding itself
// A `Get` query only contains queryItems, it should not be `Encodable`
extension RequestQuery where Self: Encodable { extension RequestQuery where Self: Encodable {
var body: Data? { var body: Data? {
return try? Mastodon.API.encoder.encode(self) return try? Mastodon.API.encoder.encode(self)
} }
} }
protocol GetQuery: RequestQuery { protocol GetQuery: RequestQuery { }
var queryItems: [URLQueryItem]? { get }
}
extension GetQuery { extension GetQuery {
var method: RequestMethod { return .GET } // By default a `GetQuery` does not has data body
var body: Data? { return nil } var body: Data? { nil }
} }
protocol PostQuery: RequestQuery { } protocol PostQuery: RequestQuery & Encodable { }
extension PostQuery { extension PostQuery {
var method: RequestMethod { return .POST } // By default a `GetQuery` does not has query items
var queryItems: [URLQueryItem]? { nil }
} }
protocol PatchQuery: RequestQuery { } protocol PatchQuery: RequestQuery & Encodable { }
extension PatchQuery { extension PatchQuery {
var method: RequestMethod { return .PATCH } // By default a `GetQuery` does not has query items
var queryItems: [URLQueryItem]? { nil }
} }