IceCubesApp/Packages/Network/Sources/Network/InstanceSocialClient.swift

28 lines
990 B
Swift
Raw Normal View History

2022-12-29 13:07:58 +00:00
import Foundation
import Models
public struct InstanceSocialClient {
private let authorization = "Bearer 8a4xx3D7Hzu1aFnf18qlkH8oU0oZ5ulabXxoS2FtQtwOy8G0DGQhr5PjTIjBnYAmFrSBuE2CcASjFocxJBonY8XGbLySB7MXd9ssrwlRHUXTQh3Z578lE1OfUtafvhML"
private let endpoint = URL(string: "https://instances.social/api/1.0/instances/list?count=1000&include_closed=false&include_dead=false&min_active_users=500")!
2023-01-17 10:36:01 +00:00
2022-12-29 13:07:58 +00:00
struct Response: Decodable {
let instances: [InstanceSocial]
}
2023-01-17 10:36:01 +00:00
public init() {}
2022-12-29 13:07:58 +00:00
public func fetchInstances() async -> [InstanceSocial] {
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
var request: URLRequest = .init(url: endpoint)
request.setValue(authorization, forHTTPHeaderField: "Authorization")
let (data, _) = try await URLSession.shared.data(for: request)
let response = try decoder.decode(Response.self, from: data)
return response.instances
} catch {
return []
}
}
}