From 2d57d5bf49740748a59c034247e2fd561531b8d2 Mon Sep 17 00:00:00 2001 From: Adam S Levy Date: Sat, 20 Oct 2018 20:20:15 -0800 Subject: [PATCH] Updated README --- README.md | 74 ++++++++++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 3a3d416..7145bf5 100644 --- a/README.md +++ b/README.md @@ -1,57 +1,53 @@ -# jsonrpc2 - v2.0.1 +# jsonrpc2/v4 [![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 an easy-to-use, thin, minimalist implementation of the -JSON-RPC 2.0 protocol with a handler for HTTP servers. It avoids implementing -any HTTP helper functions and instead simply provides conforming Request and -Response Types, and an http.HandlerFunc that handles single and batch Requests, -protocol errors, and recovers panics from the application's RPC method calls. +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. + It strives to conform to the official specification: -[https://www.jsonrpc.org](https://www.jsonrpc.org). +[https://www.jsonrpc.org](https://www.jsonrpc.org) + ## Getting started Please read the official godoc documentation for the most up to date information. -## Quick Overview ### 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. +```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) +``` ### Server -Servers must implement their RPC method functions to match the MethodFunc type -and then register their function with a name using RegisterMethod(name, -function). Read the documentation for RegisterMethod and MethodFunc for more -information. RemarshalJSON is a convenience function for converting the -abstract params argument into a custom concrete type. -```golang -jsonrpc2.RegisterMethod("subtract", func(params interface{}) jsonrpc2.Response { - var p []interface{} - var ok bool - if p, ok = params.([]interface{}); !ok { - return jsonrpc2.NewInvalidParamsErrorResponse( - "params must be an array of two numbers") - } - if len(p) != 2 { - return jsonrpc2.NewInvalidParamsErrorResponse( - "params must be an array of two numbers") - } - var x [2]float64 - for i := range x { - if x[i], ok = p[i].(float64); !ok { - return jsonrpc2.NewInvalidParamsErrorResponse( - "params must be an array of two numbers") - } - } - result := x[0] - x[1] - return jsonrpc2.NewResponse(result) -}) + +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 { + if p != nil { + return jsonrpc2.NewInvalidParamsErrorResponse(nil) + } + return jrpc.NewResponse("0.0.0") + } + jsonrpc2.RegisterMethod("version", jsonrpc2.MethodFunc(versionMethod)) ``` -After all methods are registered, set up an HTTP Server with HTTPRequestHandler -as the handler. -```golang -http.ListenAndServe(":8080", jsonrpc2.HTTPRequestHandler) +Read the documentation for RegisterMethod and MethodFunc for more information. + +After all methods are registered, set up an HTTP Server with +```go +HTTPRequestHandler as the handler. + http.ListenAndServe(":8080", jsonrpc2.HTTPRequestHandler) ```