restgo

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: Apache-2.0 Imports: 22 Imported by: 3

README

restgo

Easy to use golang http client.

Go Reference golangci-lint Go Report Card License

Get Started

Get it
go get -u github.com/pinealctx/restgo
Examples

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultClient = New()
)

Functions

func CloneURL

func CloneURL(o *url.URL) *url.URL

func DetectContentTypeAndSize

func DetectContentTypeAndSize(filePath string) (string, int64, error)

DetectContentTypeAndSize 发现文件的ContentType和大小

func Download

func Download(ctx context.Context, url string) ([]byte, error)

func ZapJSONMarshal

func ZapJSONMarshal(obj zapcore.ObjectMarshaler) ([]byte, error)

ZapJSONMarshal Zap JSON 序列化

Types

type AfterHookFunc

type AfterHookFunc func(req IRequest, rsp IResponse)

AfterHookFunc 请求后钩子函数

type BaseParam

type BaseParam struct {
	Name  string
	Value string
}

BaseParam 参数基类

func (*BaseParam) ParamName

func (p *BaseParam) ParamName() string

type BeforeHookFunc

type BeforeHookFunc func(req IRequest)

BeforeHookFunc 请求前钩子函数

type BodyParam

type BodyParam struct {
	ContentType string
	Value       io.Reader
}

BodyParam 将参数作为HTTP Body携带,具体序列化方式通过参数内容类型而定 同一个request有且仅有一个BodyParam

func NewBodyParam

func NewBodyParam(contentType string, value io.Reader) *BodyParam

func NewJSONBody

func NewJSONBody(obj interface{}) (*BodyParam, error)

func NewXMLBody

func NewXMLBody(obj interface{}) (*BodyParam, error)

func (BodyParam) ParamName

func (p BodyParam) ParamName() string

type Client

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

func New

func New(optFns ...OptionFn) *Client

func (*Client) Do

func (c *Client) Do(ctx context.Context, req IRequest) (IResponse, error)

func (*Client) Execute

func (c *Client) Execute(ctx context.Context, method, resource string, params ...IParam) (IResponse, error)

func (*Client) Get

func (c *Client) Get(ctx context.Context, resource string, params ...IParam) (IResponse, error)

func (*Client) Post

func (c *Client) Post(ctx context.Context, resource string, params ...IParam) (IResponse, error)

func (*Client) Put

func (c *Client) Put(ctx context.Context, resource string, params ...IParam) (IResponse, error)

type CookieParam

type CookieParam struct {
	http.Cookie
}

CookieParam 将参数通过cookie携带

func NewCookieParam

func NewCookieParam(cookie *http.Cookie) *CookieParam

func (CookieParam) ParamName

func (p CookieParam) ParamName() string

type FileParam

type FileParam struct {
	Name           string
	FileName       string
	ContentType    string
	ContentLength  int64
	FileWriterFunc WriterFunc
}

FileParam 将文件作为参数携带(multipart/form-data)

func NewBytesFileParam

func NewBytesFileParam(fieldName, fileName string, bytes []byte) *FileParam

func NewPathFileParam

func NewPathFileParam(fieldName, filePath string) (*FileParam, error)

func (FileParam) ParamName

func (p FileParam) ParamName() string

type FormDataParam

type FormDataParam struct {
	BaseParam
}

FormDataParam 将参数通过Form表单(multipart/form-data或application/x-www-form-urlencoded)携带

func NewFormDataParam

func NewFormDataParam(name, value string) *FormDataParam

type HeaderParam

type HeaderParam struct {
	BaseParam
}

HeaderParam 将参数通过HTTP Header携带

func NewHeaderParam

func NewHeaderParam(name, value string) *HeaderParam

type IParam

type IParam interface {
	ParamName() string
}

func ObjectParams

func ObjectParams(obj interface{}) []IParam

type IRequest

type IRequest interface {
	AddParam(param IParam) IRequest
	AddParams(params ...IParam) IRequest
	AddCookie(name, value string) IRequest
	AddHeader(name, value string) IRequest
	AddURLQuery(name, value string) IRequest
	AddURLSegment(name, value, format string) IRequest
	AddFormItem(name, value string) IRequest
	AddFileBytes(fieldName, fileName string, bytes []byte) IRequest
	AddFilePath(fieldName, filePath string) IRequest
	SetBody(contentType string, value io.Reader) IRequest
	SetJSONBody(obj interface{}) IRequest
	SetXMLBody(obj interface{}) IRequest
	WithContentType(contentType string) IRequest

	MakeURL(baseURL *url.URL) (string, error)
	GetMethod() string
	MakeRequestBody() (io.Reader, error)
	WrapperHTTPRequest(req *http.Request)
}

type IResponse

type IResponse interface {
	// GetResponse get http response
	GetResponse() *http.Response
	// StatusCode get http status code
	StatusCode() int
	// Data get response data
	// it will automatically close response body
	Data() ([]byte, error)
	// Pipe : pipe response data to writer
	// it will automatically close response body
	Pipe(writer io.Writer) error
	// JSONUnmarshal unmarshal response data to json
	// it will automatically close response body
	JSONUnmarshal(i interface{}) error
	// XMLUnmarshal unmarshal response data to xml
	// it will automatically close response body
	XMLUnmarshal(i interface{}) error
	// SaveFile save response data to file
	// it will automatically close response body
	SaveFile(fileName string) error
	// ExplicitCloseBody close response body
	// If you get a response but never access it's body,
	// you should call this method to close response body.
	// Otherwise, it will cause resource leak.
	// Actually, you can use GetResponse().Body.Close() to close response body,
	// but this method is more convenient and remind you to close response body.
	ExplicitCloseBody() error
}

func NewResponse

func NewResponse(rsp *http.Response) IResponse

type OptionFn

type OptionFn func(opt *option)

func WithAfterHook

func WithAfterHook(hook AfterHookFunc) OptionFn

WithAfterHook 挂载请求后的钩子函数

func WithBaseURL

func WithBaseURL(baseURL string) OptionFn

func WithBeforeHook

func WithBeforeHook(hook BeforeHookFunc) OptionFn

WithBeforeHook 挂载请求前的钩子函数

func WithCert

func WithCert(certPool *x509.CertPool, cert tls.Certificate) OptionFn

func WithCheckRedirect

func WithCheckRedirect(checkRedirect func(req *http.Request, via []*http.Request) error) OptionFn

func WithCookies

func WithCookies(u *url.URL, cookies ...*http.Cookie) OptionFn

func WithGlobalHeader

func WithGlobalHeader(header http.Header) OptionFn

func WithJar

func WithJar(jar *cookiejar.Jar) OptionFn

func WithTimeout

func WithTimeout(timeout time.Duration) OptionFn

func WithTransport

func WithTransport(transport http.RoundTripper) OptionFn

type Request

type Request struct {
	Resource string
	Method   string

	Cookies     []*CookieParam
	Headers     []*HeaderParam
	URLQueries  []*URLQueryParam
	URLSegments []*URLSegmentParam
	FormItems   []*FormDataParam
	Files       []*FileParam
	Body        *BodyParam
	ContentType string

	Err error
}

func NewRequest

func NewRequest(method, resource string) *Request

func (*Request) AddCookie

func (r *Request) AddCookie(name, value string) IRequest

func (*Request) AddFileBytes

func (r *Request) AddFileBytes(fieldName, fileName string, bytes []byte) IRequest

func (*Request) AddFilePath

func (r *Request) AddFilePath(fieldName, filePath string) IRequest

func (*Request) AddFormItem

func (r *Request) AddFormItem(name, value string) IRequest

func (*Request) AddHeader

func (r *Request) AddHeader(name, value string) IRequest

func (*Request) AddParam

func (r *Request) AddParam(param IParam) IRequest

func (*Request) AddParams

func (r *Request) AddParams(params ...IParam) IRequest

func (*Request) AddURLQuery

func (r *Request) AddURLQuery(name, value string) IRequest

func (*Request) AddURLSegment

func (r *Request) AddURLSegment(name, value, format string) IRequest

func (*Request) GetMethod

func (r *Request) GetMethod() string

func (*Request) MakeRequestBody

func (r *Request) MakeRequestBody() (io.Reader, error)

func (*Request) MakeURL

func (r *Request) MakeURL(baseURL *url.URL) (string, error)

func (*Request) SetBody

func (r *Request) SetBody(contentType string, value io.Reader) IRequest

func (*Request) SetJSONBody

func (r *Request) SetJSONBody(obj interface{}) IRequest

func (*Request) SetXMLBody

func (r *Request) SetXMLBody(obj interface{}) IRequest

func (*Request) WithContentType

func (r *Request) WithContentType(contentType string) IRequest

func (*Request) WrapperHTTPRequest

func (r *Request) WrapperHTTPRequest(req *http.Request)

type Response

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

func (*Response) Data

func (r *Response) Data() ([]byte, error)

func (*Response) ExplicitCloseBody added in v0.1.5

func (r *Response) ExplicitCloseBody() error

func (*Response) GetResponse

func (r *Response) GetResponse() *http.Response

func (*Response) JSONUnmarshal

func (r *Response) JSONUnmarshal(i interface{}) error

func (*Response) Pipe

func (r *Response) Pipe(writer io.Writer) error

func (*Response) SaveFile

func (r *Response) SaveFile(fileName string) error

func (*Response) StatusCode

func (r *Response) StatusCode() int

func (*Response) XMLUnmarshal

func (r *Response) XMLUnmarshal(i interface{}) error

type URLQueryParam

type URLQueryParam struct {
	BaseParam
}

URLQueryParam 将参数通过URL Query携带

func NewURLQueryParam

func NewURLQueryParam(name, value string) *URLQueryParam

type URLSegmentParam

type URLSegmentParam struct {
	BaseParam
	Format string
}

URLSegmentParam 将参数通过URL segment携带 eg: path = "tag/:Resource",name = "Resource"

func NewURLSegmentParam

func NewURLSegmentParam(name, value, format string) *URLSegmentParam

type WriterFunc

type WriterFunc func(w io.Writer) error

func BytesWriter

func BytesWriter(buff []byte) WriterFunc

func FileWriter

func FileWriter(filePath string) WriterFunc

Jump to

Keyboard shortcuts

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