Made interfaces for testing
This commit is contained in:
parent
be80cad0cc
commit
b0b78fa272
@ -1,4 +1,5 @@
|
||||
using Discord.API.Rest;
|
||||
using Discord.API.Rest.HttpClientWrapper;
|
||||
using Serilog;
|
||||
|
||||
namespace Discord.API;
|
||||
@ -12,16 +13,20 @@ public class DiscordClient
|
||||
private GatewayClient? Gateway;
|
||||
private TimeProvider TimeProvider;
|
||||
|
||||
public DiscordClient(string api_key) : this(api_key, TimeProvider.System){
|
||||
}
|
||||
|
||||
internal DiscordClient(string api_key, TimeProvider time_provider){
|
||||
this.TimeProvider=time_provider;
|
||||
public DiscordClient(string api_key){
|
||||
TimeProvider = TimeProvider.System;
|
||||
this.ApiKey = api_key;
|
||||
Rest = new RestClient(ApiBaseUrl, ApiKey);
|
||||
Task.Run(ConnectGateway);
|
||||
}
|
||||
|
||||
internal DiscordClient(string api_key, TimeProvider time_provider, IHttpClientWrapper http_client){
|
||||
this.TimeProvider=time_provider;
|
||||
this.ApiKey=api_key;
|
||||
Rest = new RestClient(ApiKey, http_client);
|
||||
Task.Run(ConnectGateway);
|
||||
}
|
||||
|
||||
private async Task ConnectGateway(){
|
||||
RestResponse<GetGatewayResponse> resp = await Rest.GetGateway();
|
||||
|
||||
|
||||
45
Discord.API/Rest/HttpClientWrapper/HttpClientWrapper.cs
Normal file
45
Discord.API/Rest/HttpClientWrapper/HttpClientWrapper.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Runtime.Versioning;
|
||||
|
||||
namespace Discord.API.Rest.HttpClientWrapper;
|
||||
|
||||
public class HttpClientWrapper : HttpClient, IHttpClientWrapper{
|
||||
//
|
||||
// Summary:
|
||||
// Initializes a new instance of the System.Net.Http.HttpClient class using a System.Net.Http.HttpClientHandler
|
||||
// that is disposed when this instance is disposed.
|
||||
public HttpClientWrapper() : base() { }
|
||||
//
|
||||
// Summary:
|
||||
// Initializes a new instance of the System.Net.Http.HttpClient class with the specified
|
||||
// handler. The handler is disposed when this instance is disposed.
|
||||
//
|
||||
// Parameters:
|
||||
// handler:
|
||||
// The HTTP handler stack to use for sending requests.
|
||||
//
|
||||
// Exceptions:
|
||||
// T:System.ArgumentNullException:
|
||||
// The handler is null.
|
||||
public HttpClientWrapper(HttpMessageHandler handler) : base(handler) { }
|
||||
//
|
||||
// Summary:
|
||||
// Initializes a new instance of the System.Net.Http.HttpClient class with the provided
|
||||
// handler, and specifies whether that handler should be disposed when this instance
|
||||
// is disposed.
|
||||
//
|
||||
// Parameters:
|
||||
// handler:
|
||||
// The System.Net.Http.HttpMessageHandler responsible for processing the HTTP response
|
||||
// messages.
|
||||
//
|
||||
// disposeHandler:
|
||||
// true if the inner handler should be disposed of by HttpClient.Dispose; false
|
||||
// if you intend to reuse the inner handler.
|
||||
//
|
||||
// Exceptions:
|
||||
// T:System.ArgumentNullException:
|
||||
// The handler is null.
|
||||
public HttpClientWrapper(HttpMessageHandler handler, bool disposeHandler) : base(handler, disposeHandler) { }
|
||||
}
|
||||
1327
Discord.API/Rest/HttpClientWrapper/IHttpClientWrapper.cs
Normal file
1327
Discord.API/Rest/HttpClientWrapper/IHttpClientWrapper.cs
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,20 +1,22 @@
|
||||
using System.Text.Json;
|
||||
using Discord.API.Rest.HttpClientWrapper;
|
||||
using Serilog;
|
||||
using Serilog.Core;
|
||||
|
||||
namespace Discord.API.Rest;
|
||||
|
||||
internal class RestClient{
|
||||
|
||||
private HttpClient httpClient;
|
||||
private IHttpClientWrapper httpClient;
|
||||
|
||||
public RestClient(string base_url, string api_key){
|
||||
Log.Debug("REST: Creating new rest client. Base url: {base_url}", base_url);
|
||||
httpClient = new HttpClient(){
|
||||
BaseAddress=new Uri(base_url)
|
||||
};
|
||||
public RestClient(string base_url, string api_key)
|
||||
: this(api_key, new HttpClientWrapper.HttpClientWrapper() {BaseAddress = new Uri(base_url)})
|
||||
{ }
|
||||
|
||||
internal RestClient(string api_key, IHttpClientWrapper http_client){
|
||||
Log.Debug("REST: Creating new rest client. Base url: {base_url}", http_client.BaseAddress?.AbsoluteUri);
|
||||
httpClient = http_client;
|
||||
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bot {api_key}");
|
||||
httpClient.DefaultRequestHeaders.Add("User-Agent", $"DiscordBot ({Discord.API.StaticProperties.LibraryWebsite}, {Discord.API.StaticProperties.LibraryVersion})");
|
||||
httpClient.DefaultRequestHeaders.Add("User-Agent", $"DiscordBot ({StaticProperties.LibraryWebsite}, {StaticProperties.LibraryVersion})");
|
||||
}
|
||||
|
||||
public async Task<RestResponse<GetGatewayResponse>> GetGateway() =>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user