-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #310 from Itamaram/async-token-store
Allow fetching tokens asynchrnously
- Loading branch information
Showing
7 changed files
with
82 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// ITokenStore.cs | ||
// IAsyncTokenStore.cs | ||
// | ||
// Author: | ||
// Jarl Gullberg <[email protected]> | ||
|
@@ -20,6 +20,8 @@ | |
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
// | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
|
||
namespace Remora.Discord.Rest; | ||
|
@@ -28,12 +30,14 @@ namespace Remora.Discord.Rest; | |
/// Represents a storage class for a single token. | ||
/// </summary> | ||
[PublicAPI] | ||
public interface ITokenStore | ||
public interface IAsyncTokenStore | ||
{ | ||
/// <summary> | ||
/// Gets the token. | ||
/// </summary> | ||
string Token { get; } | ||
/// <param name="cancellationToken">A cancellation token to cancel operation.</param> | ||
/// <returns>The token's value.</returns> | ||
ValueTask<string> GetTokenAsync(CancellationToken cancellationToken); | ||
|
||
/// <summary> | ||
/// Gets the type of the token. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// TokenStore.cs | ||
// StaticTokenStore.cs | ||
// | ||
// Author: | ||
// Jarl Gullberg <[email protected]> | ||
|
@@ -20,30 +20,34 @@ | |
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
// | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
|
||
namespace Remora.Discord.Rest; | ||
|
||
/// <summary> | ||
/// Represents a storage class for a single token. | ||
/// Represents a storage class for a static token. | ||
/// </summary> | ||
[PublicAPI] | ||
public class TokenStore : ITokenStore | ||
public class StaticTokenStore : IAsyncTokenStore | ||
{ | ||
private readonly string _token; | ||
|
||
/// <inheritdoc /> | ||
public string Token { get; } | ||
public ValueTask<string> GetTokenAsync(CancellationToken cancellationToken) => new(_token); | ||
|
||
/// <inheritdoc /> | ||
public DiscordTokenType TokenType { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="TokenStore"/> class. | ||
/// Initializes a new instance of the <see cref="StaticTokenStore"/> class. | ||
/// </summary> | ||
/// <param name="token">The token to store.</param> | ||
/// <param name="tokenType">The type of token to store.</param> | ||
public TokenStore(string token, DiscordTokenType tokenType) | ||
public StaticTokenStore(string token, DiscordTokenType tokenType) | ||
{ | ||
this.Token = token; | ||
_token = token; | ||
this.TokenType = tokenType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// TokenStoreTests.cs | ||
// StaticTokenStoreTests.cs | ||
// | ||
// Author: | ||
// Jarl Gullberg <[email protected]> | ||
|
@@ -20,6 +20,7 @@ | |
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
// | ||
|
||
using System.Threading.Tasks; | ||
using Remora.Discord.Rest; | ||
using Xunit; | ||
|
||
|
@@ -29,41 +30,41 @@ | |
namespace Remora.Discord.Tests.Tests.Core; | ||
|
||
/// <summary> | ||
/// Tests the <see cref="TokenStore"/> class. | ||
/// Tests the <see cref="StaticTokenStore"/> class. | ||
/// </summary> | ||
public class TokenStoreTests | ||
public class StaticTokenStoreTests | ||
{ | ||
/// <summary> | ||
/// Tests the <see cref="TokenStore.Token"/> property. | ||
/// Tests the <see cref="Token"/> property. | ||
/// </summary> | ||
public class Token | ||
{ | ||
[Fact] | ||
public void ReturnsCorrectValue() | ||
public async Task ReturnsCorrectValue() | ||
{ | ||
var tokenStore = new TokenStore("Hello world!", DiscordTokenType.Bearer); | ||
var tokenStore = new StaticTokenStore("Hello world!", DiscordTokenType.Bearer); | ||
|
||
Assert.Equal("Hello world!", tokenStore.Token); | ||
Assert.Equal("Hello world!", await tokenStore.GetTokenAsync(default)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Tests the <see cref="TokenStore.TokenType"/> property. | ||
/// Tests the <see cref="TokenType"/> property. | ||
/// </summary> | ||
public class TokenType | ||
{ | ||
[Fact] | ||
public void ReturnsCorrectValueForBotTokenType() | ||
{ | ||
var tokenStore = new TokenStore("Hello world!", DiscordTokenType.Bot); | ||
var tokenStore = new StaticTokenStore("Hello world!", DiscordTokenType.Bot); | ||
|
||
Assert.Equal(DiscordTokenType.Bot, tokenStore.TokenType); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsCorrectValueForBearerTokenType() | ||
{ | ||
var tokenStore = new TokenStore("Hello world!", DiscordTokenType.Bearer); | ||
var tokenStore = new StaticTokenStore("Hello world!", DiscordTokenType.Bearer); | ||
|
||
Assert.Equal(DiscordTokenType.Bearer, tokenStore.TokenType); | ||
} | ||
|