Implement GetOrCreateApplicationAuthentication

Implements the endpoint for creating an application authorization token.
This commit is contained in:
Haacked
2013-10-09 16:28:59 -07:00
parent 9d71d406fa
commit 33ad79c0fe
19 changed files with 653 additions and 4 deletions
@@ -0,0 +1,37 @@
using System;
using System.Reactive.Linq;
using Octokit.Reactive;
namespace Octokit
{
public static class AuthorizationExtensions
{
public static IObservable<Authorization> GetOrCreateApplicationAuthentication(
this IObservableAuthorizationsClient authorizationsClient,
string clientId,
string clientSecret,
AuthorizationUpdate authorization,
Func<TwoFactorRequiredException, IObservable<TwoFactorChallengeResult>> twoFactorChallengeHandler
)
{
Ensure.ArgumentNotNull(authorizationsClient, "authorizationsClient");
Ensure.ArgumentNotNullOrEmptyString(clientId, "clientId");
Ensure.ArgumentNotNullOrEmptyString(clientSecret, "clientSecret");
Ensure.ArgumentNotNull(authorization, "authorization");
return authorizationsClient.GetOrCreateApplicationAuthentication(clientId, clientSecret, authorization)
.Catch<Authorization, TwoFactorRequiredException>(exception => twoFactorChallengeHandler(exception)
.SelectMany(result =>
result.ResendCodeRequested
? authorizationsClient.GetOrCreateApplicationAuthentication(
clientId,
clientSecret,
authorization,
twoFactorChallengeHandler)
: authorizationsClient.GetOrCreateApplicationAuthentication(clientId,
clientSecret,
authorization,
result.AuthenticationCode)));
}
}
}