code.vegaprotocol.io/vega@v0.79.0/libs/jsonrpc/request.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package jsonrpc
    17  
    18  import (
    19  	"errors"
    20  )
    21  
    22  var (
    23  	ErrOnlySupportJSONRPC2 = errors.New("the API only supports JSON-RPC 2.0")
    24  	ErrMethodIsRequired    = errors.New("the method is required")
    25  )
    26  
    27  // Params is just a nicer way to describe what's passed to the handlers.
    28  type Params interface{}
    29  
    30  type Request struct {
    31  	// Version specifies the version of the JSON-RPC protocol.
    32  	// MUST be exactly "2.0".
    33  	Version string `json:"jsonrpc"`
    34  
    35  	// Method contains the name of the method to be invoked.
    36  	Method string `json:"method"`
    37  
    38  	// Params is a by-name Structured value that holds the parameter values to be
    39  	// used during the invocation of the method. This member MAY be omitted.
    40  	Params Params `json:"params,omitempty"`
    41  
    42  	// ID is an identifier established by the Client that MUST contain a String.
    43  	// If it is not included it is assumed to be a notification.
    44  	// The Server MUST reply with the same value in the Response object if included.
    45  	// This member is used to correlate the context between the two objects.
    46  	ID string `json:"id,omitempty"`
    47  }
    48  
    49  func (r *Request) Check() error {
    50  	if r.Version != VERSION2 {
    51  		return ErrOnlySupportJSONRPC2
    52  	}
    53  
    54  	if r.Method == "" {
    55  		return ErrMethodIsRequired
    56  	}
    57  
    58  	return nil
    59  }
    60  
    61  func (r *Request) IsNotification() bool {
    62  	return r.ID == ""
    63  }