Skip to content

Commit

Permalink
Update docs, fix Client.Request
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSLevy committed Oct 5, 2019
1 parent d9dc34a commit cecb07e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
19 changes: 10 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,27 @@ type Client struct {
// and params, and then parses the Response using the provided result, which
// should be a pointer so that it may be populated.
//
// If ctx is not nil, it will be added to the http.Request.
// If ctx is not nil, it is added to the http.Request.
//
// If an Error Response is received, then an Error type is returned. Other
// potential errors can result from json.Marshal and params, json.Unmarshal and
// result, http.NewRequest and url, or network errors from c.Do.
//
// A pseudorandom uint between 1 and 5000 is used for the Request.ID.
//
// The http.Request will have the "Content-Type":"application/json" header
// added and then c.Header will be added, which may override "Content-Type".
// The "Content-Type":"application/json" header is added to the http.Request,
// and then headers in c.Header are added, which may override the
// "Content-Type".
//
// If c.BasicAuth is true then http.Request.SetBasicAuth(c.User, c.Password)
// will be called.
// If c.BasicAuth is true then http.Request.SetBasicAuth(c.User, c.Password) is
// be called.
//
// If c.DebugRequest is true then the Request and Response will be printed
// using c.Log. If c.Log == nil, then c.Log = log.New(os.Stderr, "", 0).
// If c.DebugRequest is true then the Request and Response are printed using
// c.Log. If c.Log == nil, then c.Log = log.New(os.Stderr, "", 0).
func (c *Client) Request(
ctx context.Context, url, method string, params, result interface{}) error {
// Generate a random ID for this request.
reqID := rand.Uint()%5000 + 1
reqID := rand.Int()%5000 + 1

// Marshal the JSON RPC Request.
reqJrpc := Request{ID: reqID, Method: method, Params: params}
Expand Down Expand Up @@ -122,7 +123,7 @@ func (c *Client) Request(
}

// Unmarshal the HTTP response into a JSON RPC response.
var resID uint32
var resID int
resJrpc := Response{Result: result, ID: &resID}
if err := json.Unmarshal(resBytes, &resJrpc); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const (
)

// IsReserved returns true if c is within the reserved error code range:
// [LowestReservedErrorCode, HighestReservedErrorCode].
// [LowestReservedErrorCode, HighestReservedErrorCode]
func (c ErrorCode) IsReserved() bool {
return ErrorCodeMinReserved <= c && c <= ErrorCodeMaxReserved
}
Expand Down
2 changes: 1 addition & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
// The returned http.HandlerFunc efficiently handles any conforming single or
// batch requests or notifications, accurately catches all defined protocol
// errors, calls the appropriate MethodFuncs, recovers from any panics or
// invalid return values, and, returns an Internal Error or Response with the
// invalid return values, and returns an Internal Error or Response with the
// correct ID, if not a Notification.
//
// See MethodFunc for more details.
Expand Down
32 changes: 18 additions & 14 deletions methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ import (
"runtime"
)

// DebugMethodFunc controls whether additional debug information will be
// printed to stdout in the event of an InternalError when a MethodFunc is
// called. This can be helpful when troubleshooting MethodFuncs.
// DebugMethodFunc controls whether additional debug information is printed to
// stdout in the event of an InternalError when a MethodFunc is called.
//
// This can be helpful when troubleshooting panics or Internal Errors from a
// MethodFunc.
var DebugMethodFunc = false

var logger = log.New(os.Stdout, "", 0)
Expand All @@ -49,28 +51,30 @@ type MethodMap map[string]MethodFunc
// MethodFunc is the function signature used for RPC methods.
//
// MethodFuncs are invoked by the HTTPRequestHandler when a valid Request is
// parsed. MethodFuncs do not need to concern themselves with the details of
// received. MethodFuncs do not need to concern themselves with the details of
// JSON-RPC 2.0 outside of the "params" field, as all parsing and validation is
// handled by the handler.
//
// The handler will call a MethodFunc with ctx set to http.Request.Context()
// and params set to JSON from the "params" field of the Request. If "params"
// was omitted or null, params will be nil. Otherwise, params is guaranteed to
// be valid JSON that represents a JSON Object or Array.
// The handler will call a MethodFunc with ctx set to the corresponding
// http.Request.Context() and params set to the JSON data from the "params"
// field of the Request. If "params" was omitted or null, params will be nil.
// Otherwise, params is guaranteed to be valid JSON that represents a JSON
// Object or Array.
//
// A MethodFunc is responsible for application specific parsing of params. A
// MethodFunc should return an ErrorInvalidParams if there is any issue parsing
// expected parameters.
//
// To return a success Response to the client, a MethodFunc may return any
// non-nil, non-error interface{} that does not cause an error when passed to
// json.Marshal, as the returned interface{} will be used as the
// Response.Result.
// non-nil, non-error value to be used as the Response.Result. Thus, any return
// value must not cause an error when passed to json.Marshal, else an Internal
// Error is returned to the client.
//
// To return an Error Response to the client, a MethodFunc may return an Error
// value or pointer. Returned Errors other than InvalidParams must use an
// Error.Code outside of the reserved range, else an Internal Error is returned
// instead.
// value. Any returned Error, except for InvalidParams, must use an Error.Code
// outside of the reserved range and the Error.Data must not cause an error
// when passed to json.Marshal, else an Internal Error is returned instead. See
// ErrorCode.IsReserved for more details.
//
// If a MethodFunc panics or returns any other error, an Internal Error is
// returned to the client.
Expand Down

0 comments on commit cecb07e

Please sign in to comment.