rest

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2023 License: MIT Imports: 9 Imported by: 1

README

rest

REST API client.

Godoc Reference CI codecov

Just a wrapper over http.Client with better errors and JSON unmarshaling.

Installation

go get github.com/vtopc/go-rest

This will update yours go.mod file.

TODOs

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewReqError

func NewReqError(req *http.Request, wrappedErr error) error

NewReqError builds ReqError

Types

type APIError

type APIError struct {
	ExpectedStatusCodes []int

	// Resp HTTP response
	Resp *http.Response

	// Err is either error body or error from io.Copy.
	// TODO: be more specific?
	Err error
}

APIError REST API error

func (*APIError) Error

func (e *APIError) Error() string

Error implements error interface

func (*APIError) Unwrap

func (e *APIError) Unwrap() error

Unwrap provides compatibility for Go 1.13+ error chains.

type Client

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

Client the REST API client

func NewClient

func NewClient(client *http.Client) *Client

NewClient returns REST API Client. Use https://github.com/cristalhq/hedgedhttp for retries. Check https://github.com/vtopc/restclient/tree/master/interceptors for middlewares/interceptors. TODO: switch to HTTPClient interface?

func (*Client) Do

func (c *Client) Do(req *http.Request, v interface{}, expectedStatusCodes ...int) error

Do executes HTTP request.

Stores the result in the value pointed to by v. If v is not a nil and not a pointer, Do returns a json.InvalidUnmarshalError. Use func `http.NewRequestWithContext` to create `req`.

type HTTPClient

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

HTTPClient a HTTP client

type ReqError

type ReqError struct {
	// Req HTTP request
	Req *http.Request

	// Err is either an (*APIError) or error from (*http.Client).Do()
	Err error
}

ReqError request error.

func (*ReqError) Error

func (e *ReqError) Error() string

Error implements error interface

func (*ReqError) Unwrap

func (e *ReqError) Unwrap() error

Unwrap provides compatibility for Go 1.13+ error chains.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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