Browse Source

Document some more of the REST client stuff

pull/222/head
FiniteReality 9 years ago
parent
commit
596cba3d15
4 changed files with 13 additions and 3 deletions
  1. +5
    -3
      src/Discord.Net/Net/Rest/DefaultRestClient.cs
  2. +5
    -0
      src/Discord.Net/Net/Rest/IRestClient.cs
  3. +1
    -0
      src/Discord.Net/Net/Rest/RestClientProvider.cs
  4. +2
    -0
      src/Discord.Net/Rest/DiscordRestConfig.cs

+ 5
- 3
src/Discord.Net/Net/Rest/DefaultRestClient.cs View File

@@ -58,27 +58,28 @@ namespace Discord.Net.Rest
Dispose(true);
}

/// <summary> Sets a header to be used in REST requests. </summary>
/// <inheritdoc/>
public void SetHeader(string key, string value)
{
_client.DefaultRequestHeaders.Remove(key);
if (value != null)
_client.DefaultRequestHeaders.Add(key, value);
}
/// <summary> Sets the global cancellation token for any requests made by this instance. </summary>
/// <inheritdoc/>
public void SetCancelToken(CancellationToken cancelToken)
{
_parentToken = cancelToken;
_cancelToken = CancellationTokenSource.CreateLinkedTokenSource(_parentToken, _cancelTokenSource.Token).Token;
}

/// <summary> Sends a request with no body to the given endpoint. </summary>
/// <inheritdoc/>
public async Task<Stream> SendAsync(string method, string endpoint, bool headerOnly = false)
{
string uri = Path.Combine(_baseUrl, endpoint);
using (var restRequest = new HttpRequestMessage(GetMethod(method), uri))
return await SendInternalAsync(restRequest, headerOnly).ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task<Stream> SendAsync(string method, string endpoint, string json, bool headerOnly = false)
{
string uri = Path.Combine(_baseUrl, endpoint);
@@ -88,6 +89,7 @@ namespace Discord.Net.Rest
return await SendInternalAsync(restRequest, headerOnly).ConfigureAwait(false);
}
}
/// <inheritdoc/>
public async Task<Stream> SendAsync(string method, string endpoint, IReadOnlyDictionary<string, object> multipartParams, bool headerOnly = false)
{
string uri = Path.Combine(_baseUrl, endpoint);


+ 5
- 0
src/Discord.Net/Net/Rest/IRestClient.cs View File

@@ -8,11 +8,16 @@ namespace Discord.Net.Rest
//TODO: Add docstrings
public interface IRestClient
{
/// <summary> Sets a header to be used in REST requests. </summary>
void SetHeader(string key, string value);
/// <summary> Sets the global cancellation token for any requests made by this instance. </summary>
void SetCancelToken(CancellationToken cancelToken);

/// <summary> Sends a request with no body to the given endpoint. </summary>
Task<Stream> SendAsync(string method, string endpoint, bool headerOnly = false);
/// <summary> Sends a request with a body to the given endpoint. </summary>
Task<Stream> SendAsync(string method, string endpoint, string json, bool headerOnly = false);
/// <summary> Sends a multipart request with the given parameters to the given endpoint. </summary>
Task<Stream> SendAsync(string method, string endpoint, IReadOnlyDictionary<string, object> multipartParams, bool headerOnly = false);
}
}

+ 1
- 0
src/Discord.Net/Net/Rest/RestClientProvider.cs View File

@@ -1,4 +1,5 @@
namespace Discord.Net.Rest
{
/// <summary> A delegate for creating a user-defined implementation of <see cref="IRestClient"/> </summary>
public delegate IRestClient RestClientProvider(string baseUrl);
}

+ 2
- 0
src/Discord.Net/Rest/DiscordRestConfig.cs View File

@@ -2,8 +2,10 @@

namespace Discord.Rest
{
/// <summary> A set of common configuration options for REST clients. </summary>
public class DiscordRestConfig : DiscordConfig
{
/// <summary> Gets the user agent used in REST API calls </summary>
public static string UserAgent { get; } = $"DiscordBot (https://github.com/RogueException/Discord.Net, v{Version})";
internal const int RestTimeout = 10000;


Loading…
Cancel
Save