restc

package module
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2025 License: Unlicense Imports: 17 Imported by: 0

README

restc

rest client for calling apis

example

package main
import (
	"context"
	
	"github.com/jzero-io/restc"
)

func main() {
	restClient, err := NewClient("http://127.0.0.1:8080", WithRequestMiddleware(func(c Client, request *Request) error {
		request.AddHeader("test", "test")
		return nil
	}))

	result := restClient.Verb("GET").Path("/api/v1/version").Do(context.Background())
	if result.Error() != nil {
		panic(err)
	}
	println(result.Status())
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultCodeField    = "code"
	DefaultDataField    = "data"
	DefaultMessageField = "msg"
)

Functions

This section is empty.

Types

type Client added in v0.6.0

type Client interface {
	Verb(verb string) *Request
	SetHeader(headers http.Header)
}

func NewClient added in v0.6.0

func NewClient(target string, ops ...Opt) (Client, error)

type IntoOptions added in v0.6.0

type IntoOptions struct {
	WrapCodeMsg        bool
	WrapCodeMsgMapping struct {
		CodeField string
		DataField string
		MsgField  string
	}
}

type Opt

type Opt func(client *client) error

func WithClient added in v0.5.0

func WithClient(c *http.Client) Opt

func WithHeaders

func WithHeaders(headers http.Header) Opt

func WithRequestMiddleware added in v0.7.0

func WithRequestMiddleware(middleware RequestMiddleware) Opt

func WithRetryDelay

func WithRetryDelay(time time.Duration) Opt

func WithRetryTimes

func WithRetryTimes(times int) Opt

type PathParam

type PathParam struct {
	Name  string
	Value interface{}
}

type QueryParam

type QueryParam struct {
	Name  string
	Value interface{}
}

type Request

type Request struct {
	// contains filtered or unexported fields
}

Request allows for building up a request to a server in a chained fashion. Any errors are stored until the end of your call, so you only have to check once.

func NewRequest

func NewRequest(c *client) *Request

func (*Request) AddHeader added in v0.7.0

func (r *Request) AddHeader(key, value string)

func (*Request) Body

func (r *Request) Body(obj interface{}) *Request

Body makes the request use obj as the body. Optional. If obj is a string, try to read a file of that name. If obj is a []byte, send it directly. default marshal it

func (*Request) Do

func (r *Request) Do(ctx context.Context) Result

Do format and executes the request. Returns a Result object for easy response processing.

Error type: http.Client.Do errors are returned directly.

func (*Request) GetBody added in v0.7.0

func (r *Request) GetBody() io.Reader

func (*Request) GetParams added in v0.7.0

func (r *Request) GetParams() []QueryParam

func (*Request) Params

func (r *Request) Params(args ...QueryParam) *Request

func (*Request) Path added in v0.6.0

func (r *Request) Path(path string, args ...PathParam) *Request

Path set path

func (*Request) Stream

func (r *Request) Stream(ctx context.Context) (io.ReadCloser, error)

Stream proto Stream way return io.ReadCloser

func (*Request) Verb

func (r *Request) Verb(verb string) *Request

func (*Request) WsConn

func (r *Request) WsConn(ctx context.Context) (*websocket.Conn, *http.Response, error)

type RequestMiddleware added in v0.7.0

type RequestMiddleware func(Client, *Request) error

type Result

type Result struct {
	// contains filtered or unexported fields
}

Result contains the result of calling Request.Do().

func (Result) Error

func (r Result) Error() error

Error returns the error executing the request, nil if no error occurred.

func (Result) Into

func (r Result) Into(obj interface{}, options *IntoOptions) error

Into stores the result into obj, if possible. If obj is nil it is ignored.

func (Result) RawResponse

func (r Result) RawResponse() ([]byte, error)

func (Result) Status

func (r Result) Status() string

Status returns the status executing the request

func (Result) StatusCode

func (r Result) StatusCode() int

StatusCode returns the HTTP status code of the request. (Only valid if no error was returned.)

Jump to

Keyboard shortcuts

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