RpcController.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using EdjCase.JsonRpc.Router.Abstractions;
  2. using EdjCase.JsonRpc.Router.Defaults;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. namespace EdjCase.JsonRpc.Router
  8. {
  9. public abstract class RpcController
  10. {
  11. /// <summary>
  12. /// Helper for returning a successful rpc response
  13. /// </summary>
  14. /// <param name="obj">Object to return in response</param>
  15. /// <returns>Success result for rpc response</returns>
  16. public virtual RpcMethodSuccessResult Ok(object obj = null)
  17. {
  18. return new RpcMethodSuccessResult(obj);
  19. }
  20. /// <summary>
  21. /// Helper for returning an error rpc response
  22. /// </summary>
  23. /// <param name="errorCode">JSON-RPC custom error code</param>
  24. /// <param name="message">(Optional)Error message</param>
  25. /// <param name="data">(Optional)Error data</param>
  26. /// <returns></returns>
  27. public virtual RpcMethodErrorResult Error(int errorCode, string message = null, object data = null)
  28. {
  29. return new RpcMethodErrorResult(errorCode, message, data);
  30. }
  31. }
  32. public abstract class RpcErrorFilterAttribute : Attribute
  33. {
  34. public abstract OnExceptionResult OnException(RpcRouteInfo routeInfo, Exception ex);
  35. }
  36. public class OnExceptionResult
  37. {
  38. public bool ThrowException { get; }
  39. public object ResponseObject { get; }
  40. private OnExceptionResult(bool throwException, object responseObject)
  41. {
  42. this.ThrowException = throwException;
  43. this.ResponseObject = responseObject;
  44. }
  45. public static OnExceptionResult UseObjectResponse(object responseObject)
  46. {
  47. return new OnExceptionResult(false, responseObject);
  48. }
  49. public static OnExceptionResult UseMethodResultResponse(IRpcMethodResult result)
  50. {
  51. return new OnExceptionResult(false, result);
  52. }
  53. public static OnExceptionResult UseExceptionResponse(Exception ex)
  54. {
  55. return new OnExceptionResult(true, ex);
  56. }
  57. public static OnExceptionResult DontHandle()
  58. {
  59. return new OnExceptionResult(true, null);
  60. }
  61. }
  62. #if !NETSTANDARD1_3
  63. /// <summary>
  64. /// Attribute to decorate a derived <see cref="RpcController"/> class
  65. /// </summary>
  66. public class RpcRouteAttribute : Attribute
  67. {
  68. /// <summary>
  69. /// Name of the route to be used in the router. If unspecified, will use controller name.
  70. /// </summary>
  71. public string RouteName { get; }
  72. /// <summary>
  73. ///
  74. /// </summary>
  75. /// <param name="routeName">(Optional) Name of the route to be used in the router. If unspecified, will use controller name.</param>
  76. /// <param name="routeGroup">(Optional) Name of the group the route is in to allow route filtering per request.</param>
  77. public RpcRouteAttribute(string routeName = null)
  78. {
  79. this.RouteName = routeName?.Trim();
  80. }
  81. }
  82. #endif
  83. }