Skip to content
This repository was archived by the owner on Oct 14, 2025. It is now read-only.

Commit b7a74bd

Browse files
committed
chore(Core) make code more efficient and thread-safe
Used ConfigureAwait(false) to free up the calling thread to do other work while the asynchronous operation is in progress, which can improve the overall throughput and concurrency of application
1 parent 6f70c55 commit b7a74bd

File tree

11 files changed

+38
-18
lines changed

11 files changed

+38
-18
lines changed

ReversoAPI.Web.Tests/Parsers/ConjugationResponseParserTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using ReversoAPI.Web.Entities;
33
using ReversoAPI.Web.Tools.Parsers;
44
using ReversoAPI.Web.Values;
5-
using ReversoAPI.Web.Values.ConjugationObjects;
65
using System.Collections.Generic;
76
using System.IO;
87
using System.Text;

ReversoAPI.Web/Builders/ConjugationParseBuilder.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using ReversoAPI.Web.Entities;
33
using ReversoAPI.Web.Exceptions;
44
using ReversoAPI.Web.Values;
5-
using ReversoAPI.Web.Values.ConjugationObjects;
65
using System;
76
using System.Collections.Generic;
87
using System.Linq;

ReversoAPI.Web/Clients/ConjugationClient.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ public async Task<ConjugationData> GetAsync(string text, Language language)
4242

4343
var url = CombineUrl(text, language);
4444

45-
using var response = await _apiConnector.GetAsync(url);
45+
using var response = await _apiConnector
46+
.GetAsync(url)
47+
.ConfigureAwait(false);
48+
4649
if (!response.IsHtml()) throw new FormatException("Response does not match html format");
4750

4851
return _parser.Invoke(response.Content);

ReversoAPI.Web/Clients/ContextClient.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ public async Task<ContextData> GetAsync(string text, Language source, Language t
2828

2929
var url = CombineUrl(text, source, target);
3030

31-
using var response = await _apiConnector.GetAsync(url);
31+
using var response = await _apiConnector
32+
.GetAsync(url)
33+
.ConfigureAwait(false);
34+
3235
if (!response.IsHtml()) throw new FormatException("Response does not match html format");
3336

3437
return _parser.Invoke(response.Content);

ReversoAPI.Web/Clients/PronunciationClient.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public async Task<Stream> GetAsync(string text, Language language, int speed = 1
4343
// TO DO: add text validation (length)
4444

4545
var url = CombineUrl(text, language, speed);
46-
var response = await _apiConnector.GetAsync(url);
46+
47+
var response = await _apiConnector
48+
.GetAsync(url)
49+
.ConfigureAwait(false);
4750

4851
return response.Content;
4952
}

ReversoAPI.Web/Clients/SpellingClient.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ public async Task<SpellingData> GetAsync(string text, Language language, Locale
3131
if (!_supportedLanguades.Contains(language)) throw new NotSupportedException($"'{language}' is not supported");
3232
// TO DO: Add locale validation
3333

34-
using var response = await _apiConnector.PostAsync(new Uri(SpellingURL), new SpellingRequest(text, language, locale));
34+
using var response = await _apiConnector
35+
.PostAsync(new Uri(SpellingURL), new SpellingRequest(text, language, locale))
36+
.ConfigureAwait(false);
37+
3538
var spellingDto = response.Content.Deserialize<SpellingResponse>();
3639

3740
return spellingDto.ToModel();

ReversoAPI.Web/Clients/SynonymsClient.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ public async Task<SynonymsData> GetAsync(string text, Language language)
2828

2929
var url = CombineUrl(text, language);
3030

31-
using var response = await _apiConnector.GetAsync(url);
31+
using var response = await _apiConnector
32+
.GetAsync(url)
33+
.ConfigureAwait(false);
34+
3235
if (!response.IsHtml()) throw new FormatException("Response does not match html format");
3336

3437
return _parser.Invoke(response.Content);

ReversoAPI.Web/Clients/TranslationClient.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ public async Task<TranslationData> GetAsync(string text, Language source, Langua
2222
if (string.IsNullOrEmpty(text)) return null;
2323
if (source == target) throw new ArgumentException("Source and Target languages are similar"); // maybe should rid of this
2424

25-
using var response = await _apiConnector.PostAsync(new Uri(TranslationURL), new TranslationRequest(text, source, target));
25+
using var response = await _apiConnector
26+
.PostAsync(new Uri(TranslationURL), new TranslationRequest(text, source, target))
27+
.ConfigureAwait(false);
28+
2629
var translationDto = response.Content.Deserialize<TranslationResponse>();
2730

2831
return translationDto.ToModel();

ReversoAPI.Web/Entities/ConjugationData.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using ReversoAPI.Web.Values;
2-
using ReversoAPI.Web.Values.ConjugationObjects;
32
using System.Collections.Generic;
43

54
namespace ReversoAPI.Web.Entities

ReversoAPI.Web/Http/APIConnector.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,17 @@ public async Task<HttpResponse> GetAsync(Uri uri)
4343
.Handle<HttpRequestException>()
4444
.OrResult<HttpResponseMessage>(r => _httpStatusCodesWorthRetrying.Contains(r.StatusCode))
4545
.WaitAndRetryAsync(RetryAttemptCount, attempt => TimeSpan.FromSeconds(Math.Pow(2, attempt)))
46-
.ExecuteAsync(() => _httpClient.GetAsync(uri));
47-
48-
var content = await response.Content.ReadAsStreamAsync();
46+
.ExecuteAsync(() => _httpClient.GetAsync(uri))
47+
.ConfigureAwait(false);
4948

49+
var content = await response.Content
50+
.ReadAsStreamAsync()
51+
.ConfigureAwait(false);
5052

5153
return new HttpResponse
5254
{
5355
ContentType = response.Content.Headers.ContentType.MediaType,
54-
Content = await MakeACopyAsync(content),
56+
Content = await MakeACopyAsync(content).ConfigureAwait(false),
5557
};
5658
}
5759

@@ -67,21 +69,24 @@ public async Task<HttpResponse> PostAsync(Uri uri, object payload)
6769
.Handle<HttpRequestException>()
6870
.OrResult<HttpResponseMessage>(r => _httpStatusCodesWorthRetrying.Contains(r.StatusCode))
6971
.WaitAndRetryAsync(RetryAttemptCount, attempt => TimeSpan.FromSeconds(Math.Pow(2, attempt)))
70-
.ExecuteAsync(() => _httpClient.PostAsync(uri, data));
72+
.ExecuteAsync(() => _httpClient.PostAsync(uri, data))
73+
.ConfigureAwait(false);
7174

72-
var content = await response.Content.ReadAsStreamAsync();
75+
var content = await response.Content
76+
.ReadAsStreamAsync()
77+
.ConfigureAwait(false);
7378

7479
return new HttpResponse
7580
{
7681
ContentType = response.Content.Headers.ContentType.MediaType,
77-
Content = await MakeACopyAsync(content),
82+
Content = await MakeACopyAsync(content).ConfigureAwait(false),
7883
};
7984
}
8085

8186
private async Task<Stream> MakeACopyAsync(Stream stream)
8287
{
8388
var ms = new MemoryStream();
84-
await stream.CopyToAsync(ms);
89+
await stream.CopyToAsync(ms).ConfigureAwait(false);
8590
ms.Position = 0;
8691
return ms;
8792
}

0 commit comments

Comments
 (0)