diff --git a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Instance.swift b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Instance.swift index 8231b330b..e96fb57f2 100644 --- a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Instance.swift +++ b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Instance.swift @@ -33,6 +33,12 @@ extension APIService { ) -> AnyPublisher, Error> { return Mastodon.API.V2.Instance.instance(session: session, authorization: authenticationBox?.userAuthorization, domain: domain) } + public func instanceV2( + domain: String, + authenticationBox: MastodonAuthenticationBox? + ) async throws -> Mastodon.Entity.V2.Instance { + return try await Mastodon.API.V2.Instance.instance(session: session, authorization: authenticationBox?.userAuthorization, domain: domain) + } public func extendedDescription( domain: String, diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+V2+Instance.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+V2+Instance.swift index 1077b285e..7f8a70986 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+V2+Instance.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+V2+Instance.swift @@ -48,4 +48,31 @@ extension Mastodon.API.V2.Instance { .eraseToAnyPublisher() } + public static func instance( + session: URLSession, + authorization: Mastodon.API.OAuth.Authorization?, + domain: String + ) async throws -> Mastodon.Entity.V2.Instance { + let request = Mastodon.API.get( + url: instanceEndpointURL(domain: domain), + query: nil, + authorization: authorization + ) + + let (data, response) = try await session.data(for: request) + + let value: Mastodon.Entity.V2.Instance + + do { + value = try Mastodon.API.decode(type: Mastodon.Entity.V2.Instance.self, from: data, response: response) + } catch { + if let response = response as? HTTPURLResponse, 400 ..< 500 ~= response.statusCode { + // For example, AUTHORIZED_FETCH may result in authentication errors + value = Mastodon.Entity.V2.Instance(domain: domain) + } else { + throw error + } + } + return value + } } diff --git a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+InstanceV2.swift b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+InstanceV2.swift index aa0b80dbd..c8bcc35a6 100644 --- a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+InstanceV2.swift +++ b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+InstanceV2.swift @@ -6,7 +6,7 @@ extension Mastodon.Entity.V2 { /// - Since: 4.0.0 /// - Version: 4.0.3 /// # Last Update - /// 2022/12/09 + /// 2025/03/24 /// # Reference /// [Document](https://docs.joinmastodon.org/entities/instance/) public struct Instance: Codable { @@ -92,6 +92,12 @@ extension Mastodon.Entity.V2.Instance { extension Mastodon.Entity.V2.Instance { public struct Registrations: Codable { public let enabled: Bool + public let minAge: Int? + + enum CodingKeys: String, CodingKey { + case enabled + case minAge = "min_age" + } } }