-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathRestClient.cs
More file actions
184 lines (169 loc) · 5.83 KB
/
Copy pathRestClient.cs
File metadata and controls
184 lines (169 loc) · 5.83 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
namespace BytecodeApi.Rest;
/// <summary>
/// Defines the base class for REST clients.
/// </summary>
public abstract class RestClient : IDisposable
{
internal bool Disposed;
/// <summary>
/// Gets the base URL of the REST service without a trailing slash.
/// </summary>
public string BaseUrl { get; }
/// <summary>
/// Gets or sets a <see cref="bool" /> value indicating whether to disable SSL validation.
/// </summary>
public bool DisableCertificateValidation
{
get => HttpClientHandler.ServerCertificateCustomValidationCallback == HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
set => HttpClientHandler.ServerCertificateCustomValidationCallback = value ? HttpClientHandler.DangerousAcceptAnyServerCertificateValidator : null;
}
/// <summary>
/// A <see cref="RestRequestOptions" /> object with options for REST requests.
/// </summary>
protected internal RestRequestOptions RequestOptions { get; set; }
/// <summary>
/// Gets the <see cref="System.Net.Http.HttpClient" /> that is used to process requests.
/// </summary>
protected internal HttpClient HttpClient { get; }
private readonly HttpClientHandler HttpClientHandler;
/// <summary>
/// Initializes a new instance of the <see cref="RestClient" /> class.
/// </summary>
/// <param name="baseUrl">The base URL of the REST service. Trailing slashes will be removed.</param>
protected RestClient(string baseUrl)
{
Check.ArgumentNull(baseUrl);
BaseUrl = baseUrl.TrimEnd('/');
RequestOptions = new();
HttpClientHandler = new();
HttpClient = new(HttpClientHandler);
}
/// <summary>
/// Releases all resources used by the current instance of the <see cref="RestClient" /> class.
/// </summary>
public void Dispose()
{
if (!Disposed)
{
HttpClient.Dispose();
Disposed = true;
}
}
/// <summary>
/// Performs a GET request on the specified URL.
/// </summary>
/// <param name="url">The URL to perform the request on.</param>
/// <returns>
/// A <see cref="RestRequest" /> object to be used to further refine, and then send the request.
/// </returns>
protected RestRequest Get(string url)
{
Check.ObjectDisposed<RestClient>(Disposed);
Check.ArgumentNull(url);
return new(this, HttpMethod.Get, url);
}
/// <summary>
/// Performs a POST request on the specified URL.
/// </summary>
/// <param name="url">The URL to perform the request on.</param>
/// <returns>
/// A <see cref="RestRequest" /> object to be used to further refine, and then send the request.
/// </returns>
protected RestRequest Post(string url)
{
Check.ObjectDisposed<RestClient>(Disposed);
Check.ArgumentNull(url);
return new(this, HttpMethod.Post, url);
}
/// <summary>
/// Performs a PUT request on the specified URL.
/// </summary>
/// <param name="url">The URL to perform the request on.</param>
/// <returns>
/// A <see cref="RestRequest" /> object to be used to further refine, and then send the request.
/// </returns>
protected RestRequest Put(string url)
{
Check.ObjectDisposed<RestClient>(Disposed);
Check.ArgumentNull(url);
return new(this, HttpMethod.Put, url);
}
/// <summary>
/// Performs a PATCH request on the specified URL.
/// </summary>
/// <param name="url">The URL to perform the request on.</param>
/// <returns>
/// A <see cref="RestRequest" /> object to be used to further refine, and then send the request.
/// </returns>
protected RestRequest Patch(string url)
{
Check.ObjectDisposed<RestClient>(Disposed);
Check.ArgumentNull(url);
return new(this, HttpMethod.Patch, url);
}
/// <summary>
/// Performs a DELETE request on the specified URL.
/// </summary>
/// <param name="url">The URL to perform the request on.</param>
/// <returns>
/// A <see cref="RestRequest" /> object to be used to further refine, and then send the request.
/// </returns>
protected RestRequest Delete(string url)
{
Check.ObjectDisposed<RestClient>(Disposed);
Check.ArgumentNull(url);
return new(this, HttpMethod.Delete, url);
}
/// <summary>
/// Performs a PURGE request on the specified URL.
/// </summary>
/// <param name="url">The URL to perform the request on.</param>
/// <returns>
/// A <see cref="RestRequest" /> object to be used to further refine, and then send the request.
/// </returns>
protected RestRequest Purge(string url)
{
Check.ObjectDisposed<RestClient>(Disposed);
Check.ArgumentNull(url);
return new(this, new("PURGE"), url);
}
/// <summary>
/// Performs a HEAD request on the specified URL.
/// </summary>
/// <param name="url">The URL to perform the request on.</param>
/// <returns>
/// A <see cref="RestRequest" /> object to be used to further refine, and then send the request.
/// </returns>
protected RestRequest Head(string url)
{
Check.ObjectDisposed<RestClient>(Disposed);
Check.ArgumentNull(url);
return new(this, HttpMethod.Head, url);
}
/// <summary>
/// Performs a OPTIONS request on the specified URL.
/// </summary>
/// <param name="url">The URL to perform the request on.</param>
/// <returns>
/// A <see cref="RestRequest" /> object to be used to further refine, and then send the request.
/// </returns>
protected RestRequest Options(string url)
{
Check.ObjectDisposed<RestClient>(Disposed);
Check.ArgumentNull(url);
return new(this, HttpMethod.Options, url);
}
/// <summary>
/// Performs a TRACE request on the specified URL.
/// </summary>
/// <param name="url">The URL to perform the request on.</param>
/// <returns>
/// A <see cref="RestRequest" /> object to be used to further refine, and then send the request.
/// </returns>
protected RestRequest Trace(string url)
{
Check.ObjectDisposed<RestClient>(Disposed);
Check.ArgumentNull(url);
return new(this, HttpMethod.Trace, url);
}
}