Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IAction
- {
- GatewayResponseHandlerResult Run();
- }
- public abstract class GatewayResponseHandler
- {
- protected GatewayResponseHandlerResult Result = new GatewayResponseHandlerResult();
- private GatewayResponseHandlerResult Handle(GatewayResponse Response)
- {
- //specific handling code omitted
- }
- }
- public class GatewayResponseHandlerResult
- {
- public bool Succeeded;
- public bool Failed;
- public bool Errored;
- public object Result { get; set; }
- public void SetAsSuccess()
- {
- Succeeded = true;
- Failed = false;
- Errored = false;
- }
- public void SetAsFailure()
- {
- Succeeded = false;
- Failed = true;
- Errored = false;
- }
- public void SetAsError()
- {
- Succeeded = false;
- Failed = false;
- Errored = true;
- }
- }
- public sealed class LoginAction : IAction
- {
- private string Username;
- private string Password;
- public LoginAction(string Username, string Password)
- {
- this.Username = Username;
- this.Password = Password;
- }
- public GatewayResponseHandlerResult Run()
- {
- try
- {
- GatewayResponse LoginResponse = GatewayRequest.Login(Username, Password);
- return GatewayResponseHandler.Handle(LoginResponse);
- }
- catch (Exception ex)
- {
- throw ex.Log();
- }
- }
- }
- sealed class LoginHandler : GatewayResponseHandler
- {
- protected override bool CanHandle()
- {
- return Response.action == GatewayAction.LOGIN;
- }
- protected override GatewayResponseHandlerResult ProcessError(Exception ex = null)
- {
- Result.SetAsError();
- return Result;
- }
- protected override GatewayResponseHandlerResult ProcessFailure()
- {
- Result.SetAsFailure();
- return Result;
- }
- protected override GatewayResponseHandlerResult ProcessSuccess()
- {
- try
- {
- if (Response.result_code == (int)ResponseCodes.SuccessCode.MFA_REQUIRED)
- {
- //specific MFA handling omitted
- }
- else if (Response.result_code == (int)ResponseCodes.SuccessCode.PW_RESET_REQUIRED)
- {
- //specific password reset code omitted
- }
- MessageBox.Show("Login successful");
- Result.SetAsSuccess();
- }
- catch (Exception Ex)
- {
- ProcessError(Ex);
- Result.SetAsError();
- }
- return Result;
- }
- }
- public interface IActionArgs{}
- public class LoginActionArgs : IActionArgs
- {
- public string Username;
- public string Password;
- }
- public abstract class Action
- {
- public static GatewayResponseHandlerResult Run(IActionArgs ActionArgs)
- {
- throw new NotImplementedException();
- }
- }
- public sealed class LoginAction : IAction
- {
- public static new GatewayResponseHandlerResult Run(IActionArgs ActionArgs)
- {
- //code omitted
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement