From f066e736eb4d88d2b1df2ebfa05d7e25ff6f5ecf Mon Sep 17 00:00:00 2001 From: CMK Date: Fri, 5 Feb 2021 12:10:34 +0800 Subject: [PATCH] feat: add Mastodon instance api --- MastodonSDK.xctestplan | 1 + .../API/Mastodon+API+Instance.swift | 46 +++++++++++++++++++ .../MastodonSDK/API/Mastodon+API.swift | 1 + .../Entity/Mastodon+Entity+Instance.swift | 4 +- .../API/MastodonSDK+API+Instance.swift | 41 +++++++++++++++++ 5 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Instance.swift create mode 100644 MastodonSDK/Tests/MastodonSDKTests/API/MastodonSDK+API+Instance.swift diff --git a/MastodonSDK.xctestplan b/MastodonSDK.xctestplan index 28e9be637..f37534015 100644 --- a/MastodonSDK.xctestplan +++ b/MastodonSDK.xctestplan @@ -32,6 +32,7 @@ { "skippedTests" : [ "MastodonSDKTests\/testCreateAnAnpplication()", + "MastodonSDKTests\/testHomeTimeline()", "MastodonSDKTests\/testVerifyAppCredentials()" ], "target" : { diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Instance.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Instance.swift new file mode 100644 index 000000000..bd7a01bcd --- /dev/null +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Instance.swift @@ -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, 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() + } + +} diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift index 283d6e1d5..900ebf965 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift @@ -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 { } } diff --git a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Instance.swift b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Instance.swift index 73a573f2c..3154d63fa 100644 --- a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Instance.swift +++ b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Instance.swift @@ -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? diff --git a/MastodonSDK/Tests/MastodonSDKTests/API/MastodonSDK+API+Instance.swift b/MastodonSDK/Tests/MastodonSDKTests/API/MastodonSDK+API+Instance.swift new file mode 100644 index 000000000..b5de3fe31 --- /dev/null +++ b/MastodonSDK/Tests/MastodonSDKTests/API/MastodonSDK+API+Instance.swift @@ -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) + } + +}