Skip to content

Commit 7dc873a

Browse files
authored
Merge pull request #10521 from studentutu/feature/mrtk-rest-cancelation-token
feature(Rest): added cancelation token for all rest requests, fix #10517
2 parents ddf2432 + 3aa6ffb commit 7dc873a

File tree

1 file changed

+102
-8
lines changed
  • Assets/MRTK/Core/Utilities/WebRequestRest

1 file changed

+102
-8
lines changed

Assets/MRTK/Core/Utilities/WebRequestRest/Rest.cs

Lines changed: 102 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Text;
8+
using System.Threading;
89
using System.Threading.Tasks;
910
using UnityEngine;
1011
using UnityEngine.Networking;
@@ -55,14 +56,26 @@ public static string GetBearerOAuthToken(string authToken)
5556
/// <param name="certificateHandler">Optional certificate handler for custom certificate verification</param>
5657
/// <param name="disposeCertificateHandlerOnDispose">Optional bool. If true and <paramref name="certificateHandler"/> is not null, <paramref name="certificateHandler"/> will be disposed, when the underlying UnityWebRequest is disposed.</param>
5758
/// <returns>The response data.</returns>
58-
public static async Task<Response> GetAsync(string query, Dictionary<string, string> headers = null, int timeout = -1, DownloadHandler downloadHandler = null, bool readResponseData = false, CertificateHandler certificateHandler = null, bool disposeCertificateHandlerOnDispose = true)
59+
public static async Task<Response> GetAsync(
60+
string query,
61+
Dictionary<string, string> headers = null,
62+
int timeout = -1,
63+
DownloadHandler downloadHandler = null,
64+
bool readResponseData = false,
65+
CertificateHandler certificateHandler = null,
66+
bool disposeCertificateHandlerOnDispose = true,
67+
CancellationToken cancellationToken = default(CancellationToken))
5968
{
6069
using (var webRequest = UnityWebRequest.Get(query))
6170
{
6271
if (downloadHandler != null)
6372
{
6473
webRequest.downloadHandler = downloadHandler;
6574
}
75+
cancellationToken.Register(() =>
76+
{
77+
webRequest.Abort();
78+
});
6679

6780
return await ProcessRequestAsync(webRequest, timeout, headers, readResponseData, certificateHandler, disposeCertificateHandlerOnDispose);
6881
}
@@ -82,10 +95,21 @@ public static async Task<Response> GetAsync(string query, Dictionary<string, str
8295
/// <param name="certificateHandler">Optional certificate handler for custom certificate verification</param>
8396
/// <param name="disposeCertificateHandlerOnDispose">Optional bool. If true and <paramref name="certificateHandler"/> is not null, <paramref name="certificateHandler"/> will be disposed, when the underlying UnityWebRequest is disposed.</param>
8497
/// <returns>The response data.</returns>
85-
public static async Task<Response> PostAsync(string query, Dictionary<string, string> headers = null, int timeout = -1, bool readResponseData = false, CertificateHandler certificateHandler = null, bool disposeCertificateHandlerOnDispose = true)
98+
public static async Task<Response> PostAsync(
99+
string query,
100+
Dictionary<string, string> headers = null,
101+
int timeout = -1,
102+
bool readResponseData = false,
103+
CertificateHandler certificateHandler = null,
104+
bool disposeCertificateHandlerOnDispose = true,
105+
CancellationToken cancellationToken = default(CancellationToken))
86106
{
87107
using (var webRequest = UnityWebRequest.Post(query, null as string))
88108
{
109+
cancellationToken.Register(() =>
110+
{
111+
webRequest.Abort();
112+
});
89113
return await ProcessRequestAsync(webRequest, timeout, headers, readResponseData, certificateHandler, disposeCertificateHandlerOnDispose);
90114
}
91115
}
@@ -101,10 +125,21 @@ public static async Task<Response> PostAsync(string query, Dictionary<string, st
101125
/// <param name="certificateHandler">Optional certificate handler for custom certificate verification</param>
102126
/// <param name="disposeCertificateHandlerOnDispose">Optional bool. If true and <paramref name="certificateHandler"/> is not null, <paramref name="certificateHandler"/> will be disposed, when the underlying UnityWebRequest is disposed.</param>
103127
/// <returns>The response data.</returns>
104-
public static async Task<Response> PostAsync(string query, WWWForm formData, Dictionary<string, string> headers = null, int timeout = -1, bool readResponseData = false, CertificateHandler certificateHandler = null, bool disposeCertificateHandlerOnDispose = true)
128+
public static async Task<Response> PostAsync(
129+
string query,
130+
WWWForm formData,
131+
Dictionary<string, string> headers = null,
132+
int timeout = -1, bool readResponseData = false,
133+
CertificateHandler certificateHandler = null,
134+
bool disposeCertificateHandlerOnDispose = true,
135+
CancellationToken cancellationToken = default(CancellationToken))
105136
{
106137
using (var webRequest = UnityWebRequest.Post(query, formData))
107138
{
139+
cancellationToken.Register(() =>
140+
{
141+
webRequest.Abort();
142+
});
108143
return await ProcessRequestAsync(webRequest, timeout, headers, readResponseData, certificateHandler, disposeCertificateHandlerOnDispose);
109144
}
110145
}
@@ -120,10 +155,22 @@ public static async Task<Response> PostAsync(string query, WWWForm formData, Dic
120155
/// <param name="certificateHandler">Optional certificate handler for custom certificate verification</param>
121156
/// <param name="disposeCertificateHandlerOnDispose">Optional bool. If true and <paramref name="certificateHandler"/> is not null, <paramref name="certificateHandler"/> will be disposed, when the underlying UnityWebRequest is disposed.</param>
122157
/// <returns>The response data.</returns>
123-
public static async Task<Response> PostAsync(string query, string jsonData, Dictionary<string, string> headers = null, int timeout = -1, bool readResponseData = false, CertificateHandler certificateHandler = null, bool disposeCertificateHandlerOnDispose = true)
158+
public static async Task<Response> PostAsync(
159+
string query,
160+
string jsonData,
161+
Dictionary<string, string> headers = null,
162+
int timeout = -1,
163+
bool readResponseData = false,
164+
CertificateHandler certificateHandler = null,
165+
bool disposeCertificateHandlerOnDispose = true,
166+
CancellationToken cancellationToken = default(CancellationToken))
124167
{
125168
using (var webRequest = UnityWebRequest.Post(query, "POST"))
126169
{
170+
cancellationToken.Register(() =>
171+
{
172+
webRequest.Abort();
173+
});
127174
var data = new UTF8Encoding().GetBytes(jsonData);
128175
webRequest.uploadHandler = new UploadHandlerRaw(data);
129176
webRequest.downloadHandler = new DownloadHandlerBuffer();
@@ -144,10 +191,22 @@ public static async Task<Response> PostAsync(string query, string jsonData, Dict
144191
/// <param name="certificateHandler">Optional certificate handler for custom certificate verification</param>
145192
/// <param name="disposeCertificateHandlerOnDispose">Optional bool. If true and <paramref name="certificateHandler"/> is not null, <paramref name="certificateHandler"/> will be disposed, when the underlying UnityWebRequest is disposed.</param>
146193
/// <returns>The response data.</returns>
147-
public static async Task<Response> PostAsync(string query, byte[] bodyData, Dictionary<string, string> headers = null, int timeout = -1, bool readResponseData = false, CertificateHandler certificateHandler = null, bool disposeCertificateHandlerOnDispose = true)
194+
public static async Task<Response> PostAsync(
195+
string query,
196+
byte[] bodyData,
197+
Dictionary<string, string> headers = null,
198+
int timeout = -1,
199+
bool readResponseData = false,
200+
CertificateHandler certificateHandler = null,
201+
bool disposeCertificateHandlerOnDispose = true,
202+
CancellationToken cancellationToken = default(CancellationToken))
148203
{
149204
using (var webRequest = UnityWebRequest.Post(query, "POST"))
150205
{
206+
cancellationToken.Register(() =>
207+
{
208+
webRequest.Abort();
209+
});
151210
webRequest.uploadHandler = new UploadHandlerRaw(bodyData);
152211
webRequest.downloadHandler = new DownloadHandlerBuffer();
153212
webRequest.SetRequestHeader("Content-Type", "application/octet-stream");
@@ -170,10 +229,22 @@ public static async Task<Response> PostAsync(string query, byte[] bodyData, Dict
170229
/// <param name="certificateHandler">Optional certificate handler for custom certificate verification</param>
171230
/// <param name="disposeCertificateHandlerOnDispose">Optional bool. If true and <paramref name="certificateHandler"/> is not null, <paramref name="certificateHandler"/> will be disposed, when the underlying UnityWebRequest is disposed.</param>
172231
/// <returns>The response data.</returns>
173-
public static async Task<Response> PutAsync(string query, string jsonData, Dictionary<string, string> headers = null, int timeout = -1, bool readResponseData = false, CertificateHandler certificateHandler = null, bool disposeCertificateHandlerOnDispose = true)
232+
public static async Task<Response> PutAsync(
233+
string query,
234+
string jsonData,
235+
Dictionary<string, string> headers = null,
236+
int timeout = -1,
237+
bool readResponseData = false,
238+
CertificateHandler certificateHandler = null,
239+
bool disposeCertificateHandlerOnDispose = true,
240+
CancellationToken cancellationToken = default(CancellationToken))
174241
{
175242
using (var webRequest = UnityWebRequest.Put(query, jsonData))
176243
{
244+
cancellationToken.Register(() =>
245+
{
246+
webRequest.Abort();
247+
});
177248
webRequest.SetRequestHeader("Content-Type", "application/json");
178249
return await ProcessRequestAsync(webRequest, timeout, headers, readResponseData, certificateHandler, disposeCertificateHandlerOnDispose);
179250
}
@@ -190,10 +261,22 @@ public static async Task<Response> PutAsync(string query, string jsonData, Dicti
190261
/// <param name="certificateHandler">Optional certificate handler for custom certificate verification</param>
191262
/// <param name="disposeCertificateHandlerOnDispose">Optional bool. If true and <paramref name="certificateHandler"/> is not null, <paramref name="certificateHandler"/> will be disposed, when the underlying UnityWebRequest is disposed.</param>
192263
/// <returns>The response data.</returns>
193-
public static async Task<Response> PutAsync(string query, byte[] bodyData, Dictionary<string, string> headers = null, int timeout = -1, bool readResponseData = false, CertificateHandler certificateHandler = null, bool disposeCertificateHandlerOnDispose = true)
264+
public static async Task<Response> PutAsync(
265+
string query,
266+
byte[] bodyData,
267+
Dictionary<string, string> headers = null,
268+
int timeout = -1,
269+
bool readResponseData = false,
270+
CertificateHandler certificateHandler = null,
271+
bool disposeCertificateHandlerOnDispose = true,
272+
CancellationToken cancellationToken = default(CancellationToken))
194273
{
195274
using (var webRequest = UnityWebRequest.Put(query, bodyData))
196275
{
276+
cancellationToken.Register(() =>
277+
{
278+
webRequest.Abort();
279+
});
197280
webRequest.SetRequestHeader("Content-Type", "application/octet-stream");
198281
return await ProcessRequestAsync(webRequest, timeout, headers, readResponseData, certificateHandler, disposeCertificateHandlerOnDispose);
199282
}
@@ -213,10 +296,21 @@ public static async Task<Response> PutAsync(string query, byte[] bodyData, Dicti
213296
/// <param name="certificateHandler">Optional certificate handler for custom certificate verification</param>
214297
/// <param name="disposeCertificateHandlerOnDispose">Optional bool. If true and <paramref name="certificateHandler"/> is not null, <paramref name="certificateHandler"/> will be disposed, when the underlying UnityWebRequest is disposed.</param>
215298
/// <returns>The response data.</returns>
216-
public static async Task<Response> DeleteAsync(string query, Dictionary<string, string> headers = null, int timeout = -1, bool readResponseData = false, CertificateHandler certificateHandler = null, bool disposeCertificateHandlerOnDispose = true)
299+
public static async Task<Response> DeleteAsync(
300+
string query,
301+
Dictionary<string, string> headers = null,
302+
int timeout = -1,
303+
bool readResponseData = false,
304+
CertificateHandler certificateHandler = null,
305+
bool disposeCertificateHandlerOnDispose = true,
306+
CancellationToken cancellationToken = default(CancellationToken))
217307
{
218308
using (var webRequest = UnityWebRequest.Delete(query))
219309
{
310+
cancellationToken.Register(() =>
311+
{
312+
webRequest.Abort();
313+
});
220314
return await ProcessRequestAsync(webRequest, timeout, headers, readResponseData, certificateHandler, disposeCertificateHandlerOnDispose);
221315
}
222316
}

0 commit comments

Comments
 (0)