jsonrpc

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2020 License: MIT Imports: 9 Imported by: 0

README

= go-jsonrpc

go-jsonrpc is a JSON-RPC 2.0 client that communicates over HTTP in Go.

== Install

[source, console]
----
$ go get github.com/kechako/go-jsonrpc
----

=== Usage

[source, golang]
----
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"

	"github.com/kechako/go-jsonrpc"
)

type Query struct {
	Email string
}

type Result struct {
	ID      uint64
	Name    string
	Address string
}

func main() {
	c := &jsonrpc.Client{}

	q := &Query{
		Email: "test@example.com",
	}

	header := make(http.Header)
	header.Add("X-Custom-Header", "XXXX")

	var res Result
	err := c.Call(context.Background(), "https://example.com/jsonrpc", "query", q, &res, jsonrpc.WithHeader(header))
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(res.ID, res.Name, res.Address)
}
----

Documentation

Overview

Package jsonrpc is a JSON-RPC 2.0 client that communicates over HTTP.

Index

Constants

View Source
const Version = "2.0"

Version is a JSON-RPC version.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// HTTPClient is a HTTP client you want to use.
	// Use http.DefaultClient if it is nil.
	HTTPClient *http.Client
}

Client represents a JSPN-RPC 2.0 Client.

func (*Client) Call

func (client *Client) Call(ctx context.Context, url string, method string, params interface{}, result interface{}, opts ...Option) error

Call calls the method on the url with the params, and stores result responded by the server in the result.

type ErrorCode

type ErrorCode int

ErrorCode is a number that indicates the error type that occurred.

const (
	// Invalid JSON was received by the server.
	// An error occurred on the server while parsing the JSON text.
	ParseError ErrorCode = -32700
	// The JSON sent is not a valid Request object.
	InvalidRequest ErrorCode = -32600
	// The method does not exist or is not available.
	MethodNotFound ErrorCode = -32601
	// Invalid method parameter(s).
	InvalidParams ErrorCode = -32602
	// nternal JSON-RPC error.
	InternalError ErrorCode = -32603
)

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option represents an option used to method calling.

func WithHeader

func WithHeader(header http.Header) Option

type ResponseError

type ResponseError struct {
	Code    ErrorCode   `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

ResponseError represents an error responded by the server.

func (*ResponseError) Error

func (err *ResponseError) Error() string

Jump to

Keyboard shortcuts

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