Skip to content

Commit 6a06546

Browse files
[TASKSCLOUD-250] - Added async version of all existed API methods.
1 parent 17a036e commit 6a06546

File tree

7 files changed

+4151
-0
lines changed

7 files changed

+4151
-0
lines changed

Aspose.Tasks.Cloud.Sdk/Api/TasksApiAsync.cs

Lines changed: 3950 additions & 0 deletions
Large diffs are not rendered by default.

Aspose.Tasks.Cloud.Sdk/Internal/ApiInvoker.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
// </summary>
2424
// --------------------------------------------------------------------------------------------------------------------
2525

26+
using System.Threading.Tasks;
27+
2628
namespace Aspose.Tasks.Cloud.Sdk
2729
{
2830
using System;
@@ -57,6 +59,17 @@ public string InvokeApi(
5759
{
5860
return this.InvokeInternal(path, method, false, body, headerParams, formParams, contentType) as string;
5961
}
62+
63+
public async Task<string> InvokeApiAsync(
64+
string path,
65+
string method,
66+
string body = null,
67+
Dictionary<string, string> headerParams = null,
68+
Dictionary<string, object> formParams = null,
69+
string contentType = "application/json")
70+
{
71+
return await this.InvokeInternalAsync(path, method, false, body, headerParams, formParams, contentType) as string;
72+
}
6073

6174
public Stream InvokeBinaryApi(
6275
string path,
@@ -68,6 +81,17 @@ public Stream InvokeBinaryApi(
6881
{
6982
return (Stream)this.InvokeInternal(path, method, true, body, headerParams, formParams, contentType);
7083
}
84+
85+
public async Task<Stream> InvokeBinaryApiAsync(
86+
string path,
87+
string method,
88+
string body,
89+
Dictionary<string, string> headerParams,
90+
Dictionary<string, object> formParams,
91+
string contentType = "application/json")
92+
{
93+
return (Stream)await this.InvokeInternalAsync(path, method, true, body, headerParams, formParams, contentType);
94+
}
7195

7296
public FileInfo ToFileInfo(Stream stream, string paramName, string mimeType = "application/octet-stream")
7397
{
@@ -215,6 +239,70 @@ private object InvokeInternal(
215239
return this.ReadResponse(request, binaryResponse);
216240
}
217241
}
242+
243+
private async Task<object> InvokeInternalAsync(
244+
string path,
245+
string method,
246+
bool binaryResponse,
247+
string body,
248+
Dictionary<string, string> headerParams,
249+
Dictionary<string, object> formParams,
250+
string contentType)
251+
{
252+
if (formParams == null)
253+
{
254+
formParams = new Dictionary<string, object>();
255+
}
256+
257+
if (headerParams == null)
258+
{
259+
headerParams = new Dictionary<string, string>();
260+
}
261+
262+
this.requestHandlers.ForEach(async p => path = await p.ProcessUrlAsync(path));
263+
264+
WebRequest request;
265+
try
266+
{
267+
request = this.PrepareRequest(path, method, formParams, headerParams, body, contentType);
268+
return await this.ReadResponseAsync(request, binaryResponse);
269+
}
270+
catch (NeedRepeatRequestException)
271+
{
272+
request = this.PrepareRequest(path, method, formParams, headerParams, body, contentType);
273+
return await this.ReadResponseAsync(request, binaryResponse);
274+
}
275+
}
276+
277+
private async Task<object> ReadResponseAsync(WebRequest client, bool binaryResponse)
278+
{
279+
var webResponse = (HttpWebResponse)await this.GetResponseAsync(client);
280+
var resultStream = new MemoryStream();
281+
282+
StreamHelper.CopyTo(webResponse.GetResponseStream(), resultStream);
283+
try
284+
{
285+
this.requestHandlers.ForEach(async p => await p.ProcessResponseAsync(webResponse, resultStream));
286+
287+
resultStream.Position = 0;
288+
if (binaryResponse)
289+
{
290+
return resultStream;
291+
}
292+
293+
using (var responseReader = new StreamReader(resultStream))
294+
{
295+
var responseData = responseReader.ReadToEnd();
296+
resultStream.Dispose();
297+
return responseData;
298+
}
299+
}
300+
catch (Exception)
301+
{
302+
resultStream.Dispose();
303+
throw;
304+
}
305+
}
218306

219307
private WebRequest PrepareRequest(string path, string method, Dictionary<string, object> formParams,
220308
Dictionary<string, string> headerParams, string body, string contentType)
@@ -353,5 +441,22 @@ private WebResponse GetResponse(WebRequest request)
353441
throw;
354442
}
355443
}
444+
445+
private async Task<WebResponse> GetResponseAsync(WebRequest request)
446+
{
447+
try
448+
{
449+
return await request.GetResponseAsync();
450+
}
451+
catch (WebException wex)
452+
{
453+
if (wex.Response != null)
454+
{
455+
return wex.Response;
456+
}
457+
458+
throw;
459+
}
460+
}
356461
}
357462
}

Aspose.Tasks.Cloud.Sdk/Internal/IRequestHandler.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
// </summary>
2424
// --------------------------------------------------------------------------------------------------------------------
2525

26+
using System.Threading.Tasks;
27+
2628
namespace Aspose.Tasks.Cloud.Sdk
2729
{
2830
using System.IO;
@@ -31,9 +33,13 @@ namespace Aspose.Tasks.Cloud.Sdk
3133
internal interface IRequestHandler
3234
{
3335
string ProcessUrl(string url);
36+
37+
Task<string> ProcessUrlAsync(string url);
3438

3539
void BeforeSend(WebRequest request, Stream streamToSend);
3640

3741
void ProcessResponse(HttpWebResponse response, Stream resultStream);
42+
43+
Task ProcessResponseAsync(HttpWebResponse response, Stream resultStream);
3844
}
3945
}

Aspose.Tasks.Cloud.Sdk/Internal/RequestHandlers/ApiExceptionRequestHandler.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
// </summary>
2424
// --------------------------------------------------------------------------------------------------------------------
2525

26+
using System.Threading.Tasks;
27+
2628
namespace Aspose.Tasks.Cloud.Sdk.RequestHandlers
2729
{
2830
using System;
@@ -39,6 +41,11 @@ public string ProcessUrl(string url)
3941
return url;
4042
}
4143

44+
public Task<string> ProcessUrlAsync(string url)
45+
{
46+
return Task.FromResult(url);
47+
}
48+
4249
public void BeforeSend(WebRequest request, Stream streamToSend)
4350
{
4451
}
@@ -51,6 +58,12 @@ public void ProcessResponse(HttpWebResponse response, Stream resultStream)
5158
}
5259
}
5360

61+
public Task ProcessResponseAsync(HttpWebResponse response, Stream resultStream)
62+
{
63+
ProcessResponse(response, resultStream);
64+
return Task.FromResult(0);
65+
}
66+
5467
private void ThrowApiException(HttpWebResponse webResponse, Stream resultStream)
5568
{
5669
try

Aspose.Tasks.Cloud.Sdk/Internal/RequestHandlers/AuthWithSignatureRequestHandler.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
// </summary>
2424
// --------------------------------------------------------------------------------------------------------------------
2525

26+
using System.Threading.Tasks;
27+
2628
namespace Aspose.Tasks.Cloud.Sdk.RequestHandlers
2729
{
2830
using System;
@@ -55,6 +57,11 @@ public string ProcessUrl(string url)
5557
return url;
5658
}
5759

60+
public Task<string> ProcessUrlAsync(string url)
61+
{
62+
return Task.FromResult(ProcessUrl(url));
63+
}
64+
5865
public void BeforeSend(WebRequest request, Stream streamToSend)
5966
{
6067
}
@@ -63,6 +70,11 @@ public void ProcessResponse(HttpWebResponse response, Stream resultStream)
6370
{
6471
}
6572

73+
public Task ProcessResponseAsync(HttpWebResponse response, Stream resultStream)
74+
{
75+
return Task.FromResult(0);
76+
}
77+
6678
private string Sign(string url)
6779
{
6880
UriBuilder uriBuilder = new UriBuilder(url);

Aspose.Tasks.Cloud.Sdk/Internal/RequestHandlers/DebugLogRequestHandler.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
// </summary>
2424
// --------------------------------------------------------------------------------------------------------------------
2525

26+
using System.Threading.Tasks;
27+
2628
namespace Aspose.Tasks.Cloud.Sdk.RequestHandlers
2729
{
2830
using System.Diagnostics;
@@ -44,6 +46,11 @@ public string ProcessUrl(string url)
4446
return url;
4547
}
4648

49+
public Task<string> ProcessUrlAsync(string url)
50+
{
51+
return Task.FromResult(url);
52+
}
53+
4754
public void BeforeSend(WebRequest request, Stream streamToSend)
4855
{
4956
if (this.configuration.DebugMode)
@@ -61,6 +68,12 @@ public void ProcessResponse(HttpWebResponse response, Stream resultStream)
6168
}
6269
}
6370

71+
public Task ProcessResponseAsync(HttpWebResponse response, Stream resultStream)
72+
{
73+
this.ProcessResponse(response, resultStream);
74+
return Task.FromResult(0);;
75+
}
76+
6477
private void LogRequest(WebRequest request, Stream streamToSend)
6578
{
6679
var header = string.Format("{0}: {1}", request.Method, request.RequestUri);

Aspose.Tasks.Cloud.Sdk/Internal/RequestHandlers/OAuthRequestHandler.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
// </summary>
2424
// --------------------------------------------------------------------------------------------------------------------
2525

26+
using System.Threading.Tasks;
27+
2628
namespace Aspose.Tasks.Cloud.Sdk.RequestHandlers
2729
{
2830
using System.Collections.Generic;
@@ -64,6 +66,21 @@ public string ProcessUrl(string url)
6466
return url;
6567
}
6668

69+
public async Task<string> ProcessUrlAsync(string url)
70+
{
71+
if (this.configuration.AuthType != AuthType.OAuth2)
72+
{
73+
return url;
74+
}
75+
76+
if (string.IsNullOrEmpty(this.accessToken))
77+
{
78+
await this.RequestTokenAsync();
79+
}
80+
81+
return url;
82+
}
83+
6784
public void BeforeSend(WebRequest request, Stream streamToSend)
6885
{
6986
if (this.configuration.AuthType != AuthType.OAuth2)
@@ -89,6 +106,21 @@ public void ProcessResponse(HttpWebResponse response, Stream resultStream)
89106
}
90107
}
91108

109+
public async Task ProcessResponseAsync(HttpWebResponse response, Stream resultStream)
110+
{
111+
if (this.configuration.AuthType != AuthType.OAuth2)
112+
{
113+
return;
114+
}
115+
116+
if (response.StatusCode == HttpStatusCode.Unauthorized)
117+
{
118+
await this.RequestTokenAsync();
119+
120+
throw new NeedRepeatRequestException();
121+
}
122+
}
123+
92124
private void RequestToken()
93125
{
94126
var requestUrl = this.configuration.ApiBaseUrl + "/connect/token";
@@ -108,6 +140,26 @@ private void RequestToken()
108140

109141
this.accessToken = result.AccessToken;
110142
}
143+
144+
private async Task RequestTokenAsync()
145+
{
146+
var requestUrl = this.configuration.ApiBaseUrl + "/connect/token";
147+
148+
var postData = "grant_type=client_credentials";
149+
postData += "&client_id=" + this.configuration.AppSid;
150+
postData += "&client_secret=" + this.configuration.AppKey;
151+
152+
var responseString = await this.apiInvoker.InvokeApiAsync(
153+
requestUrl,
154+
"POST",
155+
postData,
156+
contentType: "application/x-www-form-urlencoded");
157+
158+
var result =
159+
(GetAccessTokenResult)SerializationHelper.Deserialize(responseString, typeof(GetAccessTokenResult));
160+
161+
this.accessToken = result.AccessToken;
162+
}
111163

112164
private class GetAccessTokenResult
113165
{

0 commit comments

Comments
 (0)