Start adding user API calls
This commit is contained in:
parent
15d80c1ab3
commit
8cbf2466cf
4 changed files with 68 additions and 1 deletions
|
@ -0,0 +1,60 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:libac_dart/discord/networking/endpoints.dart';
|
||||
|
||||
import '../structures/user.dart';
|
||||
|
||||
class UserPackets {
|
||||
/// Requests the current user from Discord's servers
|
||||
static Future<User> getCurrentUser() async {
|
||||
Dio dio = Dio();
|
||||
var reply = await dio.get(
|
||||
"${DiscordEndpoints.BaseURL}${DiscordEndpoints.Users}${DiscordEndpoints.ME}");
|
||||
|
||||
return User.fromJson(reply.data as String);
|
||||
}
|
||||
|
||||
/// Requests the user from Discord's servers
|
||||
///
|
||||
/// [id] is expected to be a valid Discord User ID
|
||||
static Future<User> getUser(int id) async {
|
||||
Dio dio = Dio();
|
||||
var reply = await dio
|
||||
.get("${DiscordEndpoints.BaseURL}${DiscordEndpoints.Users}/${id}");
|
||||
|
||||
return User.fromJson(reply.data as String);
|
||||
}
|
||||
|
||||
/// Updates any values that were set in the packet, and returns the updated user
|
||||
///
|
||||
/// Fires a User Update gateway event
|
||||
static Future<User> updateCurrentUser(ModifyCurrentUserPacket mcup) async {
|
||||
Dio dio = Dio();
|
||||
var reply = await dio.patch(
|
||||
"${DiscordEndpoints.BaseURL}${DiscordEndpoints.Users}${DiscordEndpoints.ME}",
|
||||
data: mcup.encode());
|
||||
|
||||
return User.fromJson(reply.data as String);
|
||||
}
|
||||
}
|
||||
|
||||
class ModifyCurrentUserPacket {
|
||||
String? username;
|
||||
String? avatar;
|
||||
String? banner;
|
||||
|
||||
ModifyCurrentUserPacket({this.username, this.avatar, this.banner});
|
||||
|
||||
String encode() {
|
||||
return json.encode(toJson());
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
if (username != null) "username": username,
|
||||
if (avatar != null) "avatar": avatar,
|
||||
if (banner != null) "banner": banner,
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue