-
Notifications
You must be signed in to change notification settings - Fork 1
/
Channel+API.swift
35 lines (27 loc) · 1.12 KB
/
Channel+API.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//
// Channel+API.swift
//
//
// Created by Jaehong Kang on 2022/07/21.
//
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
extension Channel {
public init(channelID: Snowflake, session: RESTSession) async throws {
var urlRequest = URLRequest(url: URL(discordAPIPath: "channels/\(channelID.rawValue)", apiVersion: session.configuration.apiVersion)!)
urlRequest.httpMethod = "GET"
let (data, _) = try await session.data(for: urlRequest, includesOAuth2Credential: true)
self = try JSONDecoder.discord.decode(Channel.self, from: data)
}
}
extension Channel {
public static func channels(forGuildID guildID: Snowflake, session: RESTSession) async throws -> [Channel] {
var urlRequest = URLRequest(url: URL(discordAPIPath: "guilds/\(guildID)/channels", apiVersion: session.configuration.apiVersion)!)
urlRequest.httpMethod = "GET"
let (data, _) = try await session.data(for: urlRequest, includesOAuth2Credential: true)
let channels = try JSONDecoder.discord.decode([Channel].self, from: data)
return channels
}
}