From c919a724abd1765d6087ae1b2642384c765c708e Mon Sep 17 00:00:00 2001 From: CMK Date: Fri, 19 Feb 2021 11:56:51 +0800 Subject: [PATCH] feat: implement onboarding /servers and /categories endpoint --- Mastodon.xcodeproj/project.pbxproj | 4 ++ .../APIService/APIService+Onboarding.swift | 26 ++++++++ ...er.swift => Mastodon+API+Onboarding.swift} | 35 ++++++++-- .../MastodonSDK/API/Mastodon+API.swift | 2 +- .../API/MastodonSDK+API+OnboardingTests.swift | 66 +++++++++++++++++++ .../API/MastodonSDK+API+ServerTests.swift | 45 ------------- 6 files changed, 128 insertions(+), 50 deletions(-) create mode 100644 Mastodon/Service/APIService/APIService+Onboarding.swift rename MastodonSDK/Sources/MastodonSDK/API/{Mastodon+API+Server.swift => Mastodon+API+Onboarding.swift} (71%) create mode 100644 MastodonSDK/Tests/MastodonSDKTests/API/MastodonSDK+API+OnboardingTests.swift delete mode 100644 MastodonSDK/Tests/MastodonSDKTests/API/MastodonSDK+API+ServerTests.swift diff --git a/Mastodon.xcodeproj/project.pbxproj b/Mastodon.xcodeproj/project.pbxproj index 375aa1b00..4d26a7314 100644 --- a/Mastodon.xcodeproj/project.pbxproj +++ b/Mastodon.xcodeproj/project.pbxproj @@ -123,6 +123,7 @@ DB98338725C945ED00AD9700 /* Strings.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB98338525C945ED00AD9700 /* Strings.swift */; }; DB98338825C945ED00AD9700 /* Assets.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB98338625C945ED00AD9700 /* Assets.swift */; }; DB98339C25C96DE600AD9700 /* APIService+Account.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB98339B25C96DE600AD9700 /* APIService+Account.swift */; }; + DBD9149025DF6D8D00903DFD /* APIService+Onboarding.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBD9148F25DF6D8D00903DFD /* APIService+Onboarding.swift */; }; DBE0821525CD382600FD6BBD /* MastodonRegisterViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBE0821425CD382600FD6BBD /* MastodonRegisterViewController.swift */; }; DBE0822425CD3F1E00FD6BBD /* MastodonRegisterViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBE0822325CD3F1E00FD6BBD /* MastodonRegisterViewModel.swift */; }; /* End PBXBuildFile section */ @@ -305,6 +306,7 @@ DB98338525C945ED00AD9700 /* Strings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Strings.swift; sourceTree = ""; }; DB98338625C945ED00AD9700 /* Assets.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Assets.swift; sourceTree = ""; }; DB98339B25C96DE600AD9700 /* APIService+Account.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "APIService+Account.swift"; sourceTree = ""; }; + DBD9148F25DF6D8D00903DFD /* APIService+Onboarding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "APIService+Onboarding.swift"; sourceTree = ""; }; DBE0821425CD382600FD6BBD /* MastodonRegisterViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MastodonRegisterViewController.swift; sourceTree = ""; }; DBE0822325CD3F1E00FD6BBD /* MastodonRegisterViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MastodonRegisterViewModel.swift; sourceTree = ""; }; DBF53F5F25C14E88008AAC7B /* Mastodon.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; name = Mastodon.xctestplan; path = Mastodon/Mastodon.xctestplan; sourceTree = ""; }; @@ -677,6 +679,7 @@ DB45FB0925CA87BC005A8AC7 /* CoreData */, 2D61335625C1887F00CAE157 /* Persist */, 2D61335D25C1894B00CAE157 /* APIService.swift */, + DBD9148F25DF6D8D00903DFD /* APIService+Onboarding.swift */, 2DF75BA625D10E1000694EC8 /* APIService+Favorite.swift */, DB98337E25C9452D00AD9700 /* APIService+APIError.swift */, DB98336A25C9420100AD9700 /* APIService+App.swift */, @@ -1227,6 +1230,7 @@ 2DA7D04425CA52B200804E11 /* TimelineLoaderTableViewCell.swift in Sources */, DB8AF52F25C13561002E6C99 /* DocumentStore.swift in Sources */, 2D38F1EB25CD477000561493 /* HomeTimelineViewModel+LoadLatestState.swift in Sources */, + DBD9149025DF6D8D00903DFD /* APIService+Onboarding.swift in Sources */, DB98337F25C9452D00AD9700 /* APIService+APIError.swift in Sources */, 2DF123A725C3B0210020F248 /* ActiveLabel.swift in Sources */, DBE0822425CD3F1E00FD6BBD /* MastodonRegisterViewModel.swift in Sources */, diff --git a/Mastodon/Service/APIService/APIService+Onboarding.swift b/Mastodon/Service/APIService/APIService+Onboarding.swift new file mode 100644 index 000000000..588366f89 --- /dev/null +++ b/Mastodon/Service/APIService/APIService+Onboarding.swift @@ -0,0 +1,26 @@ +// +// APIService+Onboarding.swift +// Mastodon +// +// Created by MainasuK Cirno on 2021-2-19. +// + +import Foundation +import Combine +import MastodonSDK + +extension APIService { + + func servers( + language: String?, + category: String? + ) -> AnyPublisher, Error> { + let query = Mastodon.API.Onboarding.ServersQuery(language: language, category: category) + return Mastodon.API.Onboarding.servers(session: session, query: query) + } + + func categories() -> AnyPublisher, Error> { + return Mastodon.API.Onboarding.categories(session: session) + } + +} diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Server.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Onboarding.swift similarity index 71% rename from MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Server.swift rename to MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Onboarding.swift index 930a19c96..d6ae7fef5 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Server.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Onboarding.swift @@ -1,5 +1,5 @@ // -// Mastodon+API+Server.swift +// Mastodon+API+Onboarding.swift // // // Created by MainasuK Cirno on 2021-2-18. @@ -8,11 +8,25 @@ import Foundation import Combine -extension Mastodon.API.Server { +extension Mastodon.API.Onboarding { static let serversEndpointURL = Mastodon.API.joinMastodonEndpointURL.appendingPathComponent("servers") static let categoriesEndpointURL = Mastodon.API.joinMastodonEndpointURL.appendingPathComponent("categories") + /// Fetch server list + /// + /// Using this endpoint to fetch booked servers + /// + /// - Since: 3.3.0 + /// - Version: 3.3.0 + /// # Last Update + /// 2021/2/19 + /// # Reference + /// undocumented + /// - Parameters: + /// - session: `URLSession` + /// - query: `ServerQuery` + /// - Returns: `AnyPublisher` contains `Server` nested in the response public static func servers( session: URLSession, query: ServersQuery @@ -30,6 +44,19 @@ extension Mastodon.API.Server { .eraseToAnyPublisher() } + /// Fetch server catagories + /// + /// Using this endpoint to fetch booked catagories + /// + /// - Since: 3.3.0 + /// - Version: 3.3.0 + /// # Last Update + /// 2021/2/19 + /// # Reference + /// undocumented + /// - Parameters: + /// - session: `URLSession` + /// - Returns: `AnyPublisher` contains `Category` nested in the response public static func categories( session: URLSession ) -> AnyPublisher, Error> { @@ -48,9 +75,9 @@ extension Mastodon.API.Server { } -extension Mastodon.API.Server { +extension Mastodon.API.Onboarding { - public struct ServersQuery: Codable, GetQuery { + public struct ServersQuery: GetQuery { public let language: String? public let category: String? diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift index fbc550b03..7d3adb0cc 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift @@ -88,8 +88,8 @@ extension Mastodon.API { public enum App { } public enum Instance { } public enum OAuth { } + public enum Onboarding { } public enum Timeline { } - public enum Server { } public enum Favorites { } } diff --git a/MastodonSDK/Tests/MastodonSDKTests/API/MastodonSDK+API+OnboardingTests.swift b/MastodonSDK/Tests/MastodonSDKTests/API/MastodonSDK+API+OnboardingTests.swift new file mode 100644 index 000000000..f13c38e1c --- /dev/null +++ b/MastodonSDK/Tests/MastodonSDKTests/API/MastodonSDK+API+OnboardingTests.swift @@ -0,0 +1,66 @@ +// +// MastodonSDK+API+OnboardingTests.swift +// +// +// Created by MainasuK Cirno on 2021-2-18. +// + +import os.log +import XCTest +import Combine +@testable import MastodonSDK + +extension MastodonSDKTests { + + func testServers() throws { + try _testServers(query: Mastodon.API.Onboarding.ServersQuery(language: nil, category: nil)) + try _testServers(query: Mastodon.API.Onboarding.ServersQuery(language: "en", category: "tech")) + } + + func _testServers(query: Mastodon.API.Onboarding.ServersQuery) throws { + let theExpectation = expectation(description: "Fetch Server List") + Mastodon.API.Onboarding.servers( + session: session, + query: query + ) + .receive(on: DispatchQueue.main) + .sink { completion in + switch completion { + case .failure(let error): + XCTFail(error.localizedDescription) + case .finished: + break + } + } receiveValue: { response in + XCTAssert(!response.value.isEmpty) + theExpectation.fulfill() + } + .store(in: &disposeBag) + + wait(for: [theExpectation], timeout: 10.0) + } + + func testCatagories() throws { + let theExpectation = expectation(description: "Fetch Server Catagories") + Mastodon.API.Onboarding.categories( + session: session + ) + .receive(on: DispatchQueue.main) + .sink { completion in + switch completion { + case .failure(let error): + XCTFail(error.localizedDescription) + case .finished: + break + } + } receiveValue: { response in + XCTAssert(!response.value.isEmpty) + theExpectation.fulfill() + } + .store(in: &disposeBag) + + wait(for: [theExpectation], timeout: 10.0) + } + + +} diff --git a/MastodonSDK/Tests/MastodonSDKTests/API/MastodonSDK+API+ServerTests.swift b/MastodonSDK/Tests/MastodonSDKTests/API/MastodonSDK+API+ServerTests.swift deleted file mode 100644 index 4033545d8..000000000 --- a/MastodonSDK/Tests/MastodonSDKTests/API/MastodonSDK+API+ServerTests.swift +++ /dev/null @@ -1,45 +0,0 @@ -// -// MastodonSDK+API+ServerTests.swift -// -// -// Created by MainasuK Cirno on 2021-2-18. -// - -import os.log -import XCTest -import Combine -@testable import MastodonSDK - -extension MastodonSDKTests { - - func testServers() throws { - try _testServers(query: Mastodon.API.Server.ServersQuery(language: nil, category: nil)) - try _testServers(query: Mastodon.API.Server.ServersQuery(language: "en", category: "tech")) - } - - func _testServers(query: Mastodon.API.Server.ServersQuery) throws { - let theExpectation = expectation(description: "Fetch Server List") - Mastodon.API.Server.servers( - session: session, - query: query - ) - .receive(on: DispatchQueue.main) - .sink { completion in - switch completion { - case .failure(let error): - XCTFail(error.localizedDescription) - case .finished: - break - } - } receiveValue: { response in - XCTAssert(!response.value.isEmpty) - theExpectation.fulfill() - } - .store(in: &disposeBag) - - wait(for: [theExpectation], timeout: 10.0) - } - - - -}