dc

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2019 License: MIT Imports: 9 Imported by: 1

Documentation

Overview

Package dc client with decoder

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyBody = errors.New("empty body")
)

ErrEmptyBody base errors

Functions

This section is empty.

Types

type Client

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

Client get response and marshaling it by decoder

func Must

func Must(baseURL string, opts ...Option) *Client

Must create client or panic

Example
req, _ := http.NewRequest(http.MethodGet, "/api?action=search&q=httpclient", nil)
var res struct {
	Query string
	Hits  []struct {
		Name    string
		Package string
		Author  string
	}
}
err := Must("https://go-search.org").Do(req, &res)
if err != nil {
	log.Fatal(err)
}
log.Print(res)
Output:

func New

func New(baseURL string, opts ...Option) (*Client, error)

New create new Client with default http client

Example
c, err := New("https://go-search.org")
if err != nil {
	log.Fatal(err)
}
req, _ := http.NewRequest(http.MethodGet, "/api?action=search&q=httpclient", nil)
var res struct {
	Query string
	Hits  []struct {
		Name    string
		Package string
		Author  string
	}
}
err = c.Do(req, &res)
if err != nil {
	log.Fatal(err)
}
log.Print(res)
Output:

func (*Client) Do

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

Do request and decode response body

func (*Client) Fetch

func (c *Client) Fetch(r *http.Request) httpclient.Fetch

Fetch do request

type Option

type Option func(*Client)

Option for the configure Client

func WithDecoder

func WithDecoder(decoder decoder.Decoder) Option

WithDecoder set decoder body

func WithErrorMiddleware

func WithErrorMiddleware(minStatusCode int,
	errFactory func() error,
	decoder func(*http.Response, io.Reader, interface{}) error) Option

WithErrorMiddleware add middleware for transport nolint: bodyclose deprecated: use WithHttpErrorMiddleware

func WithFetchMiddleware

func WithFetchMiddleware(mw ...func(*http.Response, io.Reader) error) Option

WithFetchMiddleware add middleware for transport nolint: bodyclose

func WithHTTPClient

func WithHTTPClient(cl *http.Client) Option

WithHTTPClient set http client

func WithHTTPErrorMiddleware added in v0.0.2

func WithHTTPErrorMiddleware(minStatusCode int,
	errFactory func(r *http.Response) error,
	decoder func(*http.Response, io.Reader, interface{}) error) Option

WithHttpErrorMiddleware add middleware for transport nolint: bodyclose

func WithMiddleware

func WithMiddleware(mw ...transport.Middleware) Option

WithMiddleware add middleware do request

func WithTransport

func WithTransport(tr http.RoundTripper) Option

WithTransport set transport

Jump to

Keyboard shortcuts

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