rest

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2023 License: MIT Imports: 16 Imported by: 0

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

Types

type AuthType

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

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

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

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

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(url 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("goat", "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) SetTimeout

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

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

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

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

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

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

func (r *Response) Error() error

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

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