Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSLevy committed Nov 24, 2018
1 parent 1790fae commit c4af76b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
6 changes: 4 additions & 2 deletions Error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// github.com/AdamSLevy/jsonrpc2
// Copyright 2018 Adam S Levy. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
Expand All @@ -12,9 +11,10 @@ type Error struct {
Data interface{} `json:"data,omitempty"`
}

// Official JSON-RPC 2.0 Spec Error Codes and Messages
// ErrorCode represent the int JSON RPC 2.0 error code.
type ErrorCode int

// Official JSON-RPC 2.0 Spec Error Codes and Messages
const (
LowestReservedErrorCode ErrorCode = -32768
ParseErrorCode ErrorCode = -32700
Expand All @@ -31,6 +31,7 @@ const (
InternalErrorMessage = "Internal error"
)

// IsReserved returns true if c is within the reserved error code range.
func (c ErrorCode) IsReserved() bool {
return LowestReservedErrorCode <= c && c <= HighestReservedErrorCode
}
Expand All @@ -54,6 +55,7 @@ var (
InternalError = NewError(InternalErrorCode, InternalErrorMessage, nil)
)

// NewError returns an Error with the given code, message, and data.
func NewError(code ErrorCode, message string, data interface{}) Error {
return Error{Code: code, Message: message, Data: data}
}
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# jsonrpc2/v4
# jsonrpc2/v5
[![GoDoc](https://godoc.org/github.com/AdamSLevy/jsonrpc2?status.svg)](https://godoc.org/github.com/AdamSLevy/jsonrpc2)
[![Go Report Card](https://goreportcard.com/badge/github.com/AdamSLevy/jsonrpc2)](https://goreportcard.com/report/github.com/AdamSLevy/jsonrpc2)
[![Coverage Status](https://coveralls.io/repos/github/AdamSLevy/jsonrpc2/badge.svg?branch=master)](https://coveralls.io/github/AdamSLevy/jsonrpc2?branch=master)
[![Build Status](https://travis-ci.org/AdamSLevy/jsonrpc2.svg?branch=master)](https://travis-ci.org/AdamSLevy/jsonrpc2)

Package jsonrpc2 is a minimalist implementation of the JSON-RPC 2.0 protocol
that provides types for Requests and Responses, and an http.Handler that calls
MethodFuncs registered with RegisterMethod(). The HTTPRequestHandler will
recover from any MethodFunc panics and will always respond with a valid JSON
RPC Response, unless of course the request was a notification.
Package `jsonrpc2/v5` is a minimalist implementation of the JSON-RPC 2.0
protocol that provides types for `Request`s and `Response`s, and an
`http.Handler` that calls `MethodFunc`s registered with `RegisterMethod()`. The
`HTTPRequestHandler` will recover from any `MethodFunc` panics and will always
respond with a valid JSON RPC Response, unless of course the request was a
notification.

It strives to conform to the official specification:
[https://www.jsonrpc.org](https://www.jsonrpc.org)
Expand All @@ -20,34 +21,35 @@ information.

### Client

Clients can use the Request, Response, and Error types with the json and http
packages to make HTTP JSON-RPC 2.0 calls and parse their responses.
Clients can use the `Request`, `Response`, and `Error` types with the `json`
and `http` packages to make HTTP JSON-RPC 2.0 calls and parse their responses.
```go
reqBytes, _ := json.Marshal(jsonrpc2.NewRequest("subtract", 0, []int{5, 1}))
httpResp, _ := http.Post("www.example.com", "application/json",
bytes.NewReader(reqBytes))
respBytes, _ := ioutil.ReadAll(httpResp.Body)
response := &jsonrpc2.Response{}
json.Unmarshal(respBytes, response)
response := jsonrpc2.Response{}
json.Unmarshal(respBytes, &response)
```

### Server

Servers must implement their RPC method functions to match the MethodFunc type.
Methods must be registered with a name using RegisterMethod().
Servers must implement their RPC method functions to match the `MethodFunc`
type. Methods must be registered with a name using `RegisterMethod()`.
```go
var func versionMethod(p json.RawMessage) *jsonrpc2.Response {
var func versionMethod(p json.RawMessage) jsonrpc2.Response {
if p != nil {
return jsonrpc2.NewInvalidParamsErrorResponse(nil)
}
return jrpc.NewResponse("0.0.0")
}
jsonrpc2.RegisterMethod("version", jsonrpc2.MethodFunc(versionMethod))
```
Read the documentation for RegisterMethod and MethodFunc for more information.
Read the documentation for `RegisterMethod()` and `MethodFunc` for more
information.

After all methods are registered, set up an HTTP Server with
`HTTPRequestHandler` as the handler.
```go
HTTPRequestHandler as the handler.
http.ListenAndServe(":8080", jsonrpc2.HTTPRequestHandler)
```
1 change: 0 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// github.com/AdamSLevy/jsonrpc2
// Copyright 2018 Adam S Levy. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
Expand Down

0 comments on commit c4af76b

Please sign in to comment.