feat: add Mastodon instance api

This commit is contained in:
CMK 2021-02-05 12:10:34 +08:00
parent f2951d5b07
commit f066e736eb
5 changed files with 91 additions and 2 deletions

View File

@ -32,6 +32,7 @@
{
"skippedTests" : [
"MastodonSDKTests\/testCreateAnAnpplication()",
"MastodonSDKTests\/testHomeTimeline()",
"MastodonSDKTests\/testVerifyAppCredentials()"
],
"target" : {

View File

@ -0,0 +1,46 @@
//
// Mastodon+API+Instance.swift
//
//
// Created by MainasuK Cirno on 2021-2-5.
//
import Foundation
import Combine
extension Mastodon.API.Instance {
static func instanceEndpointURL(domain: String) -> URL {
return Mastodon.API.endpointURL(domain: domain).appendingPathComponent("instance")
}
/// Information about the server
///
/// - Since: 1.0.0
/// - Version: 3.3.0
/// # Last Update
/// 2021/2/5
/// # Reference
/// [Document](https://docs.joinmastodon.org/methods/instance/)
/// - Parameters:
/// - session: `URLSession`
/// - domain: Mastodon instance domain. e.g. "example.com"
/// - Returns: `AnyPublisher` contains `Instance` nested in the response
public static func instance(
session: URLSession,
domain: String
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Instance>, Error> {
let request = Mastodon.API.get(
url: instanceEndpointURL(domain: domain),
query: nil,
authorization: nil
)
return session.dataTaskPublisher(for: request)
.tryMap { data, response in
let value = try Mastodon.API.decode(type: Mastodon.Entity.Instance.self, from: data, response: response)
return Mastodon.Response.Content(value: value, response: response)
}
.eraseToAnyPublisher()
}
}

View File

@ -84,6 +84,7 @@ extension Mastodon.API {
extension Mastodon.API {
public enum Account { }
public enum App { }
public enum Instance { }
public enum OAuth { }
public enum Timeline { }
}

View File

@ -13,7 +13,7 @@ extension Mastodon.Entity {
/// - Since: 1.1.0
/// - Version: 3.3.0
/// # Last Update
/// 2021/1/28
/// 2021/2/5
/// # Reference
/// [Document](https://docs.joinmastodon.org/entities/instance/)
public struct Instance: Codable {
@ -28,7 +28,7 @@ extension Mastodon.Entity {
public let registrations: Bool?
public let approvalRequired: Bool?
public let invitesEnabled: Bool?
public let urls: [InstanceURL]?
public let urls: InstanceURL?
public let statistics: Statistics?
public let thumbnail: String?

View File

@ -0,0 +1,41 @@
//
// MastodonSDK+API+Instance.swift
//
//
// Created by MainasuK Cirno on 2021-2-5.
//
import os.log
import XCTest
import Combine
@testable import MastodonSDK
extension MastodonSDKTests {
func testInstance() throws {
try _testInstance(domain: domain)
}
func _testInstance(domain: String) throws {
let theExpectation = expectation(description: "Fetch Instance Infomation")
Mastodon.API.Instance.instance(session: session, domain: domain)
.receive(on: DispatchQueue.main)
.sink { completion in
switch completion {
case .failure(let error):
XCTFail(error.localizedDescription)
case .finished:
break
}
} receiveValue: { response in
XCTAssertEqual(response.value.uri, domain)
print(response.value)
theExpectation.fulfill()
}
.store(in: &disposeBag)
wait(for: [theExpectation], timeout: 10.0)
}
}