-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathThirdwebHttpClient.cs
More file actions
151 lines (137 loc) · 5.72 KB
/
Copy pathThirdwebHttpClient.cs
File metadata and controls
151 lines (137 loc) · 5.72 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
namespace Thirdweb;
/// <summary>
/// Represents a HTTP client for the Thirdweb SDK.
/// </summary>
public class ThirdwebHttpClient : IThirdwebHttpClient
{
/// <summary>
/// Gets the headers for the HTTP client.
/// </summary>
public Dictionary<string, string> Headers { get; private set; }
private readonly HttpClient _httpClient;
private bool _disposed;
/// <summary>
/// Initializes a new instance of the <see cref="ThirdwebHttpClient"/> class.
/// </summary>
public ThirdwebHttpClient()
{
this._httpClient = new HttpClient();
this.Headers = new Dictionary<string, string>();
}
/// <summary>
/// Sets the headers for the HTTP client.
/// </summary>
/// <param name="headers">The headers to set.</param>
public void SetHeaders(Dictionary<string, string> headers)
{
this.Headers = new Dictionary<string, string>(headers);
}
/// <summary>
/// Clears all headers from the HTTP client.
/// </summary>
public void ClearHeaders()
{
this.Headers.Clear();
}
/// <summary>
/// Adds a header to the HTTP client.
/// </summary>
/// <param name="key">The header key.</param>
/// <param name="value">The header value.</param>
public void AddHeader(string key, string value)
{
this.Headers.Add(key, value);
}
/// <summary>
/// Removes a header from the HTTP client.
/// </summary>
/// <param name="key">The header key.</param>
public void RemoveHeader(string key)
{
_ = this.Headers.Remove(key);
}
private void AddHeaders(HttpRequestMessage request)
{
foreach (var header in this.Headers)
{
_ = request.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
}
/// <summary>
/// Sends a GET request to the specified URI.
/// </summary>
/// <param name="requestUri">The request URI.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the HTTP response message.</returns>
public async Task<ThirdwebHttpResponseMessage> GetAsync(string requestUri, CancellationToken cancellationToken = default)
{
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
this.AddHeaders(request);
var result = await this._httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);
#pragma warning disable CA2016 // Forward the 'CancellationToken' parameter to methods
var resultContent = new ThirdwebHttpContent(await result.Content.ReadAsByteArrayAsync().ConfigureAwait(false));
#pragma warning restore CA2016 // Forward the 'CancellationToken' parameter to methods
return new ThirdwebHttpResponseMessage((long)result.StatusCode, resultContent, result.IsSuccessStatusCode);
}
/// <summary>
/// Sends a POST request to the specified URI.
/// </summary>
/// <param name="requestUri">The request URI.</param>
/// <param name="content">The HTTP content to send.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the HTTP response message.</returns>
public async Task<ThirdwebHttpResponseMessage> PostAsync(string requestUri, HttpContent content, CancellationToken cancellationToken = default)
{
var request = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = content };
this.AddHeaders(request);
var result = await this._httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);
#pragma warning disable CA2016 // Forward the 'CancellationToken' parameter to methods
var resultContent = new ThirdwebHttpContent(await result.Content.ReadAsByteArrayAsync().ConfigureAwait(false));
#pragma warning restore CA2016 // Forward the 'CancellationToken' parameter to methods
return new ThirdwebHttpResponseMessage((long)result.StatusCode, resultContent, result.IsSuccessStatusCode);
}
/// <summary>
/// Sends a PUT request to the specified URI.
/// </summary>
/// <param name="requestUri">The request URI.</param>
/// <param name="content">The HTTP content to send.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the HTTP response message.</returns>
public Task<ThirdwebHttpResponseMessage> PutAsync(string requestUri, HttpContent content, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
/// <summary>
/// Sends a DELETE request to the specified URI.
/// </summary>
/// <param name="requestUri">The request URI.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the HTTP response message.</returns>
public Task<ThirdwebHttpResponseMessage> DeleteAsync(string requestUri, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
/// <summary>
/// Disposes the HTTP client.
/// </summary>
/// <param name="disposing">Whether the client is being disposed.</param>
protected virtual void Dispose(bool disposing)
{
if (!this._disposed)
{
if (disposing)
{
this._httpClient.Dispose();
}
this._disposed = true;
}
}
/// <summary>
/// Disposes the HTTP client.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
}