Start adding user API calls
This commit is contained in:
parent
15d80c1ab3
commit
8cbf2466cf
4 changed files with 68 additions and 1 deletions
|
@ -6,6 +6,7 @@ import 'package:libac_dart/discord/structures/application.dart';
|
||||||
import 'endpoints.dart';
|
import 'endpoints.dart';
|
||||||
|
|
||||||
class ApplicationPacket {
|
class ApplicationPacket {
|
||||||
|
/// This will request the current application instance.
|
||||||
static Future<Application> getCurrentApplication() async {
|
static Future<Application> getCurrentApplication() async {
|
||||||
Dio dio = Dio();
|
Dio dio = Dio();
|
||||||
var reply = await dio.get(
|
var reply = await dio.get(
|
||||||
|
@ -15,6 +16,7 @@ class ApplicationPacket {
|
||||||
return Application.fromJson(jsonReply);
|
return Application.fromJson(jsonReply);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This will update the current application instance.
|
||||||
static Future<Application> updateApplication(EditApplication app) async {
|
static Future<Application> updateApplication(EditApplication app) async {
|
||||||
Dio dio = Dio();
|
Dio dio = Dio();
|
||||||
var reply = await dio.patch(
|
var reply = await dio.patch(
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
|
/// Contains currently supported endpoints and URLs.
|
||||||
class DiscordEndpoints {
|
class DiscordEndpoints {
|
||||||
|
/// Currently this is set to 10
|
||||||
static const APIVersion = 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 BaseURL = "https://discord.com/api/v${APIVersion}";
|
||||||
static const ME = "/@me";
|
static const ME = "/@me";
|
||||||
static const Applications = "/applications";
|
static const Applications = "/applications";
|
||||||
|
static const Users = "/users";
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
name: libac_dart
|
name: libac_dart
|
||||||
description: "Aria's Creations code library"
|
description: "Aria's Creations code library"
|
||||||
version: 1.2.071024+0246
|
version: 1.2.071024+0309
|
||||||
homepage: "https://zontreck.com"
|
homepage: "https://zontreck.com"
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue