Skip to content

Commit 4ec852c

Browse files
authored
Merge pull request #48 from Quppa/ITokenStoreAsyncOnly
Remove ITokenStore (non-async)
2 parents c4fd2a1 + d58e3d8 commit 4ec852c

33 files changed

+511
-482
lines changed

PayrollTests.AU/Integration/PayItems/Find.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
using NUnit.Framework;
1+
using System.Threading.Tasks;
2+
using NUnit.Framework;
23

34
namespace PayrollTests.AU.Integration.PayItems
45
{
56
[TestFixture]
67
public class Find : ApiWrapperTest
78
{
89
[Test]
9-
public async void find_all()
10+
public async Task find_all()
1011
{
1112
var items = await Api.PayItems.FindAsync();
1213
Assert.IsNotNull(items);
1314
}
1415

1516
[Test]
16-
public async void find_paged()
17+
public async Task find_paged()
1718
{
1819
var items = await Api.PayItems.Page(1).FindAsync();
1920
Assert.IsNotNull(items);
2021
}
2122

2223
[Test]
23-
public async void Find_EarningRates()
24+
public async Task Find_EarningRates()
2425
{
2526
var items = await Api.PayItems.FindAsync();
2627

Xero.Api.Example.Console/Authenticators/PartnerAuthenticator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace Xero.Api.Example.Console.Authenticators
66
{
77
public class PartnerAuthenticator : PartnerAuthenticatorBase
88
{
9-
public PartnerAuthenticator(ITokenStore store)
9+
public PartnerAuthenticator(ITokenStoreAsync store)
1010
: this(store, new XeroApiSettings())
1111
{
1212
}
1313

14-
public PartnerAuthenticator(ITokenStore store, IXeroApiSettings xeroApiSettings)
14+
public PartnerAuthenticator(ITokenStoreAsync store, IXeroApiSettings xeroApiSettings)
1515
: base(store, xeroApiSettings)
1616
{
1717
}

Xero.Api.Example.Console/Authenticators/PublicAuthenticator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace Xero.Api.Example.Console.Authenticators
66
{
77
public class PublicAuthenticator : PublicAuthenticatorBase
88
{
9-
public PublicAuthenticator(ITokenStore store)
9+
public PublicAuthenticator(ITokenStoreAsync store)
1010
: this(store, new XeroApiSettings())
1111
{
1212
}
1313

14-
public PublicAuthenticator(ITokenStore store, IXeroApiSettings xeroApiSettings)
14+
public PublicAuthenticator(ITokenStoreAsync store, IXeroApiSettings xeroApiSettings)
1515
: base(store, xeroApiSettings)
1616
{
1717
}
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
using System.Collections.Concurrent;
22
using System.Collections.Generic;
3+
using System.Threading.Tasks;
34
using Xero.Api.Infrastructure.Interfaces;
45

56
namespace Xero.Api.Example.Console.TokenStores
67
{
7-
public class MemoryTokenStore : ITokenStore
8+
public class MemoryTokenStore : ITokenStoreAsync
89
{
910
private readonly IDictionary<string, IToken> _tokens = new ConcurrentDictionary<string, IToken>();
1011

11-
public IToken Find(string userId)
12+
public Task<IToken> FindAsync(string userId)
1213
{
1314
if (string.IsNullOrWhiteSpace(userId))
1415
return null;
1516

1617
_tokens.TryGetValue(userId, out var token);
17-
return token;
18+
19+
return Task.FromResult(token);
1820
}
1921

20-
public void Add(IToken token)
22+
public Task AddAsync(IToken token)
2123
{
2224
_tokens[token.UserId] = token;
25+
26+
return Task.CompletedTask;
2327
}
2428

25-
public void Delete(IToken token)
29+
public Task DeleteAsync(IToken token)
2630
{
2731
if (_tokens.ContainsKey(token.UserId))
2832
{
2933
_tokens.Remove(token.UserId);
3034
}
35+
36+
return Task.CompletedTask;
3137
}
3238
}
3339
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
using System.Threading.Tasks;
12
using Xero.Api.Infrastructure.Interfaces;
23

34
namespace Xero.Api.Example.MVC.Authenticators
45
{
56
public interface IMvcAuthenticator
67
{
7-
string GetRequestTokenAuthorizeUrl(string userId);
8-
IToken RetrieveAndStoreAccessToken(string userId, string tokenKey, string verfier);
8+
Task<string> GetRequestTokenAuthorizeUrlAsync(string userId);
9+
Task<IToken> RetrieveAndStoreAccessTokenAsync(string userId, string tokenKey, string verifier);
910
}
1011
}

Xero.Api.Example.MVC/Authenticators/PartnerMvcAuthenticator.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23
using Xero.Api.Infrastructure.Authenticators;
34
using Xero.Api.Infrastructure.Interfaces;
45
using Xero.Api.Infrastructure.OAuth;
@@ -8,14 +9,14 @@ namespace Xero.Api.Example.MVC.Authenticators
89
public class PartnerMvcAuthenticator : PartnerAuthenticatorBase, IMvcAuthenticator
910
{
1011
private readonly Consumer _consumer;
11-
private readonly ITokenStore _requestTokenStore;
12+
private readonly ITokenStoreAsync _requestTokenStore;
1213

13-
public PartnerMvcAuthenticator(ITokenStore requestTokenStore, ITokenStore accessTokenStore)
14+
public PartnerMvcAuthenticator(ITokenStoreAsync requestTokenStore, ITokenStoreAsync accessTokenStore)
1415
: this(requestTokenStore, accessTokenStore, new XeroApiSettings())
1516
{
1617
}
1718

18-
public PartnerMvcAuthenticator(ITokenStore requestTokenStore, ITokenStore accessTokenStore, IXeroApiSettings xeroApiSettings)
19+
public PartnerMvcAuthenticator(ITokenStoreAsync requestTokenStore, ITokenStoreAsync accessTokenStore, IXeroApiSettings xeroApiSettings)
1920
: base(accessTokenStore, xeroApiSettings)
2021
{
2122
_consumer = new Consumer(ApplicationSettings.ConsumerKey, ApplicationSettings.ConsumerSecret);
@@ -27,46 +28,46 @@ protected override string AuthorizeUser(IToken oauthToken, string scope = null,
2728
throw new NotImplementedException();
2829
}
2930

30-
public string GetRequestTokenAuthorizeUrl(string userId)
31+
public async Task<string> GetRequestTokenAuthorizeUrlAsync(string userId)
3132
{
32-
var requestToken = GetRequestToken(_consumer);
33+
var requestToken = await GetRequestTokenAsync(_consumer);
3334
requestToken.UserId = userId;
3435

35-
var existingToken = _requestTokenStore.Find(userId);
36+
var existingToken = await _requestTokenStore.FindAsync(userId);
3637
if (existingToken != null)
37-
_requestTokenStore.Delete(requestToken);
38+
await _requestTokenStore.DeleteAsync(requestToken);
3839

39-
_requestTokenStore.Add(requestToken);
40+
await _requestTokenStore.AddAsync(requestToken);
4041

4142
return GetAuthorizeUrl(requestToken);
4243
}
4344

44-
public IToken RetrieveAndStoreAccessToken(string userId, string tokenKey, string verfier)
45+
public async Task<IToken> RetrieveAndStoreAccessTokenAsync(string userId, string tokenKey, string verifier)
4546
{
46-
var existingAccessToken = Store.Find(userId);
47+
var existingAccessToken = await Store.FindAsync(userId);
4748
if (existingAccessToken != null)
4849
{
4950
if (!existingAccessToken.HasExpired)
5051
return existingAccessToken;
5152
else
52-
Store.Delete(existingAccessToken);
53+
await Store.DeleteAsync(existingAccessToken);
5354
}
5455

55-
var requestToken = _requestTokenStore.Find(userId);
56+
var requestToken = await _requestTokenStore.FindAsync(userId);
5657
if (requestToken == null)
5758
throw new ApplicationException("Failed to look up request token for user");
5859

5960
//Delete the request token from the _requestTokenStore as the next few lines will render it useless for the future.
60-
_requestTokenStore.Delete(requestToken);
61+
await _requestTokenStore.DeleteAsync(requestToken);
6162

6263
if (requestToken.TokenKey != tokenKey)
6364
throw new ApplicationException("Request token key does not match");
6465

65-
var accessToken = Tokens.GetAccessTokenAsync(requestToken, GetAuthorization(requestToken, "POST", Tokens.AccessTokenEndpoint, null, verfier)).Result;
66+
var accessToken = await Tokens.GetAccessTokenAsync(requestToken, GetAuthorization(requestToken, "POST", Tokens.AccessTokenEndpoint, null, verifier));
6667

6768
accessToken.UserId = userId;
6869

69-
Store.Add(accessToken);
70+
await Store.AddAsync(accessToken);
7071

7172
return accessToken;
7273
}

Xero.Api.Example.MVC/Authenticators/PublicMvcAuthenticator.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23
using Xero.Api.Infrastructure.Authenticators;
34
using Xero.Api.Infrastructure.Exceptions;
45
using Xero.Api.Infrastructure.Interfaces;
@@ -9,14 +10,14 @@ namespace Xero.Api.Example.MVC.Authenticators
910
public class PublicMvcAuthenticator : PublicAuthenticatorBase, IMvcAuthenticator
1011
{
1112
private readonly IConsumer _consumer;
12-
private readonly ITokenStore _requestTokenStore;
13+
private readonly ITokenStoreAsync _requestTokenStore;
1314

14-
public PublicMvcAuthenticator(ITokenStore requestTokenStore, ITokenStore accessTokenStore)
15+
public PublicMvcAuthenticator(ITokenStoreAsync requestTokenStore, ITokenStoreAsync accessTokenStore)
1516
: this(requestTokenStore, accessTokenStore, new XeroApiSettings())
1617
{
1718
}
1819

19-
public PublicMvcAuthenticator(ITokenStore requestTokenStore, ITokenStore accessTokenStore, IXeroApiSettings xeroApiSettings)
20+
public PublicMvcAuthenticator(ITokenStoreAsync requestTokenStore, ITokenStoreAsync accessTokenStore, IXeroApiSettings xeroApiSettings)
2021
: base(accessTokenStore, xeroApiSettings)
2122
{
2223
_consumer = new Consumer(ApplicationSettings.ConsumerKey, ApplicationSettings.ConsumerSecret);
@@ -28,51 +29,51 @@ protected override string AuthorizeUser(IToken token, string scope = null, bool
2829
throw new NotSupportedException();
2930
}
3031

31-
protected override IToken RenewToken(IToken sessionToken, IConsumer consumer)
32+
protected override Task<IToken> RenewTokenAsync(IToken sessionToken, IConsumer consumer)
3233
{
3334
throw new RenewTokenException();
3435
}
3536

36-
public string GetRequestTokenAuthorizeUrl(string userId)
37+
public async Task<string> GetRequestTokenAuthorizeUrlAsync(string userId)
3738
{
38-
var requestToken = GetRequestToken(_consumer);
39+
var requestToken = await GetRequestTokenAsync(_consumer);
3940
requestToken.UserId = userId;
4041

41-
var existingToken = _requestTokenStore.Find(userId);
42+
var existingToken = await _requestTokenStore.FindAsync(userId);
4243
if (existingToken != null)
43-
_requestTokenStore.Delete(requestToken);
44+
await _requestTokenStore.DeleteAsync(requestToken);
4445

45-
_requestTokenStore.Add(requestToken);
46+
await _requestTokenStore.AddAsync(requestToken);
4647

4748
return GetAuthorizeUrl(requestToken);
4849
}
4950

50-
public IToken RetrieveAndStoreAccessToken(string userId, string tokenKey, string verfier)
51+
public async Task<IToken> RetrieveAndStoreAccessTokenAsync(string userId, string tokenKey, string verifier)
5152
{
52-
var existingAccessToken = Store.Find(userId);
53+
var existingAccessToken = await Store.FindAsync(userId);
5354
if (existingAccessToken != null)
5455
{
5556
if (!existingAccessToken.HasExpired)
5657
return existingAccessToken;
5758
else
58-
Store.Delete(existingAccessToken);
59+
await Store.DeleteAsync(existingAccessToken);
5960
}
6061

61-
var requestToken = _requestTokenStore.Find(userId);
62+
var requestToken = await _requestTokenStore.FindAsync(userId);
6263
if (requestToken == null)
6364
throw new ApplicationException("Failed to look up request token for user");
6465

65-
//Delete the request token from the _requestTokenStore as the next few lines will render it useless for the future.
66-
_requestTokenStore.Delete(requestToken);
66+
//Delete the request token from the _requestTokenStore as the next few lines will render it useless for the future.
67+
await _requestTokenStore.DeleteAsync(requestToken);
6768

6869
if (requestToken.TokenKey != tokenKey)
6970
throw new ApplicationException("Request token key does not match");
7071

71-
var accessToken = Tokens.GetAccessTokenAsync(requestToken, GetAuthorization(requestToken, "POST", Tokens.AccessTokenEndpoint, null, verfier)).Result;
72+
var accessToken = await Tokens.GetAccessTokenAsync(requestToken, GetAuthorization(requestToken, "POST", Tokens.AccessTokenEndpoint, null, verifier));
7273

7374
accessToken.UserId = userId;
7475

75-
Store.Add(accessToken);
76+
await Store.AddAsync(accessToken);
7677

7778
return accessToken;
7879
}

Xero.Api.Example.MVC/Controllers/HomeController.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using System.Threading.Tasks;
2+
using Microsoft.AspNetCore.Mvc;
23
using Microsoft.Extensions.Options;
34
using Xero.Api.Example.MVC.Helpers;
45
using Xero.Api.Infrastructure.OAuth;
@@ -23,16 +24,16 @@ public IActionResult Index()
2324
return View();
2425
}
2526

26-
public ActionResult Connect()
27+
public async Task<ActionResult> Connect()
2728
{
28-
var authorizeUrl = _authenticator.GetRequestTokenAuthorizeUrl(_user.Identifier);
29+
var authorizeUrl = await _authenticator.GetRequestTokenAuthorizeUrlAsync(_user.Identifier);
2930

3031
return Redirect(authorizeUrl);
3132
}
3233

33-
public ActionResult Authorize(string oauth_token, string oauth_verifier, string org)
34+
public async Task<ActionResult> Authorize(string oauth_token, string oauth_verifier, string org)
3435
{
35-
var accessToken = _authenticator.RetrieveAndStoreAccessToken(_user.Identifier, oauth_token, oauth_verifier);
36+
var accessToken = await _authenticator.RetrieveAndStoreAccessTokenAsync(_user.Identifier, oauth_token, oauth_verifier);
3637
if (accessToken == null)
3738
return View("NoAuthorized");
3839

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
using System.Collections.Concurrent;
22
using System.Collections.Generic;
3+
using System.Threading.Tasks;
34
using Xero.Api.Infrastructure.Interfaces;
45

56
namespace Xero.Api.Example.MVC.TokenStores
67
{
7-
public class MemoryTokenStore : ITokenStore
8+
public class MemoryTokenStore : ITokenStoreAsync
89
{
910
private readonly IDictionary<string, IToken> _tokens = new ConcurrentDictionary<string, IToken>();
1011

11-
public IToken Find(string userId)
12+
public Task<IToken> FindAsync(string userId)
1213
{
1314
if (string.IsNullOrWhiteSpace(userId))
1415
return null;
1516

1617
_tokens.TryGetValue(userId, out var token);
17-
return token;
18+
19+
return Task.FromResult(token);
1820
}
1921

20-
public void Add(IToken token)
22+
public Task AddAsync(IToken token)
2123
{
2224
_tokens[token.UserId] = token;
25+
26+
return Task.CompletedTask;
2327
}
2428

25-
public void Delete(IToken token)
29+
public Task DeleteAsync(IToken token)
2630
{
2731
if (_tokens.ContainsKey(token.UserId))
2832
{
2933
_tokens.Remove(token.UserId);
3034
}
35+
36+
return Task.CompletedTask;
3137
}
3238
}
3339
}

Xero.Api/Common/XeroReadEndpoint.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ public T UseFourDecimalPlaces(bool use4Dp)
7474
return Apply4Dp(use4Dp);
7575
}
7676

77-
public virtual async Task<IEnumerable<TResult>> FindAsync()
77+
public virtual Task<IEnumerable<TResult>> FindAsync()
7878
{
79-
return await GetAsync(ApiEndpointUrl, null).ConfigureAwait(false);
79+
return GetAsync(ApiEndpointUrl, null);
8080
}
8181

82-
public virtual async Task<TResult> FindAsync(Guid child)
82+
public virtual Task<TResult> FindAsync(Guid child)
8383
{
84-
return await FindAsync(child.ToString("D")).ConfigureAwait(false);
84+
return FindAsync(child.ToString("D"));
8585
}
8686

8787
public async Task<TResult> FindAsync(string child)

0 commit comments

Comments
 (0)