Rest API groundworks
This commit is contained in:
parent
c65c371307
commit
82533d0c32
6
Discord.API/Rest/GetGatewayResponse.cs
Normal file
6
Discord.API/Rest/GetGatewayResponse.cs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
namespace Discord.API.Rest;
|
||||||
|
|
||||||
|
public readonly struct GetGatewayResponse
|
||||||
|
{
|
||||||
|
public required string Url {get; init;}
|
||||||
|
}
|
||||||
39
Discord.API/Rest/RestClient.cs
Normal file
39
Discord.API/Rest/RestClient.cs
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace Discord.API.Rest;
|
||||||
|
|
||||||
|
internal class RestClient{
|
||||||
|
|
||||||
|
private HttpClient httpClient;
|
||||||
|
|
||||||
|
public RestClient(string base_url, string api_key){
|
||||||
|
httpClient = new HttpClient(){
|
||||||
|
BaseAddress=new Uri(base_url)
|
||||||
|
};
|
||||||
|
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bot {api_key}");
|
||||||
|
httpClient.DefaultRequestHeaders.Add("User-Agent", $"DiscordBot ({Discord.API.StaticProperties.LibraryWebsite}, {Discord.API.StaticProperties.LibraryVersion})");
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<RestResponse<GetGatewayResponse>> GetGateway() =>
|
||||||
|
await Get("gateway", s => JsonSerializer.Deserialize(s, SourceGenerationContext.Default.GetGatewayResponse));
|
||||||
|
|
||||||
|
private async Task<RestResponse<T>> Get<T>(string url, Func<string, T> deserializer){
|
||||||
|
try{
|
||||||
|
HttpResponseMessage resp = await httpClient.GetAsync(url);
|
||||||
|
if(resp.IsSuccessStatusCode){
|
||||||
|
return new RestSuccessResponse<T>(
|
||||||
|
deserializer.Invoke(await resp.Content.ReadAsStringAsync()),
|
||||||
|
resp.StatusCode
|
||||||
|
);
|
||||||
|
}else{
|
||||||
|
return new RestErrorResponse<T>(
|
||||||
|
JsonSerializer.Deserialize(await resp.Content.ReadAsStringAsync(), SourceGenerationContext.Default.RestError),
|
||||||
|
resp.StatusCode
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}catch (Exception ex){
|
||||||
|
return new RestFailResponse<T>(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
7
Discord.API/Rest/RestError.cs
Normal file
7
Discord.API/Rest/RestError.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace Discord.API.Rest;
|
||||||
|
|
||||||
|
public readonly struct RestError
|
||||||
|
{
|
||||||
|
public required int Code {get; init;}
|
||||||
|
public required string Message {get; init;}
|
||||||
|
}
|
||||||
12
Discord.API/Rest/RestErrorResponse.cs
Normal file
12
Discord.API/Rest/RestErrorResponse.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using System.Net;
|
||||||
|
using Discord.API.Rest;
|
||||||
|
|
||||||
|
namespace Discord.API;
|
||||||
|
|
||||||
|
public class RestErrorResponse<T>(RestError error, HttpStatusCode code) : RestResponse<T>
|
||||||
|
{
|
||||||
|
public override T Value => throw new InvalidOperationException();
|
||||||
|
public override HttpStatusCode StatusCode => code;
|
||||||
|
public override bool Success => false;
|
||||||
|
public override RestError Error => error;
|
||||||
|
}
|
||||||
13
Discord.API/Rest/RestFailResponse.cs
Normal file
13
Discord.API/Rest/RestFailResponse.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
using System.Net;
|
||||||
|
using Discord.API.Rest;
|
||||||
|
|
||||||
|
namespace Discord.API;
|
||||||
|
|
||||||
|
public class RestFailResponse<T>(Exception? ex) : RestResponse<T>
|
||||||
|
{
|
||||||
|
public override T Value => throw new InvalidOperationException();
|
||||||
|
public override HttpStatusCode StatusCode => throw new InvalidOperationException();
|
||||||
|
public override bool Success => false;
|
||||||
|
public override RestError Error => throw new InvalidOperationException();
|
||||||
|
public Exception? Exception => ex;
|
||||||
|
}
|
||||||
12
Discord.API/Rest/RestResponse.cs
Normal file
12
Discord.API/Rest/RestResponse.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using System.Net;
|
||||||
|
using Discord.API.Rest;
|
||||||
|
|
||||||
|
namespace Discord.API;
|
||||||
|
|
||||||
|
public abstract class RestResponse<T>
|
||||||
|
{
|
||||||
|
public abstract T Value {get;}
|
||||||
|
public abstract HttpStatusCode StatusCode {get;}
|
||||||
|
public abstract bool Success {get;}
|
||||||
|
public abstract RestError Error {get;}
|
||||||
|
}
|
||||||
11
Discord.API/Rest/RestSuccessResponse.cs
Normal file
11
Discord.API/Rest/RestSuccessResponse.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
using System.Net;
|
||||||
|
|
||||||
|
namespace Discord.API.Rest;
|
||||||
|
|
||||||
|
public class RestSuccessResponse<T>(T response, HttpStatusCode code) : RestResponse<T>
|
||||||
|
{
|
||||||
|
public override T Value => response;
|
||||||
|
public override bool Success => true;
|
||||||
|
public override HttpStatusCode StatusCode => code;
|
||||||
|
public override RestError Error => throw new InvalidOperationException();
|
||||||
|
}
|
||||||
@ -14,12 +14,8 @@ namespace Discord.API;
|
|||||||
NumberHandling = JsonNumberHandling.AllowReadingFromString
|
NumberHandling = JsonNumberHandling.AllowReadingFromString
|
||||||
)]
|
)]
|
||||||
[JsonSerializable(typeof(GatewayPacket))]
|
[JsonSerializable(typeof(GatewayPacket))]
|
||||||
[JsonSerializable(typeof(IdentifyPacket))]
|
[JsonSerializable(typeof(Rest.GetGatewayResponse))]
|
||||||
[JsonSerializable(typeof(ChannelCreatePacket))]
|
[JsonSerializable(typeof(Rest.RestError))]
|
||||||
[JsonSerializable(typeof(ChannelUpdatePacket))]
|
|
||||||
[JsonSerializable(typeof(ChannelDeletePacket))]
|
|
||||||
[JsonSerializable(typeof(DispatchPacket))]
|
|
||||||
[JsonSerializable(typeof(ReadyPacket))]
|
|
||||||
internal partial class SourceGenerationContext : JsonSerializerContext
|
internal partial class SourceGenerationContext : JsonSerializerContext
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
|||||||
# Visual Studio Version 17
|
# Visual Studio Version 17
|
||||||
VisualStudioVersion = 17.0.31903.59
|
VisualStudioVersion = 17.0.31903.59
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "model", "model\model.csproj", "{2FA8D012-CC43-4FBC-9520-CF627AB4BBEA}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Discord.Model", "model\Discord.Model.csproj", "{2FA8D012-CC43-4FBC-9520-CF627AB4BBEA}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Discord.API", "Discord.API\Discord.API.csproj", "{5C77661B-670F-400B-AF77-E3EC062B673D}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Discord.API", "Discord.API\Discord.API.csproj", "{5C77661B-670F-400B-AF77-E3EC062B673D}"
|
||||||
EndProject
|
EndProject
|
||||||
@ -16,9 +16,6 @@ Global
|
|||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
Release|Any CPU = Release|Any CPU
|
Release|Any CPU = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
|
||||||
HideSolutionNode = FALSE
|
|
||||||
EndGlobalSection
|
|
||||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
{2FA8D012-CC43-4FBC-9520-CF627AB4BBEA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{2FA8D012-CC43-4FBC-9520-CF627AB4BBEA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{2FA8D012-CC43-4FBC-9520-CF627AB4BBEA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{2FA8D012-CC43-4FBC-9520-CF627AB4BBEA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
@ -37,4 +34,7 @@ Global
|
|||||||
{E89333A2-11C4-4CFF-9F45-32D951E871CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{E89333A2-11C4-4CFF-9F45-32D951E871CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{E89333A2-11C4-4CFF-9F45-32D951E871CA}.Release|Any CPU.Build.0 = Release|Any CPU
|
{E89333A2-11C4-4CFF-9F45-32D951E871CA}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user