rest

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTransport http.RoundTripper
View Source
var JsonMarshalToString = func(v any) (string, error) {
	bs, err := json.Marshal(v)
	return string(bs), err
}

JsonMarshalToString marshals a value as a JSON string. To replace this with a Jsoniter implementation, set

rest.JsonMarshalToString = jsoniter.JSON.MarshalToString
View Source
var JsonUnmarshal = func(r io.Reader, output any) error {
	decoder := json.NewDecoder(r)
	decoder.UseNumber()
	return decoder.Decode(output)
}

JsonUnmarshal unmarshals a value. The decoder UseNumber() option is set; replace this function if necessary. To replace this with a Jsoniter implementation, set

rest.JsonUnmarshal = func(r io.Reader, output any) error {
	decoder := jsoniter.JSON.NewDecoder(r)
	decoder.UseNumber()
	return decoder.Decode(output)
}
View Source
var RESTErrorStringLimit = 250

Functions

func DefaultClient

func DefaultClient(timeout time.Duration) httpclient.HttpClient

DefaultClient gets a http.Client with the DefaultTransport as its round-tripper and a specified timeout. A timeout of zero means no timeout.

func Headers

func Headers(headerKeyVals ...string) http.Header

Headers builds a http.Header using the pairs of key-value strings.

func RESTClient

func RESTClient(timeout time.Duration) httpclient.HttpClient

RESTClient returns a client that sets request headers on every request.

  • Host: (from URL)
  • Accept: application/json

func SetDefaultTLSConfig

func SetDefaultTLSConfig(cfg *tls.Config)

SetDefaultTLSConfig sets the TLS configuration used by the default transport.

Types

type Body

type Body interface {
	Bytes() []byte
	fmt.Stringer
	io.Reader
}

type HttpClient

type HttpClient interface {
	Do(req *http.Request) (*http.Response, error)
}

type RESTError

type RESTError struct {
	Code         int
	Request      *http.Request
	ResponseType header.ContentType
	Response     Body
	// contains filtered or unexported fields
}

func (*RESTError) Error

func (re *RESTError) Error() string

Error makes it compatible with `error` interface.

func (*RESTError) UnmarshalJSONResponse

func (re *RESTError) UnmarshalJSONResponse(value any) error

func (*RESTError) Unwrap

func (re *RESTError) Unwrap() error

type RESTRequest

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

func Entity

func Entity(ctx context.Context, url string, input any, contentType string) *RESTRequest

Entity constructs a request using a URL. The request will have an arbitrary entity.

func JSON

func JSON(ctx context.Context, url string, input any) *RESTRequest

JSON constructs a request using a URL. The request will have a JSON entity.

func Request

func Request(ctx context.Context, url string) *RESTRequest

Request constructs a request using a URL. The request will not have an entity.

func (*RESTRequest) Delete

func (rr *RESTRequest) Delete(httpClient HttpClient) *RESTResponse

Delete performs an HTTP DELETE request. The response headers and status code are returned. In order to close resources correctly, it is essential to call one of RESTResponse.Status, RESTResponse.Unmarshal or RESTResponse.ToMap

func (*RESTRequest) Get

func (rr *RESTRequest) Get(httpClient HttpClient) *RESTResponse

Get performs an HTTP GET request. The response headers and status code are returned. In order to close resources correctly, it is essential to call one of RESTResponse.Status, RESTResponse.Unmarshal or RESTResponse.ToMap

func (*RESTRequest) HTTPRoundTrip

func (rr *RESTRequest) HTTPRoundTrip(method string, httpClient HttpClient) *RESTResponse

HTTPRoundTrip performs an arbitrary HTTP request. The response headers and status code are returned. In order to close resources correctly, it is essential to call one of RESTResponse.Status, RESTResponse.Unmarshal or RESTResponse.ToMap

func (*RESTRequest) Head

func (rr *RESTRequest) Head(httpClient HttpClient) *RESTResponse

Head performs an HTTP HEAD request. The response headers and status code are returned. In order to close resources correctly, it is essential to call one of RESTResponse.Status, RESTResponse.Unmarshal or RESTResponse.ToMap

func (*RESTRequest) Patch

func (rr *RESTRequest) Patch(httpClient HttpClient) *RESTResponse

Patch performs an HTTP PATCH request. The response headers and status code are returned. In order to close resources correctly, it is essential to call one of RESTResponse.Status, RESTResponse.Unmarshal or RESTResponse.ToMap

func (*RESTRequest) Post

func (rr *RESTRequest) Post(httpClient HttpClient) *RESTResponse

Post performs an HTTP POST request. The response headers and status code are returned. In order to close resources correctly, it is essential to call one of RESTResponse.Status, RESTResponse.Unmarshal or RESTResponse.ToMap

func (*RESTRequest) Put

func (rr *RESTRequest) Put(httpClient HttpClient) *RESTResponse

Put performs an HTTP PUT request. The response headers and status code are returned. In order to close resources correctly, it is essential to call one of RESTResponse.Status, RESTResponse.Unmarshal or RESTResponse.ToMap

func (*RESTRequest) Query

func (rr *RESTRequest) Query(query urlpkg.Values) *RESTRequest

Query adds query parameters to the request, as if a "?key=value" list had been attached to the URL.

func (*RESTRequest) With

func (rr *RESTRequest) With(m http.Header) *RESTRequest

With adds headers to the request.

func (*RESTRequest) WithKV

func (rr *RESTRequest) WithKV(headerKeyVals ...string) *RESTRequest

WithKV adds key-value pairs as headers to the request.

type RESTResponse

type RESTResponse struct {
	Req  *http.Request
	Res  *http.Response
	Body io.ReadCloser
	Err  error
}

func (*RESTResponse) Status

func (rr *RESTResponse) Status() (respHeader http.Header, statusCode int, err error)

Status gets the status of the response.

func (*RESTResponse) ToMap

func (rr *RESTResponse) ToMap() (data map[string]any, statusCode int, err error)

ToMap extracts a map containing a tree of data from the JSON response. If the response does not contain JSON, the map will be nil.

func (*RESTResponse) Unmarshal

func (rr *RESTResponse) Unmarshal(output any) (respHeader http.Header, statusCode int, err error)

Unmarshal extracts data from the response. Typically, when the response contains JSON, output will be a pointer to a struct that matches the expected content. However, it may also be *string or *[]byte. See RESTResponse.ToMap.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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