Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
add - Added the async version of functions
Browse files Browse the repository at this point in the history
We've added asynchronous functions.

---

We've added asynchronous version of the functions so that web developers can use this library.

---

Type: add
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Aug 13, 2023
1 parent dd5ff03 commit 54e7336
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Dictify/Dictify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
28 changes: 28 additions & 0 deletions Dictify/DictionaryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace Dictify.Manager
{
Expand Down Expand Up @@ -66,5 +67,32 @@ public static DictionaryWord[] GetWordInfo(string Word)
return CachedWords.ToArray();
}
}

/// <summary>
/// Gets the word information and puts it into an array of dictionary words
/// </summary>
public static async Task<DictionaryWord[]> GetWordInfoAsync(string Word)
{
if (CachedWords.Any((word) => word.Word == Word))
{
// We already have a word, so there is no reason to download it again
return CachedWords.Where((word) => word.Word == Word).ToArray();
}
else
{
// Download the word information
HttpResponseMessage Response = await DictClient.GetAsync($"https://api.dictionaryapi.dev/api/v2/entries/en/{Word}");
Response.EnsureSuccessStatusCode();
Stream WordInfoStream = await Response.Content.ReadAsStreamAsync();
string WordInfoString = new StreamReader(WordInfoStream).ReadToEnd();

// Serialize it to DictionaryWord to cache it so that we don't have to download it again
DictionaryWord[] Words = (DictionaryWord[])JsonConvert.DeserializeObject(WordInfoString, typeof(DictionaryWord[]));
CachedWords.AddRange(Words);

// Return the word
return CachedWords.ToArray();
}
}
}
}

0 comments on commit 54e7336

Please sign in to comment.