Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. public interface IAction
  2. {
  3. GatewayResponseHandlerResult Run();
  4. }
  5.  
  6. public abstract class GatewayResponseHandler
  7. {
  8. protected GatewayResponseHandlerResult Result = new GatewayResponseHandlerResult();
  9.  
  10. private GatewayResponseHandlerResult Handle(GatewayResponse Response)
  11. {
  12. //specific handling code omitted
  13. }
  14. }
  15.  
  16. public class GatewayResponseHandlerResult
  17. {
  18. public bool Succeeded;
  19. public bool Failed;
  20. public bool Errored;
  21. public object Result { get; set; }
  22.  
  23. public void SetAsSuccess()
  24. {
  25. Succeeded = true;
  26. Failed = false;
  27. Errored = false;
  28. }
  29.  
  30. public void SetAsFailure()
  31. {
  32. Succeeded = false;
  33. Failed = true;
  34. Errored = false;
  35. }
  36.  
  37. public void SetAsError()
  38. {
  39. Succeeded = false;
  40. Failed = false;
  41. Errored = true;
  42. }
  43. }
  44.  
  45. public sealed class LoginAction : IAction
  46. {
  47. private string Username;
  48. private string Password;
  49.  
  50. public LoginAction(string Username, string Password)
  51. {
  52. this.Username = Username;
  53. this.Password = Password;
  54. }
  55.  
  56. public GatewayResponseHandlerResult Run()
  57. {
  58. try
  59. {
  60. GatewayResponse LoginResponse = GatewayRequest.Login(Username, Password);
  61. return GatewayResponseHandler.Handle(LoginResponse);
  62. }
  63. catch (Exception ex)
  64. {
  65. throw ex.Log();
  66. }
  67. }
  68. }
  69.  
  70. sealed class LoginHandler : GatewayResponseHandler
  71. {
  72. protected override bool CanHandle()
  73. {
  74. return Response.action == GatewayAction.LOGIN;
  75. }
  76.  
  77. protected override GatewayResponseHandlerResult ProcessError(Exception ex = null)
  78. {
  79. Result.SetAsError();
  80. return Result;
  81. }
  82.  
  83. protected override GatewayResponseHandlerResult ProcessFailure()
  84. {
  85. Result.SetAsFailure();
  86. return Result;
  87. }
  88.  
  89. protected override GatewayResponseHandlerResult ProcessSuccess()
  90. {
  91. try
  92. {
  93. if (Response.result_code == (int)ResponseCodes.SuccessCode.MFA_REQUIRED)
  94. {
  95. //specific MFA handling omitted
  96. }
  97. else if (Response.result_code == (int)ResponseCodes.SuccessCode.PW_RESET_REQUIRED)
  98. {
  99. //specific password reset code omitted
  100. }
  101.  
  102. MessageBox.Show("Login successful");
  103. Result.SetAsSuccess();
  104. }
  105. catch (Exception Ex)
  106. {
  107. ProcessError(Ex);
  108. Result.SetAsError();
  109. }
  110. return Result;
  111. }
  112. }
  113.  
  114. public interface IActionArgs{}
  115.  
  116. public class LoginActionArgs : IActionArgs
  117. {
  118. public string Username;
  119. public string Password;
  120. }
  121.  
  122. public abstract class Action
  123. {
  124. public static GatewayResponseHandlerResult Run(IActionArgs ActionArgs)
  125. {
  126. throw new NotImplementedException();
  127. }
  128. }
  129.  
  130. public sealed class LoginAction : IAction
  131. {
  132. public static new GatewayResponseHandlerResult Run(IActionArgs ActionArgs)
  133. {
  134. //code omitted
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement