jsonrpc2.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package utils
  2. import "encoding/json"
  3. type RpcErrorCode int
  4. const (
  5. ErrParse RpcErrorCode = -32700 // Parse error (解析错误)
  6. ErrInvalidRequest RpcErrorCode = -32600 // Invalid Request (无效请求)
  7. ErrMethodNotFound RpcErrorCode = -32601 // Method not found (无效方法)
  8. ErrInvalidParams RpcErrorCode = -32602 // Invalid params (无效参数)
  9. ErrInternal RpcErrorCode = -32603 // Internal error (内部错误)
  10. )
  11. type RpcRequest struct {
  12. JSONRPC string `json:"jsonrpc"` // 版本号, 固定: "2.0"
  13. Method string `json:"method"` // 调用方法, 执行函数名
  14. Params json.RawMessage `json:"params,omitempty"` // 请求参数, 可选可为空
  15. ID int `json:"id"` // 请求ID, 用于标识请求
  16. }
  17. type RpcError struct {
  18. Code RpcErrorCode `json:"code"` // 错误码
  19. Message string `json:"message"` // 错误信息
  20. }
  21. type RpcResponse struct {
  22. JSONRPC string `json:"jsonrpc"` // 版本号, 固定: "2.0"
  23. Result json.RawMessage `json:"result,omitempty"` // 响应结果, 可选可为空
  24. Error *RpcError `json:"error,omitempty"` // 错误信息, 可选可为空
  25. ID int `json:"id"` // 应答ID, 响应匹配请求
  26. }
  27. // 解析请求数据
  28. func ParseRequest(jsonStr string) (*RpcRequest, error) {
  29. var req RpcRequest
  30. if err := json.Unmarshal([]byte(jsonStr), &req); err != nil {
  31. return nil, err
  32. }
  33. return &req, nil
  34. }
  35. // 构建响应数据
  36. func BuildResponseResult(id int, result any) (*RpcResponse, error) {
  37. b, err := json.Marshal(result)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return &RpcResponse{
  42. JSONRPC: "2.0",
  43. Result: b,
  44. ID: id,
  45. }, nil
  46. }
  47. // 构建错误响应
  48. func BuildResponseError(id int, code RpcErrorCode, message string) *RpcResponse {
  49. return &RpcResponse{
  50. JSONRPC: "2.0",
  51. Error: &RpcError{
  52. Code: code,
  53. Message: message,
  54. },
  55. ID: id,
  56. }
  57. }
  58. // 构建解析错误
  59. func ResponseParseError(id int) *RpcResponse {
  60. return BuildResponseError(id, ErrParse, "Parse error")
  61. }
  62. // 构建无效请求
  63. func ResponseInvalidRequest(id int) *RpcResponse {
  64. return BuildResponseError(id, ErrInvalidRequest, "Invalid Request")
  65. }
  66. // 构建无效方法
  67. func ResponseMethodNotFound(id int) *RpcResponse {
  68. return BuildResponseError(id, ErrMethodNotFound, "Method not found")
  69. }
  70. // 构建无效参数
  71. func ResponseInvalidParams(id int) *RpcResponse {
  72. return BuildResponseError(id, ErrInvalidParams, "Invalid params")
  73. }
  74. // 构建内部错误
  75. func ResponseInternalError(id int) *RpcResponse {
  76. return BuildResponseError(id, ErrInternal, "Internal error")
  77. }
  78. // 构建用户错误
  79. func ResponseCustomError(id int, code RpcErrorCode, message string) *RpcResponse {
  80. return BuildResponseError(id, code, message)
  81. }
  82. // 应答转字符串
  83. func (resp *RpcResponse) ToString() (string, error) {
  84. b, err := json.Marshal(resp)
  85. if err != nil {
  86. return "", err
  87. }
  88. return string(b), nil
  89. }