diff --git a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Onboarding.swift b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Onboarding.swift index 84104d838..1a103b610 100644 --- a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Onboarding.swift +++ b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Onboarding.swift @@ -28,5 +28,8 @@ extension APIService { return Mastodon.Entity.Category(category: kind.rawValue, serversCount: 0) } } - + + public func languages() -> AnyPublisher, Error> { + return Mastodon.API.Onboarding.languages(session: session) + } } diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Onboarding.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Onboarding.swift index ab99deed6..a41a58039 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Onboarding.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Onboarding.swift @@ -12,6 +12,7 @@ extension Mastodon.API.Onboarding { static let serversEndpointURL = Mastodon.API.joinMastodonEndpointURL.appendingPathComponent("servers") static let categoriesEndpointURL = Mastodon.API.joinMastodonEndpointURL.appendingPathComponent("categories") + static let languagesEndpointURL = Mastodon.API.joinMastodonEndpointURL.appendingPathComponent("languages") /// Fetch server list /// @@ -68,7 +69,33 @@ extension Mastodon.API.Onboarding { } .eraseToAnyPublisher() } - + + /// Fetch server languages + /// + /// Using this endpoint to fetch booked languages + /// + /// # Last Update + /// 2022/12/19 + /// # Reference + /// undocumented + /// - Parameters: + /// - session: `URLSession` + /// - Returns: `AnyPublisher` contains `Language` nested in the response + public static func languages( + session: URLSession + ) -> AnyPublisher, Error> { + let request = Mastodon.API.get( + url: languagesEndpointURL, + query: nil, + authorization: nil + ) + return session.dataTaskPublisher(for: request) + .tryMap { data, response in + let value = try Mastodon.API.decode(type: [Mastodon.Entity.Language].self, from: data, response: response) + return Mastodon.Response.Content(value: value, response: response) + } + .eraseToAnyPublisher() + } } extension Mastodon.API.Onboarding { diff --git a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Language.swift b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Language.swift new file mode 100644 index 000000000..989bea084 --- /dev/null +++ b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Language.swift @@ -0,0 +1,29 @@ +// +// File.swift +// +// +// Created by Nathan Mattes on 19.12.22. +// + +import Foundation + +extension Mastodon.Entity { + + public struct Language: Codable { + public let locale: String + public let serversCount: Int + public let language: String? + + enum CodingKeys: String, CodingKey { + case locale + case serversCount = "servers_count" + case language + } + + public init(locale: String, serversCount: Int, language: String?) { + self.locale = locale + self.serversCount = serversCount + self.language = language + } + } +}