restc

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: Unlicense Imports: 17 Imported by: 0

README

restc

rest client for calling apis

example

package main
import (
	"context"
	
	"github.com/jaronnie/restc"
)

func main() {
	restClient, err := restc.New(restc.WithUrl("http://127.0.0.1:8080"))
	if err != nil {
		panic(err)
	}

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Interface

type Interface interface {
	Verb(verb string) *Request
	Post() *Request
	Get() *Request
}

type Opt

type Opt func(client *RESTClient) error

func WithAddr

func WithAddr(addr string) Opt

func WithClient

func WithClient(client *http.Client) Opt

func WithHeaders

func WithHeaders(headers http.Header) Opt

func WithPort

func WithPort(port string) Opt

func WithProtocol

func WithProtocol(protocol string) Opt

func WithRetryDelay

func WithRetryDelay(time time.Duration) Opt

func WithRetryTimes

func WithRetryTimes(times int) Opt

func WithUrl

func WithUrl(u string) Opt

type PathParam

type PathParam struct {
	Name  string
	Value interface{}
}

type QueryParam

type QueryParam struct {
	Name  string
	Value interface{}
}

type RESTClient

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

func New

func New(ops ...Opt) (*RESTClient, error)

func (*RESTClient) Get

func (r *RESTClient) Get() *Request

func (*RESTClient) Post

func (r *RESTClient) Post() *Request

func (*RESTClient) Verb

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

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 *RESTClient) *Request

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) DoUpload

func (r *Request) DoUpload(ctx context.Context, fieldName string, filename string, filedata []byte) Result

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

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

func (*Request) Params

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

func (*Request) Stream

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

Stream proto Stream way return io.ReadCloser

func (*Request) SubPath

func (r *Request) SubPath(subPath string, args ...PathParam) *Request

SubPath set subPath

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 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{}, isWarpHttpResponse bool) 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.)

func (Result) TransformResponse

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

Jump to

Keyboard shortcuts

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