RpcRequest.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System;
  4. // ReSharper disable AutoPropertyCanBeMadeGetOnly.Local
  5. // ReSharper disable UnusedMember.Local
  6. namespace EdjCase.JsonRpc.Core
  7. {
  8. /// <summary>
  9. /// Model representing a Rpc request
  10. /// </summary>
  11. public class RpcRequest
  12. {
  13. /// <param name="id">Request id</param>
  14. /// <param name="method">Target method name</param>
  15. /// <param name="parameterList">Json parameters for the target method</param>
  16. public RpcRequest(RpcId id, string method, RpcParameters parameters = default)
  17. {
  18. this.Id = id;
  19. this.Method = method;
  20. this.Parameters = parameters;
  21. }
  22. /// <param name="method">Target method name</param>
  23. /// <param name="parameterList">Json parameters for the target method</param>
  24. public RpcRequest(string method, RpcParameters parameters = default)
  25. {
  26. this.Id = null;
  27. this.Method = method;
  28. this.Parameters = parameters;
  29. }
  30. /// <summary>
  31. /// Request Id (Optional)
  32. /// </summary>
  33. public RpcId Id { get; private set; }
  34. /// <summary>
  35. /// Name of the target method (Required)
  36. /// </summary>
  37. public string Method { get; private set; }
  38. /// <summary>
  39. /// Parameters to invoke the method with (Optional)
  40. /// </summary>
  41. public RpcParameters Parameters { get; private set; }
  42. public static RpcRequest WithNoParameters(string method, RpcId id = default)
  43. {
  44. return RpcRequest.WithParameters(method, default, id);
  45. }
  46. public static RpcRequest WithParameterList(string method, IList<object> parameterList, RpcId id = default)
  47. {
  48. parameterList = parameterList ?? new object[0];
  49. RpcParameters parameters = RpcParameters.FromList(parameterList);
  50. return RpcRequest.WithParameters(method, parameters, id);
  51. }
  52. public static RpcRequest WithParameterMap(string method, IDictionary<string, object> parameterDictionary, RpcId id = default)
  53. {
  54. parameterDictionary = parameterDictionary ?? new Dictionary<string, object>();
  55. RpcParameters parameters = RpcParameters.FromDictionary(parameterDictionary);
  56. return RpcRequest.WithParameters(method, parameters, id);
  57. }
  58. public static RpcRequest WithParameters(string method, RpcParameters parameters, RpcId id = default)
  59. {
  60. if(method == null)
  61. {
  62. throw new ArgumentNullException(nameof(method));
  63. }
  64. return new RpcRequest(id, method, parameters);
  65. }
  66. }
  67. }