README
GoLang jrpc2client (early beta)
This is a json-rpc 2.0 client package for golang based on:
- HTTP Client: erikdubbelboer/fasthttp
- JSON Parser: pquerna/ffjson
- Logger: Sirupsen/logrus
- Errors: riftbit/jrpc2errors
to get high perfomance
This package is still in development
Examples
Without custom logger settings
package main
import (
"github.com/riftbit/jrpc2client"
)
type TestReply struct {
LogID string `json:"log_id"`
}
func main() {
client := jrpc2client.NewClient()
client.SetBaseURL("http://127.0.0.1:65001")
client.SetUserAgent("JsonRPC Test Client")
client.SetBasicAuth("user", "password")
dstT := &TestReply{}
err := client.Call("/api", "demo.Test", TestArgs{ID: "TESTER_ID_1"}, dstT)
if err != nil {
panic(err)
}
println(dstT.LogID)
}
With custom logger settings
package main
import (
"github.com/riftbit/jrpc2client"
)
type TestReply struct {
LogID string `json:"log_id"`
}
func main() {
logger := &logrus.Logger{
Out: os.Stdout,
Formatter: &logrus.JSONFormatter{DisableTimestamp: false},
Level: logrus.DebugLevel,
}
client := jrpc2client.NewClientWithLogger(logger)
client.SetBaseURL("http://127.0.0.1:65001")
client.SetUserAgent("JsonRPC Test Client")
client.SetBasicAuth("user", "password")
dstT := &TestReply{}
err := client.Call("/api", "demo.Test", TestArgs{ID: "TESTER_ID_3"}, dstT)
if err != nil {
panic(err)
}
println(dstT.LogID)
}
Benchmark results
Documentation
Overview ¶
Package jrpc2client implements client for json-rpc 2.0 protocol and based on another packages:
HTTP Client: github.com/valyala/fasthttp
JSON Parser: github.com/pquerna/ffjson/ffjson
Logger: github.com/sirupsen/logrus
Errors: github.com/riftbit/jrpc2errors
Example can be found only in client_test.go at this moment
You can see your godoc rendered as HTML by running a local godoc server. This is great for previewing your godoc before committing changes. To do that, Make sure your code is in GOPATH and run:
godoc -http ":8080"
Go to http://localhost:8080/pkg and you should see your packages on the list.
If you want the raw HTML, you can run:
godoc -url=/pkg/your_package > your_page.html
Index ¶
- type Client
- func (cl *Client) Call(urlPath string, method string, args interface{}, dst interface{}) error
- func (cl *Client) CallForMap(urlPath string, method string, args interface{}) (map[string]interface{}, error)
- func (cl *Client) DeleteAuthHeader()
- func (cl *Client) DeleteCustomHeader(headerName string)
- func (cl *Client) DisableHeaderNamesNormalizing(fix bool)
- func (cl *Client) SetBaseURL(baseURL string)
- func (cl *Client) SetBasicAuthHeader(login string, password string)
- func (cl *Client) SetClientTimeout(duration time.Duration)
- func (cl *Client) SetCustomAuthHeader(authType string, authData string)
- func (cl *Client) SetCustomHeader(headerName string, headerValue string)
- func (cl *Client) SetUserAgent(userAgent string)
- type ErrorCode
Examples ¶
Constants ¶
Variables ¶
Functions ¶
Types ¶
type Client ¶
type Client struct { BaseURL string // contains filtered or unexported fields }
Client basic struct that contains all method to work with JSON-RPC 2.0 protocol
func NewClient ¶
func NewClient() *Client
NewClient returns new configured Client to start work with JSON-RPC 2.0 protocol
func NewClientWithLogger ¶
NewClientWithLogger returns new configured Client with custom Logger configureation (based on Sirupsen/logrus) to start work with JSON-RPC 2.0 protocol
func (*Client) Call ¶
Call run remote procedure on JSON-RPC 2.0 API with parsing answer to provided structure or interface
Example ¶
This function is named ExampleClient_Call(), this way godoc knows to associate it with the Client type and method Call.
Output:
func (*Client) CallForMap ¶
func (cl *Client) CallForMap(urlPath string, method string, args interface{}) (map[string]interface{}, error)
CallForMap run remote procedure on JSON-RPC 2.0 API with returning map[string]interface{}
Example ¶
This function is named ExampleClient_CallForMap(), this way godoc knows to associate it with the Client type and method CallForMap.
Output:
func (*Client) DeleteAuthHeader ¶
func (cl *Client) DeleteAuthHeader()
DeleteAuthHeader clear basic auth header
func (*Client) DeleteCustomHeader ¶
DeleteCustomHeader delete custom header
func (*Client) DisableHeaderNamesNormalizing ¶
SetFixHeaders setting normalize headers or not
func (*Client) SetBaseURL ¶
SetBaseURL setting basic url for API
func (*Client) SetBasicAuthHeader ¶
SetBasicAuthHeader setting basic auth header
func (*Client) SetClientTimeout ¶
SetClientTimeout this method sets globally for client its timeout
func (*Client) SetCustomAuthHeader ¶
SetCustomAuthHeader setting custom auth header with type of auth and auth data
func (*Client) SetCustomHeader ¶
SetCustomHeader setting custom header
func (*Client) SetUserAgent ¶
SetUserAgent setting custom User Agent header