-
Notifications
You must be signed in to change notification settings - Fork 1
/
User+API.swift
35 lines (27 loc) · 1.03 KB
/
User+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
//
// User+API.swift
//
//
// Created by Jaehong Kang on 2022/07/21.
//
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
extension User {
public static func me(session: RESTSession) async throws -> User {
var urlRequest = URLRequest(url: URL(discordAPIPath: "users/@me", apiVersion: session.configuration.apiVersion)!)
urlRequest.httpMethod = "GET"
let (data, _) = try await session.data(for: urlRequest, includesOAuth2Credential: true)
let user = try JSONDecoder.discord.decode(User.self, from: data)
return user
}
}
extension User {
public init(userID: Snowflake, session: RESTSession) async throws {
var urlRequest = URLRequest(url: URL(discordAPIPath: "users/\(userID.rawValue)", apiVersion: session.configuration.apiVersion)!)
urlRequest.httpMethod = "GET"
let (data, _) = try await session.data(for: urlRequest, includesOAuth2Credential: true)
self = try JSONDecoder.discord.decode(User.self, from: data)
}
}