rest

package
v2.0.25 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2024 License: MIT Imports: 26 Imported by: 16

Documentation

Index

Constants

View Source
const (
	ACCEPT_HEADER           = "Accept"
	CONTENT_TYPE_HEADER     = "Content-Type"
	CONTENT_ENCODING_HEADER = "Content-Encoding"
	AUTHORIZATION_HEADER    = "Authorization"
)

Variables

This section is empty.

Functions

func HeaderFilterFlags

func HeaderFilterFlags(content string) string

func NewDefaultHeader

func NewDefaultHeader() http.Header

func NewTLSConfig

func NewTLSConfig(certFile, keyFile, caFile string) (*tls.Config, error)

Types

type AuthType

type AuthType string
const (
	BearerToken AuthType = "bearer_token"
	BasicAuth   AuthType = "basic_auth"
)

type Exception added in v2.0.18

type Exception struct {
	Code int
	Body []byte
	// contains filtered or unexported fields
}

func NewException added in v2.0.18

func NewException(code int, body []byte) *Exception

func (*Exception) Decode added in v2.0.18

func (e *Exception) Decode(v any) error

func (*Exception) Error added in v2.0.18

func (e *Exception) Error() string

func (*Exception) MustDecode added in v2.0.18

func (e *Exception) MustDecode(v any)

func (*Exception) WithDecoder added in v2.0.18

func (e *Exception) WithDecoder(decoder negotiator.Decoder) *Exception

type ExceptionHandleFunc added in v2.0.18

type ExceptionHandleFunc func(*Exception) error

type RESTClient

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

func NewRESTClient

func NewRESTClient() *RESTClient

NewRESTClient creates a new RESTClient. This client performs generic REST functions such as Get, Put, Post, and Delete on specified paths.

func (*RESTClient) Clone

func (c *RESTClient) Clone() *RESTClient

func (*RESTClient) Delete

func (c *RESTClient) Delete(path string) *Request

Delete begins a DELETE request. Short for c.Verb("DELETE").

func (*RESTClient) DisableTrace

func (c *RESTClient) DisableTrace() *RESTClient

关闭Trace

func (*RESTClient) EnableTrace

func (c *RESTClient) EnableTrace() *RESTClient

开启后一定要配置全局Tracer

func (*RESTClient) Get

func (c *RESTClient) Get(path string) *Request

Get begins a GET request. Short for c.Verb("GET").

func (*RESTClient) Group

func (c *RESTClient) Group(urlPath string) *RESTClient

func (*RESTClient) Method

func (c *RESTClient) Method(verb string) *Request

Verb begins a request with a verb (GET, POST, PUT, DELETE).

Example usage of RESTClient's request building interface: c, err := NewRESTClient(...) if err != nil { ... } resp, err := c.Verb("GET").

Path("pods").
SelectorParam("labels", "area=staging").
Timeout(10*time.Second).
Do()

if err != nil { ... } list, ok := resp.(*api.PodList)

func (*RESTClient) Patch

func (c *RESTClient) Patch(path string) *Request

Patch begins a PATCH request. Short for c.Verb("Patch").

func (*RESTClient) Post

func (c *RESTClient) Post(path string) *Request

Post begins a POST request. Short for c.Verb("POST").

func (*RESTClient) Put

func (c *RESTClient) Put(path string) *Request

Put begins a PUT request. Short for c.Verb("PUT").

func (*RESTClient) SetBaseURL

func (c *RESTClient) SetBaseURL(url string) *RESTClient

func (*RESTClient) SetBasicAuth

func (c *RESTClient) SetBasicAuth(username, password string) *RESTClient

SetBasicAuth method sets the basic authentication header in the current HTTP request.

For Example:

Authorization: Basic <base64-encoded-value>

To set the header for username "go-resty" and password "welcome"

client.SetBasicAuth("mcube", "welcome")

This method overrides the credentials set by method `Client.SetBasicAuth`.

func (*RESTClient) SetBearerTokenAuth

func (c *RESTClient) SetBearerTokenAuth(token string) *RESTClient

SetAuthToken method sets the auth token header(Default Scheme: Bearer) in the current HTTP request. Header example:

Authorization: Bearer <auth-token-value-comes-here>

For Example: To set bearer token BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F

client.SetBearerToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F")

This method overrides the Auth token set by method `Client.SetAuthToken`.

func (*RESTClient) SetContentType

func (c *RESTClient) SetContentType(contentType string)

SetContentType set the Content-Type header of the request. application/json

func (*RESTClient) SetCookie

func (c *RESTClient) SetCookie(cs ...*http.Cookie) *RESTClient

SetCookie method sets an array of cookies in the client instance. These cookies will be added to all the request raised from this client instance.

cookies := []*http.Cookie{
	&http.Cookie{
		Name:"key-1",
		Value:"This is cookie 1 value",
	},
	&http.Cookie{
		Name:"key2-2",
		Value:"This is cookie 2 value",
	},
}

// Setting a cookies into
client.SetCookie(cookies...)

func (*RESTClient) SetHeader

func (c *RESTClient) SetHeader(key string, values ...string) *RESTClient

func (*RESTClient) SetRequestRate

func (c *RESTClient) SetRequestRate(rate float64, capacity int64) *RESTClient

SetRequestRate 设置全局请求速率, rate: 速率, capacity: 容量(控制并发量)

func (*RESTClient) SetTLSConfig

func (c *RESTClient) SetTLSConfig(conf *tls.Config) *RESTClient

func (*RESTClient) SetTimeout

func (c *RESTClient) SetTimeout(t time.Duration) *RESTClient

func (*RESTClient) WithExceptionHandleFunc added in v2.0.18

func (c *RESTClient) WithExceptionHandleFunc(fn ExceptionHandleFunc)

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

NewRequest creates a new request helper object.

func (*Request) AbsURL

func (r *Request) AbsURL(p string) *Request

设置请求的URL的绝对路径

func (*Request) Body

func (r *Request) Body(v any) *Request

func (*Request) Cookie

func (r *Request) Cookie(cs ...*http.Cookie) *Request

func (*Request) Do

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

func (*Request) Header

func (r *Request) Header(key string, values ...string) *Request

func (*Request) Method

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

Method sets the verb this request will use.

func (*Request) Param

func (r *Request) Param(paramName, value string) *Request

Param creates a query parameter with the given string value.

func (*Request) ParamBindJson added in v2.0.17

func (r *Request) ParamBindJson(obj any) *Request

Param creates a query parameter with the given json obj to url.Values.

func (*Request) Prefix

func (r *Request) Prefix(segments ...string) *Request

Prefix adds segments to the relative beginning to the request path. These items will be placed before the optional Namespace, Resource, or Name sections. Setting AbsPath will clear any previously set Prefix segments

func (*Request) SetRequestRate

func (c *Request) SetRequestRate(rate float64, capacity int64) *Request

SetRequestRate 设置请求速率, rate: 速率, capacity: 容量(控制并发量)

func (*Request) Suffix

func (r *Request) Suffix(segments ...string) *Request

Suffix appends segments to the end of the path. These items will be placed after the prefix and optional Namespace, Resource, or Name sections.

func (*Request) Timeout

func (r *Request) Timeout(d time.Duration) *Request

Timeout makes the request use the given duration as an overall timeout for the request. Additionally, if set passes the value as "timeout" parameter in URL.

func (*Request) URL

func (r *Request) URL(p string) *Request

设置请求的URL, 会补充前缀与后缀

type Response

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

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

func NewResponse

func NewResponse(c *RESTClient) *Response

func (*Response) ContentType

func (r *Response) ContentType(m negotiator.MIME) *Response

默认不设置通过Content-Type获取, 如果设定, 以设定为准

func (*Response) Error

func (r *Response) Error() *Exception

不处理返回, 直接判断请求是否正常

func (*Response) Header

func (r *Response) Header(header string, v *string) *Response

func (*Response) Into

func (r *Response) Into(v any) error

请求正常的情况下, 获取返回的数据, 会根据Content-Type做解析

func (*Response) Raw

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

请求正常的情况下, 获取返回的数据, 不做解析

func (*Response) Stream

func (r *Response) Stream() (io.ReadCloser, error)

直接返回stream, 常用于websocket

type User

type User struct {
	Username string
	Password string
}

Jump to

Keyboard shortcuts

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