jrpc2client

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2018 License: MIT Imports: 12 Imported by: 0

README

GoLang jrpc2client (early beta)

Website | Blog

license GoDoc Coverage Status Build Status Go Report Card

This is a json-rpc 2.0 client package for golang based on:

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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

Example
package main

import (
	"github.com/riftbit/jrpc2client"
)

func main() {
	client := jrpc2client.NewClient()
	//print empty line because BaseURL not setted
	println(client.BaseURL)
}
Output:

func NewClientWithLogger

func NewClientWithLogger(logger *logrus.Logger) *Client

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

func (cl *Client) Call(urlPath string, method string, args interface{}, dst interface{}) error

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.

package main

import (
	"fmt"
	"time"

	"github.com/riftbit/jrpc2client"
)

func main() {

	type TestReply struct {
		LogID     string `json:"log_id"`
		UserAgent string `json:"user_agent"`
	}

	type TestArgs struct {
		ID string
	}

	client := jrpc2client.NewClient()
	client.SetBaseURL("http://127.0.0.1:8080")
	dstP := &TestReply{}
	client.SetClientTimeout(10 * time.Millisecond)

	// final url will be http://127.0.0.1:8080/api
	err := client.Call("/api", "demo.TestClientTimeout", TestArgs{ID: "123"}, dstP)
	if err != nil {
		panic(err)
	}

	fmt.Println(dstP.LogID)
}
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.

package main

import (
	"fmt"
	"time"

	"github.com/riftbit/jrpc2client"
)

func main() {
	type TestArgs struct {
		ID string
	}

	client := jrpc2client.NewClient()
	client.SetBaseURL("http://127.0.0.1:8080")

	client.SetClientTimeout(10 * time.Millisecond)
	// final url will be http://127.0.0.1:8080/api
	dstM, err := client.CallForMap("/api", "demo.TestClientTimeout", TestArgs{ID: "321"})
	if err != nil {
		panic(err)
	}

	val, ok := dstM["log_id"]
	if ok != true {
		panic("key log_id not exists")
	}

	fmt.Println(val)
}
Output:

func (*Client) DeleteAuthHeader

func (cl *Client) DeleteAuthHeader()

DeleteAuthHeader clear basic auth header

func (*Client) DeleteCustomHeader

func (cl *Client) DeleteCustomHeader(headerName string)

DeleteCustomHeader delete custom header

func (*Client) DisableHeaderNamesNormalizing added in v1.1.0

func (cl *Client) DisableHeaderNamesNormalizing(fix bool)

SetFixHeaders setting normalize headers or not

func (*Client) SetBaseURL

func (cl *Client) SetBaseURL(baseURL string)

SetBaseURL setting basic url for API

func (*Client) SetBasicAuthHeader

func (cl *Client) SetBasicAuthHeader(login string, password string)

SetBasicAuthHeader setting basic auth header

func (*Client) SetClientTimeout

func (cl *Client) SetClientTimeout(duration time.Duration)

SetClientTimeout this method sets globally for client its timeout

func (*Client) SetCustomAuthHeader

func (cl *Client) SetCustomAuthHeader(authType string, authData string)

SetCustomAuthHeader setting custom auth header with type of auth and auth data

func (*Client) SetCustomHeader

func (cl *Client) SetCustomHeader(headerName string, headerValue string)

SetCustomHeader setting custom header

func (*Client) SetUserAgent

func (cl *Client) SetUserAgent(userAgent string)

SetUserAgent setting custom User Agent header

type ErrorCode

type ErrorCode int

ErrorCode type for error codes

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL