initial commit; very basic REST client
This commit is contained in:
commit
6b39228b67
18 changed files with 1177 additions and 0 deletions
9
Foxcord/Rest/DiscordRestClient.User.cs
Normal file
9
Foxcord/Rest/DiscordRestClient.User.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
using Foxcord.Discord;
|
||||
|
||||
namespace Foxcord.Rest;
|
||||
|
||||
public partial class DiscordRestClient
|
||||
{
|
||||
public Task<User> GetUserAsync(ulong id) => RequestAsync<User>(HttpMethod.Get, $"/users/{id}");
|
||||
public Task<User> GetMeAsync() => RequestAsync<User>(HttpMethod.Get, "/users/@me");
|
||||
}
|
78
Foxcord/Rest/DiscordRestClient.cs
Normal file
78
Foxcord/Rest/DiscordRestClient.cs
Normal file
|
@ -0,0 +1,78 @@
|
|||
using System.Net.Http.Headers;
|
||||
using Foxcord.Serialization;
|
||||
|
||||
namespace Foxcord.Rest;
|
||||
|
||||
public partial class DiscordRestClient
|
||||
{
|
||||
public string UserAgent { get; private init; } = "DiscordBot (https://codeberg.org/u1f320/Foxcord, v1)";
|
||||
|
||||
private readonly HttpClient _client = new();
|
||||
private readonly string _token;
|
||||
private readonly string _baseUrl = "https://discord.com/api/v10";
|
||||
|
||||
public DiscordRestClient(string token, string? userAgent = null, string? baseUrl = null)
|
||||
{
|
||||
_token = token;
|
||||
if (userAgent != null) UserAgent = userAgent;
|
||||
if (baseUrl != null) _baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
public async Task<TResponse> RequestAsync<TResponse>(HttpMethod method, string path,
|
||||
IReadOnlyDictionary<string, string>? extraHeaders = null) where TResponse : class
|
||||
{
|
||||
var request = CreateRequest(method, path, body: null, extraHeaders);
|
||||
var resp = await Send(request);
|
||||
return await resp.Content.ReadAsJsonAsync<TResponse>() ??
|
||||
throw new FoxcordException("HTTP response was unexpectedly null");
|
||||
}
|
||||
|
||||
public async Task<TResponse> RequestAsync<TRequest, TResponse>(HttpMethod method, string path, TRequest body,
|
||||
IReadOnlyDictionary<string, string>? extraHeaders = null)
|
||||
where TRequest : class where TResponse : class
|
||||
{
|
||||
var request = CreateRequest(method, path, body, extraHeaders);
|
||||
var resp = await Send(request);
|
||||
return await resp.Content.ReadAsJsonAsync<TResponse>() ??
|
||||
throw new FoxcordException("HTTP response was unexpectedly null");
|
||||
}
|
||||
|
||||
public async Task RequestAsync<TRequest>(HttpMethod method, string path, TRequest body,
|
||||
IReadOnlyDictionary<string, string>? extraHeaders = null) where TRequest : class =>
|
||||
await Send(CreateRequest(method, path, body, extraHeaders));
|
||||
|
||||
public async Task RequestAsync(HttpMethod method, string path,
|
||||
IReadOnlyDictionary<string, string>? extraHeaders = null) =>
|
||||
await Send(CreateRequest(method, path, body: null, extraHeaders));
|
||||
|
||||
// TODO: rate limits
|
||||
private async Task<HttpResponseMessage> Send(HttpRequestMessage request)
|
||||
{
|
||||
var resp = await _client.SendAsync(request);
|
||||
if (!resp.IsSuccessStatusCode)
|
||||
{
|
||||
var error = await resp.Content.ReadAsJsonAsync<RestException.ErrorResponse>();
|
||||
throw new RestException(resp.StatusCode, error!);
|
||||
}
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
private HttpRequestMessage CreateRequest(HttpMethod method, string path, object? body,
|
||||
IReadOnlyDictionary<string, string>? extraHeaders = null)
|
||||
{
|
||||
var request = new HttpRequestMessage(method, _baseUrl + path);
|
||||
request.Headers.Add("Authorization", _token);
|
||||
if (body != null)
|
||||
{
|
||||
request.Content = new ReadOnlyMemoryContent(JsonSerializer.SerializeToUtf8Bytes(body));
|
||||
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json", "utf-8");
|
||||
}
|
||||
|
||||
if (extraHeaders != null)
|
||||
foreach (var (k, v) in extraHeaders)
|
||||
request.Headers.Add(k, v);
|
||||
|
||||
return request;
|
||||
}
|
||||
}
|
28
Foxcord/Rest/RestException.cs
Normal file
28
Foxcord/Rest/RestException.cs
Normal file
|
@ -0,0 +1,28 @@
|
|||
using System.Net;
|
||||
using System.Text.Json.Nodes;
|
||||
using Foxcord.Serialization;
|
||||
|
||||
namespace Foxcord.Rest;
|
||||
|
||||
public class RestException : Exception
|
||||
{
|
||||
public HttpStatusCode StatusCode { get; private init; }
|
||||
public int Code { get; private init; }
|
||||
public new string Message { get; private init; }
|
||||
public string? Errors { get; private init; }
|
||||
|
||||
internal RestException(HttpStatusCode statusCode, ErrorResponse response)
|
||||
{
|
||||
StatusCode = statusCode;
|
||||
Code = response.Code;
|
||||
Message = response.Message;
|
||||
Errors = response.Errors?.ToJsonString();
|
||||
}
|
||||
|
||||
public enum ErrorCode
|
||||
{
|
||||
GeneralError = 0
|
||||
}
|
||||
|
||||
internal record ErrorResponse(int Code, string Message, JsonObject? Errors);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue