Start adding user API calls

This commit is contained in:
zontreck 2024-07-10 03:10:05 -07:00
parent 15d80c1ab3
commit 8cbf2466cf
4 changed files with 68 additions and 1 deletions

View file

@ -6,6 +6,7 @@ import 'package:libac_dart/discord/structures/application.dart';
import 'endpoints.dart';
class ApplicationPacket {
/// This will request the current application instance.
static Future<Application> getCurrentApplication() async {
Dio dio = Dio();
var reply = await dio.get(
@ -15,6 +16,7 @@ class ApplicationPacket {
return Application.fromJson(jsonReply);
}
/// This will update the current application instance.
static Future<Application> updateApplication(EditApplication app) async {
Dio dio = Dio();
var reply = await dio.patch(

View file

@ -1,6 +1,11 @@
/// Contains currently supported endpoints and URLs.
class DiscordEndpoints {
/// Currently this is set to 10
static const APIVersion = 10;
/// This is the current base URL of all discord API calls per reference.
static const BaseURL = "https://discord.com/api/v${APIVersion}";
static const ME = "/@me";
static const Applications = "/applications";
static const Users = "/users";
}

View file

@ -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,
};
}
}

View file

@ -1,6 +1,6 @@
name: libac_dart
description: "Aria's Creations code library"
version: 1.2.071024+0246
version: 1.2.071024+0309
homepage: "https://zontreck.com"