/
BaseRestRequest.cs
141 lines (121 loc) · 4.81 KB
/
BaseRestRequest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DisCatSharp.Net;
/// <summary>
/// Represents a request sent over HTTP.
/// </summary>
public abstract class BaseRestRequest
{
/// <summary>
/// Gets the discord client.
/// </summary>
protected internal BaseDiscordClient? Discord { get; }
/// <summary>
/// Gets the oauth2 client.
/// </summary>
protected internal DiscordOAuth2Client? OAuth2Client { get; }
/// <summary>
/// Gets the request task source.
/// </summary>
protected internal TaskCompletionSource<RestResponse> RequestTaskSource { get; }
/// <summary>
/// Gets the url to which this request is going to be made.
/// </summary>
public Uri Url { get; }
/// <summary>
/// Gets the HTTP method used for this request.
/// </summary>
public RestRequestMethod Method { get; }
/// <summary>
/// Gets the generic path (no parameters) for this request.
/// </summary>
public string Route { get; }
/// <summary>
/// Gets the headers sent with this request.
/// </summary>
public IReadOnlyDictionary<string, string>? Headers { get; } = null;
/// <summary>
/// Gets the override for the rate limit bucket wait time.
/// </summary>
public double? RateLimitWaitOverride { get; }
/// <summary>
/// Gets the rate limit bucket this request is in.
/// </summary>
internal RateLimitBucket RateLimitBucket { get; }
/// <summary>
/// Creates a new <see cref="BaseRestRequest"/> with specified parameters.
/// </summary>
/// <param name="client"><see cref="DiscordClient"/> from which this request originated.</param>
/// <param name="bucket">Rate limit bucket to place this request in.</param>
/// <param name="url">Uri to which this request is going to be sent to.</param>
/// <param name="method">Method to use for this request,</param>
/// <param name="route">The generic route the request url will use.</param>
/// <param name="headers">Additional headers for this request.</param>
/// <param name="ratelimitWaitOverride">Override for ratelimit bucket wait time.</param>
internal BaseRestRequest(BaseDiscordClient client, RateLimitBucket bucket, Uri url, RestRequestMethod method, string route, IReadOnlyDictionary<string, string>? headers = null, double? ratelimitWaitOverride = null)
{
this.Discord = client;
this.RateLimitBucket = bucket;
this.RequestTaskSource = new();
this.Url = url;
this.Method = method;
this.Route = route;
this.RateLimitWaitOverride = ratelimitWaitOverride;
if (headers is null)
return;
headers = headers.Select(x => new KeyValuePair<string, string>(x.Key, Uri.EscapeDataString(x.Value)))
.ToDictionary(x => x.Key, x => x.Value);
this.Headers = headers;
}
/// <summary>
/// Creates a new <see cref="BaseRestRequest"/> with specified parameters.
/// </summary>
/// <param name="client"><see cref="DiscordOAuth2Client"/> from which this request originated.</param>
/// <param name="bucket">Rate limit bucket to place this request in.</param>
/// <param name="url">Uri to which this request is going to be sent to.</param>
/// <param name="method">Method to use for this request,</param>
/// <param name="route">The generic route the request url will use.</param>
/// <param name="headers">Additional headers for this request.</param>
/// <param name="ratelimitWaitOverride">Override for ratelimit bucket wait time.</param>
internal BaseRestRequest(DiscordOAuth2Client client, RateLimitBucket bucket, Uri url, RestRequestMethod method, string route, IReadOnlyDictionary<string, string>? headers = null, double? ratelimitWaitOverride = null)
{
this.OAuth2Client = client;
this.RateLimitBucket = bucket;
this.RequestTaskSource = new();
this.Url = url;
this.Method = method;
this.Route = route;
this.RateLimitWaitOverride = ratelimitWaitOverride;
if (headers is null)
return;
headers = headers.Select(x => new KeyValuePair<string, string>(x.Key, Uri.EscapeDataString(x.Value)))
.ToDictionary(x => x.Key, x => x.Value);
this.Headers = headers;
}
/// <summary>
/// Asynchronously waits for this request to complete.
/// </summary>
/// <returns>HTTP response to this request.</returns>
public Task<RestResponse> WaitForCompletionAsync()
=> this.RequestTaskSource.Task;
/// <summary>
/// Sets as completed.
/// </summary>
/// <param name="response">The response to set.</param>
protected internal void SetCompleted(RestResponse response)
=> this.RequestTaskSource.SetResult(response);
/// <summary>
/// Sets as faulted.
/// </summary>
/// <param name="ex">The exception to set.</param>
protected internal void SetFaulted(Exception ex)
=> this.RequestTaskSource.SetException(ex);
/// <summary>
/// Tries to set as faulted.
/// </summary>
/// <param name="ex">The exception to set.</param>
protected internal bool TrySetFaulted(Exception ex)
=> this.RequestTaskSource.TrySetException(ex);
}