httpclient

package
v6.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2020 License: Apache-2.0 Imports: 9 Imported by: 27

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is the http client implementation

func NewClient

func NewClient(opts ...Option) *Client

NewClient returns a new instance of http Client

func (*Client) AddPlugin

func (c *Client) AddPlugin(p heimdall.Plugin)

AddPlugin Adds plugin to client

func (*Client) Delete

func (c *Client) Delete(url string, headers http.Header) (*http.Response, error)

Delete makes a HTTP DELETE request with provided URL

func (*Client) Do

func (c *Client) Do(request *http.Request) (*http.Response, error)

Do makes an HTTP request with the native `http.Do` interface

func (*Client) Get

func (c *Client) Get(url string, headers http.Header) (*http.Response, error)

Get makes a HTTP GET request to provided URL

func (*Client) Patch

func (c *Client) Patch(url string, body io.Reader, headers http.Header) (*http.Response, error)

Patch makes a HTTP PATCH request to provided URL and requestBody

func (*Client) Post

func (c *Client) Post(url string, body io.Reader, headers http.Header) (*http.Response, error)

Post makes a HTTP POST request to provided URL and requestBody

func (*Client) Put

func (c *Client) Put(url string, body io.Reader, headers http.Header) (*http.Response, error)

Put makes a HTTP PUT request to provided URL and requestBody

type MockPlugin

type MockPlugin struct {
	mock.Mock
}

MockPlugin provides a mock plugin for heimdall

func (*MockPlugin) OnError

func (m *MockPlugin) OnError(req *http.Request, err error)

OnError is called when the request errors out

func (*MockPlugin) OnRequestEnd

func (m *MockPlugin) OnRequestEnd(req *http.Request, res *http.Response)

OnRequestEnd is called when the request ends

func (*MockPlugin) OnRequestStart

func (m *MockPlugin) OnRequestStart(req *http.Request)

OnRequestStart is called when the request starts

type Option

type Option func(*Client)

Option represents the client options

func WithHTTPClient

func WithHTTPClient(client heimdall.Doer) Option

WithHTTPClient sets a custom http client

Example
m := &mockClient{}
c := NewClient(WithHTTPClient(m))
req, err := http.NewRequest(http.MethodGet, "https://www.gojek.io/", nil)
if err != nil {
	panic(err)
}
_, _ = c.Do(req)
Output:

mock client called

func WithHTTPTimeout

func WithHTTPTimeout(timeout time.Duration) Option

WithHTTPTimeout sets hystrix timeout

Example
c := NewClient(WithHTTPTimeout(5 * time.Second))
req, err := http.NewRequest(http.MethodGet, "https://www.gojek.io/", nil)
if err != nil {
	panic(err)
}
res, err := c.Do(req)
if err != nil {
	panic(err)
}
fmt.Println("Response status : ", res.StatusCode)
Output:

Response status :  200
Example (Expired)
c := NewClient(WithHTTPTimeout(1 * time.Millisecond))
req, err := http.NewRequest(http.MethodGet, "https://www.gojek.io/", nil)
if err != nil {
	panic(err)
}
res, err := c.Do(req)
if err != nil {
	fmt.Println("error:", err)
	return
}
fmt.Println("Response status : ", res.StatusCode)
Output:

func WithRetrier

func WithRetrier(retrier heimdall.Retriable) Option

WithRetrier sets the strategy for retrying

Example
c := NewClient(WithHTTPTimeout(1*time.Millisecond), WithRetryCount(3), WithRetrier(&mockRetrier{}))
req, err := http.NewRequest(http.MethodGet, "https://www.gojek.io/", nil)
if err != nil {
	panic(err)
}
res, err := c.Do(req)
if err != nil {
	fmt.Println("error")
	return
}
fmt.Println("Response status : ", res.StatusCode)
Output:

retry attempt 0
retry attempt 1
retry attempt 2
retry attempt 3
error

func WithRetryCount

func WithRetryCount(retryCount int) Option

WithRetryCount sets the retry count for the hystrixHTTPClient

Example
c := NewClient(WithHTTPTimeout(1*time.Millisecond), WithRetryCount(3))
req, err := http.NewRequest(http.MethodGet, "https://www.gojek.io/", nil)
if err != nil {
	panic(err)
}
res, err := c.Do(req)
if err != nil {
	fmt.Println("error:", err)
	return
}
fmt.Println("Response status : ", res.StatusCode)
Output:

Jump to

Keyboard shortcuts

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